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/"
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.
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.
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.
The PATH environment variable specifies the search paths for commands, while LD_LIBRARY_PATH specifies the search paths for shared libraries for the linker.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With