简体   繁体   English

g ++链接库ld没有

[英]g++ links library ld doesn't

I would like to know why 我想知道为什么

g++ test1.o test2.o -o test.x -lgtk-x11-2.0

works, while 工作,而

ld 'r test1.o test2.o -o test.o -lgtk-x11-2.0

gives: 得到:

ld: cannot find -lgtk-x11-2.0

unless I explicitly include the path adding -L/usr/lib/i386-linux-gnu 除非我明确包含添加-L/usr/lib/i386-linux-gnu的路径

I tried everything adding the path to $LD_LIBRARY_PATH or to ld.so.conf but nothing works and I cannot explain myself the difference between the two calls. 我尝试了添加$LD_LIBRARY_PATHld.so.conf路径的所有内容,但没有任何作用,我无法解释两个调用之间的区别。

On the manual of ld it is specified: 在ld的手册中指定:

The default set of paths searched (without being specified with `-L') depends on which emulation mode ld is using, and in some cases also on how it was configured 搜索的默认路径集(未指定为“-L”)取决于ld使用的仿真模式,在某些情况下还取决于它的配置方式

but trying different emulations (by using -m*emulation*) imply segfaulting (probably since the default emulation elf_i386, is the right one). 但尝试不同的仿真(通过使用-m *仿真*)意味着segfaulting(可能自默认仿真elf_i386,是正确的)。

Can you help me shed light on this topic? 你能帮我解释一下这个话题吗?

gcc -print-search-dirs (or g++ ) displays the library search path used by the compiler; gcc -print-search-dirs (或g++ )显示编译器使用的库搜索路径; /usr/lib/i386-linux-gnu is likely built in. This option is passed to the linker by gcc, but not built in to the linker. /usr/lib/i386-linux-gnu很可能是内置的。这个选项通过gcc传递给链接器,但没有内置到链接器中。

Try adding -v : g++ -v test1.o test2.o -o test.x -lgtk-x11-2.0 to see how the linker is invoked. 尝试添加-vg++ -v test1.o test2.o -o test.x -lgtk-x11-2.0以查看如何调用链接器。

If you are curious about the paths used by ld 如果你对ld使用的路径感到好奇

ld --verbose | grep SEARCH

usually you don't want to use ld explicitly for linking, it's probably better to perform every task with gcc alone, and you should prefer the latter over the former. 通常你不想明确地使用ld进行链接,单独用gcc执行每个任务可能会更好,而你应该更喜欢后者而不是前者。

If you want to know more about gcc settings 如果您想了解更多有关gcc设置的信息

gcc -dumpspecs

gives you a taste of what it's like to take a look at the internals of gcc , you will probably end up taking a look at the gcc manual if you are serious about this, but I don't think that is something worth doing just for the sake of it. 让你体会一下看看gcc的内部结构是什么样的,如果你认真对待这个问题,你最终可能会看看gcc手册,但我认为这不值得为为了它。

Settings defined at the time when gcc was compiled can be important too, so you should also learn how to compile gcc from source if you really want to grasp how the internals work. 编译gcc时定义的设置也很重要,所以如果你真的想了解内部的工作方式,你还应该学习如何从源代码编译gcc

tl;dr TL;博士

Just use gcc for both compiling and linking, other options are probably not worth the trouble. 只需使用gcc进行编译和链接,其他选项可能不值得麻烦。

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

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