Is there a way to ignore unused undefined references?

前端 未结 2 1285
不知归路
不知归路 2021-01-12 15:39

Suppose I have two source files — UndefErr.cpp:

#include 

void UndefFunc();
void Func2(){UndefFunc();}

void Func1(){printf(\"Hi\\n\"         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-12 16:14

    The problem is that your undefined function is used. The object code for Func2 has been generated, there's a reference to unexisting UndefFunc, and it goes to the linker. Compiler has no way to understand that this function will always be undefined as there are multiple translation units.

    The only way to avoid compilation error is to tell linker that you don't need object code for unused functions, so that it won't try to generate assembly for this case.

提交回复
热议问题