Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gcc

g++

linker

ld

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.

like image 925
Regel Avatar asked Apr 05 '13 14:04

Regel


People also ask

Where does the linker search for libraries?

The linker searches for libraries first in any directories specified by the -L options and then in the standard directories. This option is useful only if it is placed preceding the –l library options to which it applies.

What is Rpath in GCC?

In computing, rpath designates the run-time search path hard-coded in an executable file or library. Dynamic linking loaders use the rpath to find required libraries. Specifically, it encodes a path to shared libraries into the header of an executable (or another shared library).

How do I find the library path in Linux?

By default, libraries are located in /usr/local/lib, /usr/local/lib64, /usr/lib and /usr/lib64; system startup libraries are in /lib and /lib64. Programmers can, however, install libraries in custom locations. The library path can be defined in /etc/ld.

What is linking in GCC?

Linking is performed when the input file are object files " .o " (instead of source file " . cpp " or " . c "). GCC uses a separate linker program (called ld.exe ) to perform the linking.


1 Answers

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.

like image 125
Jonathan Wakely Avatar answered Sep 19 '22 07:09

Jonathan Wakely