Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can LD_LIBRARY_PATH be changed within CMake?

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?

like image 389
Daniel Saad Avatar asked May 21 '15 17:05

Daniel Saad


People also ask

How do I set LD_LIBRARY_PATH?

In your terminal, type the following sudo ldconfig and press enter on your keyboard. Close all your open terminals that you were using then open a new terminal session and run echo $LD_LIBRARY_PATH If you see the path you added is echoed back, you did it right.

What is LD_LIBRARY_PATH variable?

LD_LIBRARY_PATH is an environmental variable used in Linux/UNIX Systems. It is used to tell dynamic link loaders where to look for shared libraries for specific applications. It is useful until you don't mess with it.

What does LD_LIBRARY_PATH contain?

The LD_LIBRARY_PATH environment variable tells Linux applications, such as the JVM, where to find shared libraries when they are located in a different directory from the directory that is specified in the header section of the program.

What does LD_LIBRARY_PATH stand for?

LD_LIBRARY_PATH - stands for LOAD LIBRARY PATH or some times called as LOADER LIBRARY PATH.


2 Answers

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") 
like image 186
hank Avatar answered Sep 21 '22 11:09

hank


If your shared lib is not build in the same CMake project of your executable, you can use the CMake rpath handling like this:

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) 

When you will run make install, CMake will automatically set the runtime path of your executable to your shared library.

If your shared library is built in the same CMake project, use this:

set(CMAKE_INSTALL_RPATH "/usr/local/lib") set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) 

In this case you must add yourself the directory where your shared library will be installed to the runtime path.

For more information, you can read CMake rpath handling

like image 21
explo91 Avatar answered Sep 21 '22 11:09

explo91