Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I have to define LD_LIBRARY_PATH with an export every time I run my application?

I have some code that uses some shared libraries (c code on gcc). When compiling I have to explicitly define the include and library directories using -I and -L, since they aren't in the standard places. When I try to run the code, I get the following error:

./sync_test  ./sync_test: error while loading shared libraries: libsync.so: cannot open shared object file: No such file or directory 

However, do the following, everything works just fine:

export LD_LIBRARY_PATH="/path/to/library/" ./sync_test 

Now, the strange part is, this only works once. If I try and run sync_test again I get the same error unless I run the export command first. I tried adding the following to my .bashrc, but it made no difference:

LD_LIBRARY_PATH="/path/to/library/" 
like image 449
Paul Wicks Avatar asked Mar 29 '09 22:03

Paul Wicks


People also ask

What does export LD_LIBRARY_PATH do?

LD_LIBRARY_PATH is an environmental variable used in Linux/UNIX Systems. It is used to tell dynamic link loaders where to look for shared libraries for specific applications. It is useful until you don't mess with it.

Why do we need LD_LIBRARY_PATH?

In Linux, the environment variable LD_LIBRARY_PATH is a colon-separated set of directories where libraries should be searched for first, before the standard set of directories; this is useful when debugging a new library or using a nonstandard library for special purposes.

What does LD_LIBRARY_PATH mean?

LD_LIBRARY_PATH is the default library path which is accessed to check for available dynamic and shared libraries. It is specific to linux distributions. It is similar to environment variable PATH in windows that linker checks for possible implementations during linking time.

What is the difference between path and LD_LIBRARY_PATH?

The PATH environment variable specifies the search paths for commands, while LD_LIBRARY_PATH specifies the search paths for shared libraries for the linker.


1 Answers

You should avoid setting LD_LIBRARY_PATH in your .bashrc. See "Why LD_LIBRARY_PATH is bad" for more information.

Use the linker option -rpath while linking so that the dynamic linker knows where to find libsync.so during runtime.

gcc ... -Wl,-rpath /path/to/library -L/path/to/library -lsync -o sync_test 

EDIT:

Another way would be to use a wrapper like this

#!/bin/bash  LD_LIBRARY_PATH=/path/to/library sync_test "$@" 

If sync_test starts any other programs, they might end up using the libs in /path/to/library which may or may not be intended.

like image 93
sigjuice Avatar answered Oct 15 '22 00:10

sigjuice