34

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Regel
  • 607
  • 1
  • 6
  • 11
  • Have you tried setting + exporting the LD_LIBRARY_PATH before running g++ (instead of providing it on the g++ command line) ? – Frank Schmitt Apr 05 '13 at 14:37
  • I have tried exporting both LIBRARY_PATH and LD_LIBRARY_PATH before running g++. – Regel Apr 05 '13 at 14:42
  • @FrankSchmitt, setting it the way the OP did has the same effect as exporting it (but limited to that command and its children) so that's not going to help – Jonathan Wakely Mar 17 '15 at 13:29
  • 2
    Possible same, except no mention of `LIBRARY_PATH` not working: http://stackoverflow.com/questions/2726993/g-how-to-specify-preference-of-library-path You may also want to have a look at spec files: http://stackoverflow.com/a/7505529/895245 – Ciro Santilli OurBigBook.com May 15 '15 at 17:05
  • 2
    I wondering if you face the problem during runtime linking on the same system you compiled the binary. The -L option does not stop linker from linking the system library first before it during runtime. -rpath seem to be better option for you. – Sany Liew Dec 25 '16 at 18:00

2 Answers2

25

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.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • 13
    Why was this the accepted answer? It does not answer the question. – Utkarsh Sinha Dec 29 '15 at 20:54
  • This answer is very helpful because it explains why you cannot do what OP was asking, and what I've been trying to do as well. I'm not happy having to change my Makefiles, but at least now I know why the environment variables don't work. – joanis May 22 '19 at 17:24
  • 2
    The answer is incorrect. The standard gcc installation in Ubuntu puts LIBRARY_PATH first. If you compile gcc yourself, LIBRARY_PATH comes last. – facetus Feb 22 '20 at 18:58
0

Try specifying the library path in a .conf file in /etc/ld.so.conf.d/

The linker looks at paths specified in files in /etc/ld.so.conf.d/ while linking.

Make sure you run 'ldconfig' once you create the file, that will force it to update its cache.

Priyank Desai
  • 3,693
  • 1
  • 27
  • 17
  • 11
    I believe you are talking about the loader (ld.so) and not the linker (ld). – Programmer_D Jan 11 '15 at 10:14
  • 3
    Why this answer is so much "minused"? When I am doing strace on gcc binary, I can see /etc/ld.so.conf file is read (which usually includes ld.so.conf.d/*.conf). – Kuchara Nov 22 '17 at 15:54
  • 1
    I think both the loader and the linker look at this configuration, only the linker intended for cross-compilation will not. – Paul Stelian Apr 14 '20 at 18:38