7

I've done this before a couple of times, but somehow I'm stuck this time. I have an executable "myapp" and a own shared library "mylib". In my cmakelists I have the following:

ADD_LIBRARY(mylib SHARED ${SOURCES_LIB})
INSTALL(TARGETS mylib DESTINATION .)
ADD_EXECUTABLE(myapp ${SOURCES_APP})
TARGET_LINK_LIBRARIES(myapp ${QT_LIBRARIES} mylib)
INSTALL(TARGETS myapp DESTINATION .)

Everything compiles and links correctly, but when I start myapp, I get the following error:

error while loading shared libraries: libmylib.so: cannot open shared object file: No such file or directory

The lib and the executable are present in the install directory. When I make my library static by changing the first line of the above cmakelists to:

ADD_LIBRARY(mylib STATIC ${SOURCES_LIB})

then everything works 100%.

Does anyone know what I'm doing wrong?

3 Answers 3

6

During the installation of your library and executable, the runtime paths to find the library are stripped from the executable. Therefore your library has to reside in the runtime library search path. For example under Linux, try to set LD_LIBRARY_PATH to the directory that contains the installed library when starting your executable.

1
  • 10
    In cmake you can set: SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/libfolder") where libfolder is the directory where your shared libraries are installed. This only works for Unix systems. I'll try to find something for Windows too. Jan 16, 2012 at 19:36
2

This is a very common question about "make install". Actually, there are 3 ways to link a library to your executable file. First, you may use -l -L flags in simple cases. As Benjamin said you may use LD_LIBRARY_PATH and write something like: export LD_LIBRARY_PATH=/usr/local/my_lib. In fact this is not a good way. It's much better to use RPATH. There is a very useful doc page about it. Check it out. Well if you write something like this in your top level CMakeLists.txt, it will solve the problem:

SET(CMAKE_SKIP_BUILD_RPATH  FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) 
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib64")
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib64")
1
  • no it's not. it's a very specific question about cmake and ld paths. also your answer is neither well formatted nor describing why it works. please don't just link to answers elsewhere but also summarize the relevant parts.
    – nonchip
    Jul 31, 2017 at 10:55
0

Add the path of the directory containing the library to the LD_LIBRARY_PATH environment variable, by appanding a new path:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/the/library/

You can check the library is correctly found with the 'ldd' tool:

lld ./executable

If the library is not stated as "not found" it is OK and your executable will be executed properly.

Add the 'export' command to your bashrc to properly set the LD_LIBRARY_PATH variable after each system reboot, otherwise you will have to execute again the 'export' command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.