开发者

Get the file causing linker error in g++

开发者 https://www.devze.com 2023-02-17 00:53 出处:网络
I am building a binary which uses multiple components which are compiled as .so files. I am getting a flurry of linker errors which point to which .so file caused them, but can I get information about

I am building a binary which uses multiple components which are compiled as .so files. I am getting a flurry of linker errors which point to which .so file caused them, but can I get information about which files are calling undefined functions or if possible source code locations where undefined functions are being called? I am finding it too tedious to hunt down the function esp since lot of overloading a开发者_StackOverflow社区nd templates are being used (which means same name in many places). In windows, it shows which .o file caused the undefined symbol, but am stuck at the level of libraries in linux. I use g++ in linux. Any pointer would be useful.


You are asking "which object file inside a shared library is causing the error".

The problem is that by the time that shared library has been linked, all object files have been "fused together", and no longer exist inside the shared library as separate entities, so your question is somewhat meaningless.

That said, if you do a debug build (with -g flag), the linker will tell you which source file and line is causing the problem, which you then may be able to translate into the object file.

If you can't (e.g. because the problem symbol is referenced in a header file), you can ask the linker for help: rebuild the library again, passing the linker -y flag:

g++ -fPIC -shared ${OBJECTS} -o foo.so -Wl,-y,my_unresolved_symbol

will tell you which object(s) reference my_unresolved_symbol.

Note: linker operates "below" C++, so you must pass the mangled name, e.g. _Znw to it.


With ldd (print shared library dependencies) you can check the dependencies of an so and if they are resolved or not.

With the use of nm -Aa --demangle you can get a list of symbols used or defined in an *.so file, as long as they were not stripped away. The used symbols should at least remain, so that you can check if there are some unresolved symbols.

0

精彩评论

暂无评论...
验证码 换一张
取 消