I'm building a simple C++ program and I want to temporarily substitute a system supplied shared library with a more recent version of it, for development and testing.
I tried setting the LD_LIBRARY_PATH variable but the linker (ld) failed with:
/usr/bin/ld: cannot find -lyaml-cpp
I expected that to work because according to the ld man page:
The linker uses the following search paths to locate required shared libraries: ... For a native linker, the contents of the environment variable "LD_LIBRARY_PATH"...
I then tried setting the LIBRARY_PATH, and that worked.
According to the GCC manual:
The value of LIBRARY_PATH is a colon-separated list of directories, much like PATH. When configured as a native compiler, GCC tries the directories thus specified when searching for special linker files, if it can't find them using GCC_EXEC_PREFIX. Linking using GCC also uses these directories when searching for ordinary libraries for the -l option (but directories specified with -L come first).
As the (GCC) manual suggests, LIBRARY_PATH works because I link with GCC.
But..
- Since I link with
gccwhyldis being called, as the error message suggests? - What's the point of having two variables serving the same purpose? Are there any other differences?
4 Answers 4
LIBRARY_PATH is used by gcc before compilation to search directories containing static and shared libraries that need to be linked to your program.
LD_LIBRARY_PATH is used by your program to search directories containing shared libraries after it has been successfully compiled and linked.
EDIT:
As pointed below, your libraries can be static or shared. If it is static then the code is copied over into your program and you don't need to search for the library after your program is compiled and linked. If your library is shared then it needs to be dynamically linked to your program and that's when LD_LIBRARY_PATH comes into play.
8 Comments
LIBRARY_PATH is used for searching directories containing static AND dynamic libraries, instead of only static libraries.LIBRARY_PATH is searched for libraries (static or dynamic) at compile time and LD_LIBRARY_PATH is searched for dynamic libraries at run time. Of course at run time you don't need to search for static libraries.LD_LIBRARY_PATH is searched when the program starts, LIBRARY_PATH is searched at link time.
caveat from comments:
- When linking libraries with
ld(instead ofgccorg++), theLIBRARY_PATHorLD_LIBRARY_PATHenvironment variables are not read. - When linking libraries with
gccorg++, theLIBRARY_PATHenvironment variable is read (see documentation "gccuses these directories when searching for ordinary libraries").
4 Comments
ld by itself does not look for libraries in either LIBRARY_PATH or LD_LIBRARY_PATH. It's only when gcc invokes ld that LIBRARY_PATH becomes used. (Learned this the hard way.)ld and it says ld only search LD_LIBRARY_PATH when searching for dependencies of a shared library. If you just run ld -lSOMETHING it won't search those paths in LD_LIBRARY_PATH, indeed.Since I link with gcc why ld is being called, as the error message suggests?
gcc calls ld internally when it is in linking mode.
Comments
LIBRARY_PATH is used by linker (ld)
LD_LIBRARY_PATH is used by loader (ld.so)
-rpath-linkoption, which is related to finding shared libs that are dependencies of the shared libs you're linking to explicitly (to check that they satisfy any undefined refs in the ones you're linking to). The rpath-link dirs are not used to find the libs you're linking to explicitly,ldonly uses the dirs named with the-Loption for that (and GCC converts the contents ofLIBRARY_PATHinto-Loptions forld).