GCC how to add before the default linker search path by default? LIBRARY_PATH not working

Regel picture Regel · Apr 5, 2013 · Viewed 57k times · Source

I'm trying to figure out how to set some environment variable which would make g++ to link to correct versions of the libraries.

I have some old boost libraries in /usr/lib64 (linking against these will fail) and new libraries in /v/users/regel/lib. So the linker should link against the new libraries.

Command:

$ g++ test.cpp -lboost_system -L/v/users/regel/lib

links the program correctly. However, I wish to set this as the number 1 search directory for the linker so that I don't have to specify '-L' every time I link.

The following environment variables do not seem to do the trick:

$ LIBRARY_PATH=/v/users/regel/lib g++ test.cpp -lboost_system
/tmp/regel/cc4SmBtI.o: In function `main':
test.cpp:(.text+0x5): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status

and

$ LD_LIBRARY_PATH=/v/users/regel/lib:$LD_LIBRARY_PATH g++ test.cpp -lboost_system
/tmp/regel/ccUreBZy.o: In function `main':
test.cpp:(.text+0x5): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status

Despite reading numerous articles and posts on similar subjects, I have not found a solution yet.

Answer

Jonathan Wakely picture Jonathan Wakely · Mar 17, 2015

As the GCC manual says, LIBRARY_PATH is the correct environment variable to add directories to the library search path.

If you add -v to the g++ command you should see the LIBRARY_PATH that it uses, and you should see it includes the directory you have specified, and that it gets added to the collect2 command as -L, but you will see it gets added after the standard directories such as -L/usr/lib etc.

I don't know any way to make the directories in LIBRARY_PATH come first, I think you have to use -L for that.