Why is -L needed when -rpath is used?

假装没事ソ 提交于 2019-11-28 17:37:42

问题


I find that the -L flag must be given when using -rpath. For instance:

gcc -o test test.o -L. -lmylib -Wl,-rpath=.

Why is the -L flag needed? What information more than the information from the h-files are needed at compile time?

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

Provided that libmyLib can be found in /usr/lib, that is. Why isn't -L needed now?

This is a follow-up question to https://stackoverflow.com/a/8482308/1091780.


回答1:


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.

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

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




回答2:


A clarification of OMGtechy's answer.

If the linker does not check which symbols are provided by a library, it can never tell you if any symbols are missing at compile time. They might be in one of the libraries loaded at run-time. You could never know. There is no connection at compile time between the header files of a library and the .so file.



来源:https://stackoverflow.com/questions/28096133/why-is-l-needed-when-rpath-is-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!