206

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 gcc why ld is being called, as the error message suggests?
  • What's the point of having two variables serving the same purpose? Are there any other differences?
Georgios Politis
  • 2,610
  • 3
  • 19
  • 10

4 Answers4

267

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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Naveen
  • 4,456
  • 2
  • 22
  • 16
  • 17
    And of course LD_LIBRARY_PATH only makes sense with dynamic libraries – Alex Jasmin Nov 22 '10 at 22:11
  • 2
    My point is that if I were to use ld for linking (directly), then, according to the ld manual, LD_LIBRARY_PATH would have been used to search for directories containing the libraries that need to be linked to my program. I must be missing something here.. – Georgios Politis Nov 22 '10 at 22:20
  • 2
    unless you invoke ld yourself and combine the object files with the libraries, it will 'inherit' the path that gcc passes to it. You can override the standard gcc, with -Xlinker options. – Naveen Nov 22 '10 at 22:27
  • 7
    Actually, `LIBRARY_PATH ` is used for searching directories containing static **AND** dynamic libraries, instead of only static libraries. – particle128 Jan 01 '19 at 09:11
  • 8
    Yeah this is wrong - the difference is that `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. – Timmmm Feb 01 '19 at 12:50
  • would be great to also mention LD_RUN_PATH, even though it is not in the question – Evan Benn Apr 04 '19 at 23:04
  • @Timmmm that's wrong, too ;) ... it's searched at _link time_. Just because the _compiler driver_ is used to do the linker step, doesn't mean it's _compiling_. – 0xC0000022L May 31 '23 at 09:47
  • Yes if you are being pedantic. Linking is part of the compilation process. "compile-time" usually means "when compiling and linking". – Timmmm Jun 01 '23 at 10:24
62

LD_LIBRARY_PATH is searched when the program starts, LIBRARY_PATH is searched at link time.

caveat from comments:

Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
F'x
  • 12,105
  • 7
  • 71
  • 123
  • 46
    Note: when linking libraries, `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.) – Rufflewind Jul 02 '14 at 00:34
  • 1
    @Rufflewind Interesting, but would have been even more if you had given any reference. – hmijail Feb 15 '16 at 17:50
  • This view makes a distinction in the moment the libraries are searched for (link time versus run time) while @Naveen makes a distinction of the type of libraries that are searched for (static v dynamic). Are there two views effectively identical (dynamic : run time = static : link time) or are there important situations when this correspondence does not hold? I would guess that some knowledge about the dynamic libraries is needed also at compile time. – XavierStuvw Oct 31 '18 at 10:41
  • @Rufflewind Hi Rufflewind. I read the manual for `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. – Frank Dec 12 '20 at 06:03
13

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.

heroxbd
  • 750
  • 1
  • 7
  • 20
0

LIBRARY_PATH is used by linker (ld)

LD_LIBRARY_PATH is used by loader (ld.so)

zzzhhh
  • 291
  • 3
  • 10
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 04 '21 at 10:50