How can LD_LIBRARY_PATH be changed within CMake?

Daniel Saad picture Daniel Saad · May 21, 2015 · Viewed 27.5k times · Source

I have a local shared library which is not in $LD_LIBRARY_PATH. I want to run my executable, but since it cannot find the shared library in the system folders, it outputs "error while loading shared libraries".

A possible fix for this is to export a new LD_LIBRARY_PATH containing the local library folder.

How can I export automatically this environment variable within CMake?

Answer

hank picture hank · May 22, 2015

You can set the runtime shared library search path using the -rpath linker option:

SET(CMAKE_EXE_LINKER_FLAGS 
          "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath -Wl,/usr/local/lib")