为什么-L使用-rpath时需要? [英] Why is -L needed when -rpath is used?
问题描述
我发现使用-rpath时必须给出-L标志。例如:
I find that the -L flag must be given when using -rpath. For instance:
gcc -o test test.o -L. -lmylib -Wl,-rpath=.
为什么需要-L标志?在编译时间需要比h文件中的信息更多的信息?
Why is the -L flag needed? What information more than the information from the h-files are needed at compile time?
如果我删除-L。我收到以下消息:
If I remove -L. I get the following message:
gcc -o test test.o -lmylib -Wl,-rpath=.
/usr/bin/ld: cannot find -lmyLib
完全可以同时删除标志,但是。像这样:
It's perfectly ok to remove both flags, though. Like this:
gcc -o test test.o -lmylib
假设libmyLib可以在/ usr / lib中找到,那就是。为什么现在不需要-L?
Provided that libmyLib can be found in /usr/lib, that is. Why isn't -L needed now?
这是对 https://stackoverflow.com/a/8482308/1091780 。
推荐答案
即使动态库也需要一个静态联动程度;链接器需要知道动态库应该提供哪些符号。关键的区别在于,动态库在运行时提供了定义,而完全静态库在链接时提供了定义。
Even dynamic libraries required a degree of static linkage; the linker needs to know what symbols should be supplied by the dynamic library. The key difference is that the dynamic library provides the definition at runtime, whilst with fully static library provides the definition at link time.
因此, -L
用于指定要链接的文件的位置,就像 -l </ code>指定特定的库一样。
。
表示当前目录。
For this reason, -L
is needed to specify where the file to link against is, just as -l
specifies the specific library. The .
indicates the current directory.
-rpath
在应用程序尝试加载动态库时在运行时发挥作用。它会通知程序在尝试加载动态库时要搜索的其他位置。
-rpath
comes into play at runtime, when the application tries to load the dynamic library. It informs the program of an additional location to search in when trying to load a dynamic library.
原因 -L / usr / lib
不需要指定是因为链接器默认在那里寻找(因为这是一个非常常见的放置库的地方)。
The reason -L/usr/lib
doesn't need to be specified is because the linker is looking there by default (as this is a very common place to put libraries).
这篇关于为什么-L使用-rpath时需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!