有没有办法忽略未使用的未定义的引用? [英] Is there a way to ignore an unused undefined references?

查看:165
本文介绍了有没有办法忽略未使用的未定义的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个源代码文件 - UndefErr.cpp

  include< cstdio> 

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

void Func1(){printf(Hi\\\
);}
/ pre>

并且 main.cpp

  void Func1(); 

int main(){
Func1();
return 0;
}

正如您在 UndefErr.cpp Func2()会触发错误,因为它使用未定义的 UndefFunc()。但在主函数中我不关心 Func2()。因此,其中一个问题,我只能传递一个选项 - unresolved-symbols = ignore-in-object-files 链接器,但我想要一个不同的东西。我需要一个链接器知道未定义的函数在某处使用,并仅在这些情况下触发错误。我正在使用GCC。



我问这样一个奇怪的问题的原因是我试图使用 lwIP ,并且很难理解它的所有依赖项(我只需要TCP / IP),我找不到在互联网上的教程。所以我想我可以编译大多数(或所有).c文件分开,并写一些简单的测试,看看它做什么。但是这种方法崩溃的未定义的引用大多数可能在特定情况下没有意义。

解决方案

2我设法链接代码没有错误与以下:

  $ g ++ -c main.cpp -O3 -flto 
$ g ++ -c UndefErr.cpp -O3 -flto
$ g ++ main.o UndefErr.o -flto -O3 -o out

我知道 -flto 会使链接器的行为像 -fwhole-program 被传递,整个事情是一个单一的编译单元。根据手册, -fwhole-program 对应于函数上 static 的正确使用,因此允许未使用的函数从输出中消除(即,你确保编译器的所有函数不会被其他代码使用,可能动态加载,并且你为用户保证的唯一入口点是 main() )。



我不得不添加 -O3 ,但编译器对检查函数和消除死代码不感兴趣。


Suppose that I have a two source code files -- UndefErr.cpp:

#include <cstdio>

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

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

And the main.cpp:

void Func1();

int main(){
    Func1();
    return 0;
}

As you see in the UndefErr.cpp the Func2() going to trigger an error, as it use the UndefFunc() which is undefined. But in the main function I don't care about the Func2() at all. Accordingly to one of the questions I can just pass an option --unresolved-symbols=ignore-in-object-files to the linker, but I want a kinda different thing. I need a linker to know if the undefined functions used somewhere, and trigger an error in only those cases. I'm using GCC.

The reason why I'm asking such a strange question is that I am trying to use a lwIP, and it is hard to understand all of it's dependencies(I am only need TCP/IP), and I can't find a tutorials on the internet. So I thought I could compile most(or all) the .c files separately, and write some simple tests to see what it does. But this approach crashing by an undefined references most of which probably have no a sense in the particular case.

解决方案

With Gcc 4.8.2 I managed to link the code without errors with the following:

$ g++ -c main.cpp -O3 -flto
$ g++ -c UndefErr.cpp -O3 -flto
$ g++ main.o UndefErr.o -flto -O3 -o out

I know -flto will make the linker behave as if -fwhole-program was passed and the whole thing was a single compilation unit. And -fwhole-program, according to the manual, corresponds to the proper usage of static on functions, so allowing the unused function to be eliminated from the output (i.e. you assure the compiler all your functions will not be used by some other code, possibly dynamically loaded, and the only entry point you guarantee for your users is main()).

I had to add -O3, I am not sure why exactly, but the compiler was not very interested in inspecting the functions and eliminating dead code without it.

这篇关于有没有办法忽略未使用的未定义的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆