Why is -L required when -rpath is used?

I found that when using -rpath, the -L flag should be set. For example:

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

Why is the -L flag needed? What information is more than information from h files 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 
However, it is normal to remove both flags. Like this:
 gcc -o test test.o -lmylib 

If libmyLib can be found in / usr / lib, that is. Why not -L needed now?

This is the following question: stack overflow

+10
gcc linker shared-libraries
Jan 22 '15 at 18:29
source share
2 answers

Even dynamic libraries required a certain static link; the linker needs to know which characters should be provided by the dynamic library. The main difference is that the dynamic library provides the definition at runtime, while the fully static library provides the definition at link time.

For this reason, -L must specify where the file for the link is located, just as -L indicates a specific library. . indicates the current directory.

-rpath enters the game at runtime when the application tries to load a dynamic library. It tells the program additional search space when trying to load a dynamic library.

The reason -L/usr/lib not required to be specified, because the linker searches by default (since this is a very common place to put libraries).

+13
Jan 22 '15 at 18:32
source share

Clarification of OMGtechy's answer.

If the linker does not check which characters are provided by the library, it will never be able to tell if any characters are missing at compile time. They may be in one of the libraries loaded at runtime. You will never know. At compile time, there is no connection between the library header files and the .so file.

0
Aug 30 '17 at 15:44
source share



All Articles