简体   繁体   中英

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

you can use c++filt to do the puzzling for you.

c++filt _ZN5CandyD2Ev

will return Candy::~Candy() so somehow Candy's destructor didn't get linked.

Well the first thing would be 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; 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:

  • nm: to find the symbols in object files
  • ld: to find how a library links
  • c++filt: to find the C++ name of a symbol from its mangled name

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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