链接器选项列表库使用 [英] linker option to list libraries used

查看:117
本文介绍了链接器选项列表库使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在Linux平台上,并使用GNU C ++编译器。我试图解析说,一些符号是不确定的链接错误。我可以去找到提供的命令行上的名字库,看看它们包含有问题的符号(使用'纳米'实用程序)。

I am working on a Linux platform and using the GNU C++ compiler. I am trying to resolve a link error that says some symbols are undefined. I can go find libraries with the name provided on the command line, and see that they contain the symbols in question ( using the 'nm' utility ).

我知道,编译步骤,我可以使用命令行标志-H而不是-c为了得到那被包含(#include)到编译头文件的列表。是否有链接的类似选项?我想如果我可以看到,用来处理每个'-lmylibrary'标志的链接,我可以进一步解决的文件列表。

I know that for the compilation step, I can use the command-line flag '-H' instead of '-c' in order to get a list of header files that were #included into the compilation. Is there a similar option for the linker? I figure if I could see the list of files that the linker used to handle each '-lmylibrary' flag, I can troubleshoot further.

推荐答案

如果你的库链接到可能不会像你想象的那么有用未定义的符号错误就意味着你忘了把其中一些图书馆,知道,因为很明显的标志是从这些库丢失,但是你可以使用 -print文件名=库选项找出哪个路径GCC将使用链接库,例如:

If you get an undefined symbol error it means you forgot to link some library, knowing which libraries you link to will probably not be as useful as you may think, because obviously the symbol is missing from those libraries, however you can use the -print-file-name=library option to find out which path gcc would use to link a library, example:

$ gcc -print-file-name=libc.a
/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../libc.a

又路过 - 跟踪来的链接显示了相似的输出

Also passing --trace to the linker shows a similar output

gcc -Wl,--trace myprog.c -o myprog -L. -lmylib
-lmylib (./libmylib.a)
-lgcc_s (/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/libgcc_s.so)
....

(在上面注 - 跟踪的参数给连接器的如此这般的 -Wl GCC 引用用于连接不会发出任何有用的 - 跟踪作为参数 GCC 自身)。

(Note in the above that --trace is an argument to the linker so it goes in -Wl. gcc invoked for linking won't emit anything useful for --trace as an argument to gcc its self).

您也可以使用 LDD 之后的你成功打造程序,找出被使用的动态链接库,它的输出结果如下:

You could also use ldd after you successfully build the program to find out which dynamically linked libraries were used, its output looks like this:

ldd `which ls`
linux-vdso.so.1 =>  (0x00007fff7ffff000)
libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f2e8ea93000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f2e8e88b000)
libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f2e8e682000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2e8e2ee000)
....

这篇关于链接器选项列表库使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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