I want to build an executable foobar
on Linux, that depends on shared libraries such as libfoobar.so
. So I do
gcc foobar.o -Xlinker -rpath
relative path from executable to library-lfoobar -o foobar
I have to provide the path to the library so that the linker does not complain, even though the library is not needed at link time.
Then the executable and libraries get installed to system directories (which are unrelated to the initial locations and to each other).
After that, I can't run it, because the path to the library, used to build is no longer valid, and the library is not found.
What is the best way to setup linker options so that they work both at link time and at runtime?
I cannot use absolute paths, they are a bad idea in general to use during build, and also not possible during runtime, as different users will have different installation roots. Only the relative paths at build time and runtime, are guaranteed to be always the same.