33

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.

5
  • Have you tried setting + exporting the LD_LIBRARY_PATH before running g++ (instead of providing it on the g++ command line) ? Apr 5, 2013 at 14:37
  • I have tried exporting both LIBRARY_PATH and LD_LIBRARY_PATH before running g++.
    – Regel
    Apr 5, 2013 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 Mar 17, 2015 at 13:29
  • 2
    Possible same, except no mention of LIBRARY_PATH not working: stackoverflow.com/questions/2726993/… You may also want to have a look at spec files: stackoverflow.com/a/7505529/895245 May 15, 2015 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, 2016 at 18:00

2 Answers 2

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.

3
  • 13
    Why was this the accepted answer? It does not answer the question. Dec 29, 2015 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, 2019 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, 2020 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.

3
  • 11
    I believe you are talking about the loader (ld.so) and not the linker (ld). Jan 11, 2015 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, 2017 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. Apr 14, 2020 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.