When we creating a binary using the Dynamic library we apply gcc -o binary main.o -L. -lmylib -Wl,-rpath,.
where -L.
indicate that linker must search library in current directory. Why without -Wl,-rpath,.
we can't use the dynamic library?
Asked
Active
Viewed 202 times
0
-
Have you read 'man ld.so'? – bmargulies Jan 02 '14 at 00:19
2 Answers
0
By default, ld.so
searches in the directories specified by LD_LIBRARY_PATH
. If your shared lib isn't in one of those, it won't be found.
The -rpath
option to ld
causes it to store a pathname in the executable that ld.so
will look in.

bmargulies
- 97,814
- 39
- 186
- 310
0
There are several directories in the system where ld.so looks for libraries. If your library is not in a directory from that list, you have two alternatives:
- Specify path to it in the
LD_LIBRARY_PATH
variable; - Write path to it in the
rpath
of the binary.
You can change/view rpath of the binary using chrpath
.
Difference betwee -rpath
and -L
:
-rpath=dir
Add a directory to the runtime library search path. This is used
when linking an ELF executable with shared objects. All -rpath
arguments are concatenated and passed to the runtime linker, which
uses them to locate shared objects at runtime.
vs.
-L searchdir
--library-path=searchdir
Add path searchdir to the list of paths that ld will search for
archive libraries and ld control scripts.

Community
- 1
- 1

Igor Chubin
- 61,765
- 13
- 122
- 144
-
Thanks for your answer! I have one doubt: Is it true that when we use shared object (.so) linker is dynamically uploaded a corresponding object file when it needs or all objects file uploaded after beginnig execution? – Jan 02 '14 at 01:26
-
See http://stackoverflow.com/questions/5130654/when-how-does-linux-load-shared-libraries-into-address-space – Igor Chubin Jan 02 '14 at 10:46