Linking to shared libraries in non-standard locations in Linux
I wrote a CGI app in C++ which compiled and linked with no problem. When I ran it, thud:
error while loading shared libraries… can not open shared object file… no such file or directory
The problem is that programs don’t hard-code paths to shared libs. This works great most of the time because the app simply searches the standard lib paths and find it. However, if the shared lib is elsewhere, game over – runtime link error.
Since it was only this program that needed to link to the library, I used a linker option that adds a directory to the runtime library search path: the -rpath flag. Since I was using g++ through a makefile, I passed the flag to the linker via -Wl, which – you guessed it – passed flags to the linker! The full LDFLAGS were: -L/path/to/shared/lib -l[libname] -Wl,-rpath,/path/to/shared/lib. The commas are a little weird and through me off at first, but they are correct. You can read about the syntax here (for -Wl) and here (for -rpath) and here (for the combination).
Exactly what I wanted: the program can find the library at runtime in its non-standard location!