[gcc] How to specify non-default shared-library path in GCC Linux? Getting "error while loading shared libraries" when running

There is a laptop on which I have no root privilege.

onto the machine I have a library installed using configure --prefix=$HOME/.usr .

after that, I got these files in ~/.usr/lib :

libXX.so.16.0.0
libXX.so.16
libXX.so
libXX.la
libXX.a

when I compile a program that invokes one of function provided by the library with this command : gcc XXX.c -o xxx.out -L$HOME/.usr/lib -lXX

xxx.out was generated without warning, but when I run it error like this was thrown:

./xxx.out: error while loading shared libraries: libXX.so.16: cannot open shared object file: No such file or directory , though libXX.so.16 resides there.

my clue-less assumption is that ~/.usr/lib wasn't searched when xxx.out is invoked. but what can I do to specify path of .so , in order that xxx.out can look there for .so file?

An addition is when I feed -static to gcc, another error happens like this:

undefined reference to `function_proviced_by_the_very_librar'

It seems .so does not matter even though -L and -l are given to gcc. what should I do to build a usable exe with that library?


For other people who has the same question as I did

I found a useful article at tldp about this.

It introduces static/shared/dynamic loaded library, as well as some example code to use them.

This question is related to gcc linker shared-libraries

The answer is


There are two ways to achieve that:

  • Use -rpath linker option:

gcc XXX.c -o xxx.out -L$HOME/.usr/lib -lXX -Wl,-rpath=/home/user/.usr/lib

  • Use LD_LIBRARY_PATH environment variable - put this line in your ~/.bashrc file:

    export LD_LIBRARY_PATH=/home/user/.usr/lib

This will work even for a pre-generated binaries, so you can for example download some packages from the debian.org, unpack the binaries and shared libraries into your home directory, and launch them without recompiling.

For a quick test, you can also do (in bash at least):

LD_LIBRARY_PATH=/home/user/.usr/lib ./xxx.out

which has the advantage of not changing your library path for everything else.


Should it be LIBRARY_PATH instead of LD_LIBRARY_PATH. gcc checks for LIBRARY_PATH which can be seen with -v option


Examples related to gcc

Can't compile C program on a Mac after upgrade to Mojave Compiling an application for use in highly radioactive environments Make Error 127 when running trying to compile code How to Install gcc 5.3 with yum on CentOS 7.2? How does one set up the Visual Studio Code compiler/debugger to GCC? How do I set up CLion to compile and run? CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found How to printf a 64-bit integer as hex? Differences between arm64 and aarch64 Fatal error: iostream: No such file or directory in compiling C program using GCC

Examples related to linker

C compile : collect2: error: ld returned 1 exit status How to fix symbol lookup error: undefined symbol errors in a cluster environment gcc: undefined reference to libpthread.so.0: error adding symbols: DSO missing from command line Compilation fails with "relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object" Multiple definition of ... linker error C Linking Error: undefined reference to 'main' ld cannot find -l<library> ldconfig error: is not a symbolic link Why am I getting "undefined reference to sqrt" error even though I include math.h header?

Examples related to shared-libraries

How to install the Six module in Python2.7 "relocation R_X86_64_32S against " linking Error Compilation fails with "relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object" How to create a shared library with cmake? error while loading shared libraries: libncurses.so.5: Build .so file from .c file using gcc command line What's the difference between .so, .la and .a library files? ld cannot find -l<library> How do you find what version of libstdc++ library is installed on your linux machine? What are .a and .so files?