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

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

问题描述

我正在使用 Linux 平台并使用 GNU C++ 编译器.我正在尝试解决显示某些符号未定义的链接错误.我可以使用命令行中提供的名称查找库,并查看它们是否包含有问题的符号(使用nm"实用程序).

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"来获取编译中#included 的头文件列表.链接器是否有类似的选项?我想如果我能看到链接器用来处理每个-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-file-name=library 选项来找出 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

同样将 --trace 传递给链接器会显示类似的输出

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)
....

(请注意,上面的 --trace链接器的参数,因此它进入 -Wl.gcc为链接而调用的 不会发出任何对 --trace 有用的东西作为 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天全站免登陆