How to Specify Preference of Library Path

How to specify preference of library path?

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

Your solution should work with using the -L/my/dir -lfoo options, at runtime use LD_LIBRARY_PATH to point to the location of your library.

Careful with using LD_LIBRARY_PATH - in short (from link):

..implications..:

Security: Remember that the directories specified in LD_LIBRARY_PATH get searched before(!) the standard locations? In that
way, a nasty person could get your application to load a version of a
shared library that contains malicious code! That’s one reason why
setuid/setgid executables do neglect that variable!

Performance: The link loader has to search all the directories specified, until it finds the directory where the shared library
resides – for ALL shared libraries the application is linked against!
This means a lot of system calls to open(), that will fail with
“ENOENT (No such file or directory)”! If the path contains many
directories, the number of failed calls will increase linearly, and
you can tell that from the start-up time of the application. If some
(or all) of the directories are in an NFS environment, the start-up
time of your applications can really get long – and it can slow down
the whole system!

Inconsistency: This is the most common problem. LD_LIBRARY_PATH forces an application to load a shared library it wasn’t linked
against, and that is quite likely not compatible with the original
version. This can either be very obvious, i.e. the application
crashes, or it can lead to wrong results, if the picked up library not
quite does what the original version would have done. Especially the
latter is sometimes hard to debug.

OR

Use the rpath option via gcc to linker - runtime library search path, will be used
instead of looking in standard dir (gcc option):

-Wl,-rpath,$(DEFAULT_LIB_INSTALL_PATH)

This is good for a temporary solution. Linker first searches the LD_LIBRARY_PATH for libraries before looking into standard directories.

If you don't want to permanently update LD_LIBRARY_PATH you can do it on the fly on command line:

LD_LIBRARY_PATH=/some/custom/dir ./fooo

You can check what libraries 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)

How to specify the correct library to use when LD_LIBRARY_PATH includes path to both 32 bit and 64 bit versions

In answer to your second question, you only used -L to specify the link time path. You need to also add -rpath=<path> for each path that you specify with -L. to get it to look at run-time as well.

If you're using the gcc/g++ driver for linking, then you should use -Wl,-rpath=<path>

e.g. library libfoo.so in /opt/libs/lib64:

gcc -o test test.o -L/opt/libs/lib64 -Wl,-rpath=/opt/libs/lib64 -lfoo

In answer to your first question, if LD_LIBRARY_PATH is causing issues, then your first port of call is ldd which shows the paths to the libraries that are being linked at run-time. Start with an empty string and work up from there.

Thirdly, if the reason that LD_LIBRARY_PATH needs to be set is because there are libraries in /opt/lib, etc... then you should be adding these paths to your /etc/ld.so.conf and using ldconfig to update the search map for these libraries. This will typically save you from needing the LD_LIBRARY_PATH variable at all.

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

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.

How do I change the default library path for R packages

See help(Startup) and help(.libPaths) as you have several possibilities where this may have gotten set. Among them are

  • setting R_LIBS_USER
  • assigning .libPaths() in .Rprofile or Rprofile.site

and more.

In this particular case you need to go backwards and unset whereever \\\\The library/path/I/don't/want is set.

To otherwise ignore it you need to override it use explicitly i.e. via

library("somePackage", lib.loc=.libPaths()[-1])

when loading a package.

gcc linker library search order; paths plus static vs shared

In your example, the static library libmylibrary.a will be linked in
preference to any libmylibrary.a or libmylibrary.so that might
exist in one of the linker's default search directories. The linker
searchs in mypath before any of the default places, and as soon as
it finds a libmylibrary.a or libmylibrary.so, it looks no further
to satisfy -lmylibrary.

If mypath contained both libmylibrary.a and libmylibrary.so, the
latter would be preferred.

I am not sure what source you are referring to by "the gcc manual", but
"the gcc linker" is the GNU linker, ld, and you will find the
meanings of the commandline options very well explained in its manual

How to set the java.library.path from Eclipse

Don't mess with the library path! Eclipse builds it itself!

Instead, go into the library settings for your projects and, for each jar/etc that requires a native library, expand it in the Libraries tab. In the tree view there, each library has items for source/javadoc and native library locations.

Specifically: select Project, right click -> Properties / Java Build Path / Libraries tab, select a .jar, expand it, select Native library location, click Edit, folder chooser dialog will appear)

Messing with the library path on the command line should be your last ditch effort, because you might break something that is already properly set by eclipse.

Native library location



Related Topics



Leave a reply



Submit