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?
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")
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
When you use this solution:
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
Remembter to set the RPATH before defining the targets in your CMake-File. So this instruction have to be before add_executable() or add_library() is called, otherwise it has no effect.
Related
I have an external library. That I am bringing into a CMake build using an imported library target. The build is baking in relative path to the shared library with respect to the CMAKE_BINARY_DIR.
I have something like this:
add_library(libstring UNKNOWN IMPORTED)
set_target_properties(libstring PROPERTIES
IMPORTED_LOCATION "${CMAKE_BINARY_DIR}/external/libstring.so"
)
add_executable(my_exe "${CMAKE_CURRENT_BINARY_DIR}/my_exe.cpp")
target_link_libraries(my_exe PRIVATE libstring)
Then ldd my_exe returns
external/libstring.so => not found
If instead of linking to the imported target I link directly to the library giving the absolute file path it works fine.
target_link_libraries(my_exe PRIVATE "${CMAKE_BINARY_DIR}/external/libstring.so")
Then ldd returns
libstring.so => /<bin-dir>/external/libstring.so (0x00007fce27537000)
In both cases rpath is set to /<bin-dir>/external.
When linking to an imported target how to make CMake bake in just the name of the library in the executable?
Note, that when the imported library path is outside of the binary tree, then the absolute path is baked in the executable.
It turned out that it is not CMake causing this problem, but this particular library.
With Unix Makefiles, CMake passes to the linker relative paths with respect to the CMAKE_CURRENT_BINARY_DIR.
../external/libstring.so
Or with Ninja it passes relative to CMAKE_BINARY_DIR.
external/libstring.so
The linker then bakes-in this whole relative path. Not the name of the library only.
Other libs are not affected even when passed to the linker like that. I don't know what is different with this library.
The solution is to set IMPORTED_NO_SONAME property for the library:
set_property(TARGET libstring PROPERTY IMPORTED_NO_SONAME TRUE)
Documentation for this property says:
Set this property to true for an imported shared library file that has
no soname field. CMake may adjust generated link commands for some
platforms to prevent the linker from using the path to the library in
place of its missing soname.
I am trying to get rid of setting LD_LIBRARY_PATH everytime time I run my program. After adding in the library and targeting my executable to the library, when I run it tells me it can not open shared object library, no such file or directory.
In my CMakeLists.txt I have:
add_library(heart SHARED ${HEART_FILES})
add_executable(run ${RUN_FILES})
target_link_libraries(run heart)
set(CMAKE_SKIP_BUILD_PATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "~/person/target/usr/local/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
I set an absolute link to my library folder to test out whether this would create an rpath to my library and it seems like there isn't. I have checked and made sure that the shared library is indeed in lib. libheart.so is the file that is being linked. What else am I missing?
It is because you build heart and run from the same cmake project:
CMAKE_INSTALL_RPATH_USE_LINK_PATH is an interesting and very useful option. When building a target with RPATH, CMake determines the RPATH by using the directories of all libraries to which this target links. Some of these libraries may be located in the same build tree, e.g. libbar.so, these directories are also added to the RPATH.
If this option is enabled, all these directories except those which are also in the build tree will be added to the install RPATH automatically. The only directories which may then still be missing from the RPATH are the directories where the libraries from the same project (i.e. libbar.so) are installed to. If the install directory for the libraries is not one of the systems default library directories, you have to add this directory yourself to the install RPATH by setting CMAKE_INSTALL_RPATH accordingly
You can try this:
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
More documentation here cmake rpath handling
EDIT:
Only this should work:
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
add_library(heart SHARED ${HEART_FILES})
add_executable(run ${RUN_FILES})
target_link_libraries(run heart)
install(
TARGETS heart run
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
)
Clean your build directory and then:
cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/home/person/target/usr/local ..
make install
At the end of the g++ line Linking CXX executable run you should see like -Wl,-rpath,/home/person/target/usr/local/lib
If you want a fully relocatable package:
set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
PS: are you sur that it is libheart.so that is not found ?
In your CMake file, set the RPATH before defining the targets. The CMAKE_INSTALL_RPATH must be defined before calling add_executable(), otherwise it has no effect.
I had a similar issue as the original post. I created executables which linked to external shared libraries. This approach compiled and executed fine from the build directory. However, the executable that was installed to a separate directory could not find a shared library at runtime:
error while loading shared libraries: libxxxx.so.1: cannot open shared object file: No such file or directory
To solve, I
1) upgraded to CMake 3.17
2) used Craig Scott's recommended:
set(CMAKE_INSTALL_RPATH $ORIGIN)
as explained in his talk
3) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) as directly mentioned to solve this error in the second common question in Kitware's documention
4) Put all this before adding the targets as mentioned in this post
5) Used the "$ORIGIN/../lib" syntax instead of Craig's Scott's mentioned $ORIGIN as mentioned by #explo91
In summary, and to my suprise, only the "$ORIGIN/../lib" before the target definition was necessary from above (I tested the other combinations which did not fix the cannot open shared object file runtime issue).
Anyway the solution I finally applied, which may be of better, fine-grained CMake style or at least may be helpful to others on their RPATH journey is:
set_target_properties(target_defined_above PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")
I am cross-compiling using CMake.
In my CMakeLists.txt (used for both compile and cross compile):
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
find_package(foo REQUIRED)
add_library(mylib SHARED ${SRCS})
target_link_libraries(mylib ${FOO_LIBRARIES)
In my toolchain.cmake:
set(CMAKE_CXX_FLAGS "... --sysroot=/path/to/sysroot/ ... ")
set(CMAKE_CXX_LINK_FLAGS "... --sysroot=/path/to/sysroot/ ... )
...
set(CMAKE_FIND_ROOT_PATH /path/to/sysroot)
Consider foo is located to /path/to/sysroot/usr/local/lib/foo.so, when i cross-compile the runtime path for mylib is /path/to/sysroot/usr/local/lib
I want that the runtime path is /usr/local/lib to reflect my target filesystem.
How can i do this without define a hard-coded CMAKE_INSTALL_RPATH variable in my CMakelists.txt ?
EDIT: I used /usr/local/lib for the example but foo lib are located to a specific folder that is not a part of the system dirs: /path/to/sysroot/usr/local/share/mypackage/lib/foo.so
Check out the wiki on CMake RPATH handling.
By default, CMake compiles your executable with an RPATH pointing to the host-system library location (/crosssdk/sysroot/usr/lib/) and then (I believe) when installing (ie. make install) it edits the RPATH in the executable to replace it with the appropriate target RPATH (/usr/lib or wherever you've got it). I think the idea is that then you can make changes to the shared lib and execute the output on your host system without having to install both the shared lib and executable every time.
In my case, my host is x86 and target is ARM, so I tell CMake to set the build RPATH the same as the install RPATH:
set_target_properties(mytarget PROPERTIES BUILD_WITH_INSTALL_RPATH TRUE)
I am not sure which cross compile toolchain you are using.
You need to specify the C/CXX compilers, Linker etc.
Along with that some of the important variables are CMAKE_FIND_ROOT_PATH_MODE_LIBRARY and CMAKE_FIND_ROOT_PATH_MODE_INCLUDE. If you set them to "ONLY", when you make calls to FindXXX(), the search happens only in the target root file system directory but not the build machine.
In my case I don't have to specify the sysroot as the cross compiler already knows that it's cross compiling and it also knows the location of the target root file system.
With this toolchain file, I just compile the sources without any additional flags, load the executable on the target and it runs fine picking up the *.so file directly from the right path.
Give it a try with this and let me know how it goes.
Here is my toolchain file:
set(ELDK_DIR /opt/eldk/ppc-v42-1)
set (CMAKE_C_COMPILER ${ELDK_DIR}/usr/bin/ppc_6xx-gcc)
set (CMAKE_CXX_COMPILER ${ELDK_DIR}/usr/bin/ppc_6xx-g++)
set (CMAKE_LINKER ${ELDK_DIR}/usr/bin/ppc_6xx-ld CACHE STRING "Set the cross-compiler tool LD" FORCE)
set (CMAKE_AR ${ELDK_DIR}/usr/bin/ppc_6xx-ar CACHE STRING "Set the cross-compiler tool AR" FORCE)
set (CMAKE_NM ${ELDK_DIR}/usr/bin/ppc_6xx-nm CACHE STRING "Set the cross-compiler tool NM" FORCE)
set (CMAKE_OBJCOPY ${ELDK_DIR}/usr/bin/ppc_6xx-objcopy CACHE STRING "Set the cross-compiler tool OBJCOPY" FORCE)
set (CMAKE_OBJDUMP ${ELDK_DIR}/usr/bin/ppc_6xx-objdump CACHE STRING "Set the cross-compiler tool OBJDUMP" FORCE)
set (CMAKE_RANLIB ${ELDK_DIR}/usr/bin/ppc_6xx-ranlib CACHE STRING "Set the cross-compiler tool RANLIB" FORCE)
set (CMAKE_STRIP ${ELDK_DIR}/usr/bin/ppc_6xx-strip CACHE STRING "Set the cross-compiler tool RANLIB" FORCE)
# Target environment
set (CMAKE_FIND_ROOT_PATH ${ELDK_DIR}/ppc_6xx)
# Don't search for programs in the host environment
set (CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Search for the libraries and headers in the target environment
set (CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set (CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
all you need is:
set(CMAKE_BUILD_RPATH "/my/libs/location")
specifying runtime path (RPATH) entries to add to binaries linked in the build tree (for platforms that support it). The entries will not be used for binaries in the install tree. See also the CMAKE_INSTALL_RPATH variable.
I am trying to run cmake to generate makefiles. In the minimum working example, I have three files and 1 build directory.
File 1 is CMakeLists.txt, containing exactly:
add_library (MathFunctions SHARED mysqrt.cxx)
install (TARGETS MathFunctions LIBRARY DESTINATION lib)
File 2 is MathFunctions.h containing the function prototype, function relates to mysqrt.cxx.
File 3 is mysqrt.cxx containing include statement and a function definition.
When I create a build sub-directory and run "cmake ..", I am getting
CMake Error at CMakeLists.txt:2 (install):
install Library TARGETS given no DESTINATION!
Isn't my add_library, then install statement grammar correct? If I remove both SHARED and LIBRARY, cmake builds without errors.
Thanks for your help.
The problem is likely down to you running this on what CMake calls a "DLL platform" and how CMake classifies a shared library on such a platform.
From the docs for install:
For DLL platforms the DLL part of a shared library is treated as a RUNTIME target and the corresponding import library is treated as an ARCHIVE target. All Windows-based systems including Cygwin are DLL platforms.
So, try changing your command to something like:
install (TARGETS MathFunctions
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin)
I'm trying to create a ndk library using android-cmake.
My target here is a shared library and build fails because of option
-Wl,--no-undefined
As the targets refers symbols from another library.
Is there a way to the missing library to the command
add_library(foo SHARED ${sources})
I was able to build by changing cmake variable
set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -L<path to library> -lfoo")