m_deployment
m_deployment

Reputation: 45

How can I change the library path of an executable after it has been built/installed?

Let's say I build myTest with cmake. myTest uses /opt/path1/lib/lib.so at compile and link time. After running it a few times I decide that I want myTest to now use /opt/path2/lib.so (same lib name, same interfaces, just different path).

This might be cause I want to temporarily test changes to lib.so without affecting others that might be using it. I also may not have the source to myTest but know that it uses lib.so.

If I used a Makefile and used regular gnu/g++ make I can make this happen by setting LD_LIBRARY_PATH in the local folder. CMake ignores LD_LIB_PATH - so how do I make this happen?

Upvotes: 1

Views: 2278

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65870

For find a library at runtime, ldd uses (among other things) RPATH directories, embedded into the executable.

By default, when build the executable/library, CMake adds to RPATH directories, where linked libraries are located.

E.g., when link with library /opt/path1/lib/lib.so, CMake adds directory /opt/path1/lib to RPATH. So ldd always finds lib.so library as /opt/path1/lib/lib.so.

For tell CMake to not set RPATH, set CMAKE_SKIP_RPATH variable:

set(CMAKE_SKIP_RPATH TRUE)

After that, ldd will search lib.so in directory, listed in LD_LIBRARY_PATH environment variable.

Upvotes: 2

Related Questions