How to specify library path preference?

I am compiling a C ++ program using g++ and ld . I have a .so library that I want to use during linking. However, a library with the same name exists in /usr/local/lib , and ld selects this library over the one I directly point to. How can i fix this?

In the examples below, my library file is /my/dir/libfoo.so.0 . Things I tried that don't work:

  • My command g ++ g++ -g -Wall -o my_binary -L/my/dir -lfoo bar.cpp
  • adding /my/dir to the beginning or end of my $PATH en` variable
  • adding /my/dir/libfoo.so.0 as an argument to g ++
+62
c ++ linker g ++
Apr 28 '10 at 5:17
source share
5 answers

Add the path to your new LD_LIBRARY_PATH library (it has a slightly different name on Mac ...)

Your solution should work using the -L/my/dir -lfoo , at run time use LD_LIBRARY_PATH to indicate the location of your library.

OR

Use the rpath option via gcc in the search path of the link library - runtime, will be used instead of searching in the standard directory (gcc option):

 -Wl,-rpath,$(DEFAULT_LIB_INSTALL_PATH) 

This is useful for a workaround. Linker first looks for LD_LIBRARY_PATH for libraries before looking into standard directories.

If you do not want to constantly update LD_LIBRARY_PATH, you can do this on the fly at the command line:

 LD_LIBRARY_PATH=/some/custom/dir ./fooo 

You can check which libraries the linker knows about using (example):

 /sbin/ldconfig -p | grep libpthread libpthread.so.0 (libc6, OS ABI: Linux 2.6.4) => /lib/libpthread.so.0 

And you can check which library your application is using:

 ldd foo linux-gate.so.1 => (0xffffe000) libpthread.so.0 => /lib/libpthread.so.0 (0xb7f9e000) libxml2.so.2 => /usr/lib/libxml2.so.2 (0xb7e6e000) librt.so.1 => /lib/librt.so.1 (0xb7e65000) libm.so.6 => /lib/libm.so.6 (0xb7d5b000) libc.so.6 => /lib/libc.so.6 (0xb7c2e000) /lib/ld-linux.so.2 (0xb7fc7000) libdl.so.2 => /lib/libdl.so.2 (0xb7c2a000) libz.so.1 => /lib/libz.so.1 (0xb7c18000) 
+68
Apr 28 '10 at 5:29
source share

Specifying the absolute path to the library should work fine:

 g++ /my/dir/libfoo.so.0 ... 

Did you remember to remove -lfoo after adding the absolute path?

+16
Apr 28 2018-10-10T00:
source share

This is an old question, but no one seems to have mentioned it.

You are fortunate that this was generally related.

You needed to change

 g++ -g -Wall -o my_binary -L/my/dir -lfoo bar.cpp 

:

 g++ -g -Wall -o my_binary -L/my/dir bar.cpp -lfoo 

Your linker keeps track of the characters you need to resolve. If he first reads the library, he does not have the required characters, so he ignores the characters in it. List libraries after what you need. to reference them so that your linker has characters to find in them.

In addition, -lfoo does a search for a file named libfoo.a or libfoo.so as necessary. Not libfoo.so.0 . Therefore, either ln name or rename the library as appopriate.

To quote the gcc man page:

 -l library ... It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded. 

Adding the file directly to the g++ line should work, unless, of course, you put it before bar.cpp , as a result of which the linker ignores it due to the lack of necessary characters, because the characters are not needed yet.

+10
Nov 15 '16 at 5:34
source share

Alternatively, you can use the environment variables LIBRARY_PATH and CPLUS_INCLUDE_PATH , which respectively indicate where to look for libraries and where to look for headers ( CPATH will also do the job), without specifying - L and -I.

Edit: CPATH includes a header with -I and CPLUS_INCLUDE_PATH with -isystem .

+8
Apr 28 '10 at 6:40
source share

If you use to work with DLLs in Windows and want to skip .so version numbers in linux / QT, adding "CONFIG + = plugin" will result in version numbers. To use the absolute path to .so, giving it to the linker works just fine, as Mr. Klacko noted.

0
Jan 08 '16 at 23:50
source share



All Articles