[gcc] LD_LIBRARY_PATH vs LIBRARY_PATH

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?

This question is related to gcc g++ ld

The answer is


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.


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

caveat from comments:


Examples related to gcc

Can't compile C program on a Mac after upgrade to Mojave Compiling an application for use in highly radioactive environments Make Error 127 when running trying to compile code How to Install gcc 5.3 with yum on CentOS 7.2? How does one set up the Visual Studio Code compiler/debugger to GCC? How do I set up CLion to compile and run? CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found How to printf a 64-bit integer as hex? Differences between arm64 and aarch64 Fatal error: iostream: No such file or directory in compiling C program using GCC

Examples related to g++

How do I set up CLion to compile and run? Compile c++14-code with g++ Fatal error: iostream: No such file or directory in compiling C program using GCC How does #include <bits/stdc++.h> work in C++? DSO missing from command line C++ unordered_map using a custom class type as the key How do I enable C++11 in gcc? usr/bin/ld: cannot find -l<nameOfTheLibrary> cc1plus: error: unrecognized command line option "-std=c++11" with g++ to_string is not a member of std, says g++ (mingw)

Examples related to ld

What's the difference between .so, .la and .a library files? ld cannot find -l<library> How to print the ld(linker) search path How to remove unused C/C++ symbols with GCC and ld? I don't understand -Wl,-rpath -Wl, Compiling problems: cannot find crt1.o How to set the LDFLAGS in CMakeLists.txt? LD_LIBRARY_PATH vs LIBRARY_PATH Why do I have to define LD_LIBRARY_PATH with an export every time I run my application?