简体   繁体   English

如何处理C ++ / GNU工具链中的链接器错误?

[英]How to handle linker errors in C++/GNU toolchain?

给定一个C ++ / GNU工具链,什么是解决链接器错误的好方法或工具或策略?

Not sure exactly what you mean but if you are talking about cryptic linker symbols like: 不确定确切的意思,但是如果您正在谈论诸如以下这样的隐秘链接器符号:

mylib.so: undefined symbol: _ZN5CandyD2Ev mylib.so:未定义符号:_ZN5CandyD2Ev

you can use c++filt to do the puzzling for you. 您可以使用c ++ filt为您解决难题。

c++filt _ZN5CandyD2Ev C ++过滤_ZN5CandyD2Ev

will return Candy::~Candy() so somehow Candy's destructor didn't get linked. 将会返回Candy ::〜Candy(),因此不知道Candy的析构函数没有被链接。

Well the first thing would be RTFM. 那么第一件事就是RTFM。 No, seriously, read the documentation. 不,认真阅读文档。

If you don't want to do that, try a search on the error that comes up. 如果您不想这样做,请尝试搜索出现的错误。

Here are a few other things to remember: "missing" symbols are often an indication that you haven't included the appropriate source or library; 还有一些其他需要记住的事情:“缺失”符号通常表示您没有包括适当的源代码或库; "missing" symbols are sometimes an indication that you're attempting to link a library created with a different mangling convention (or a different compiler); 有时,“缺少”符号表示您正在尝试链接使用不同的处理约定(或不同的编译器)创建的库。 make sure that you have extern "C" where appropriate; 确保在适当的地方加上“ C”字样; declaring and defining aren't the same thing; 声明和定义不是一回事; if your compiler doesn't support "export" make sure your template code is available for when you instantiate objects. 如果您的编译器不支持“导出”,请确保在实例化对象时可使用模板代码。

With gcc toolchain, I use: 通过gcc工具链,我使用:

  • nm: to find the symbols in object files nm:在目标文件中查找符号
  • ld: to find how a library links ld:查找库如何链接
  • c++filt: to find the C++ name of a symbol from its mangled name c ++ filt:从错误的名称中查找符号的C ++名称

Check this for details. 检查以获取详细信息。

Look at the names of the symbols that are reported to be problematic. 查看报告有问题的符号的名称。

  • If there are missing symbols reported, find out in which source files or libraries those function/... should be defined in. Inspect the compilation/linker settings to find out why these files aren't compiled or linked. 如果报告了缺少的符号,请找出应在哪些源文件或库中定义这些功能/...。检查编译/链接器设置以找出为什么未编译或链接这些文件的原因。
  • If there are multiply defined symbols the linker usually mentions which object files or libraries contain them. 如果存在多个定义的符号,则链接程序通常会提及包含它们的对象文件或库。 Look at those files/their sources to find out why the offending functions/... are included in both of them. 查看这些文件/它们的来源,以找出两个文件中都包含令人反感的功能/ ...的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM