为什么设计CMake以便在安装时删除运行时路径 [英] Why is CMake designed so that it removes runtime path when installing
问题描述
我自己构建了我的共享库(我使用lib计算斐波纳契数),并希望在我的另一个由 CMake构建的c ++项目中使用它
I built my shared library(I use a lib calculating the fibonacci number for example) myself and want to use it in my another c++ project built by CMake
假设共享库和标题位于 / path / to / my / lib
,共享库 libfib.so
位于 / path / to / my / lib / lib
中,标题 fib.h
位于 / path / to / my / lib / include
,我自己的项目位于 / path / to / my / project
Let's say the shared library and headers located in /path/to/my/lib
, the shared library libfib.so
is in /path/to/my/lib/lib
and the header fib.h
is in /path/to/my/lib/include
and my own project located in /path/to/my/project
这是我原来的 CMakeLists.txt
:
cmake_minimum_required(VERSION 3.2)
project(learn-lib)
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
set(FIB_INCLUDE "${FIB_PREFIX}/include")
set(FIB_LIB "${FIB_PREFIX}/lib")
set(EXE mybin)
include_directories(${FIB_INCLUDE})
link_directories(${FIB_LIB})
add_executable(${EXE} main.cpp)
target_link_libraries(${EXE} fib)
install(TARGETS ${EXE} RUNTIME DESTINATION bin)
我使用这个脚本来构建和安装我的项目:
And I use this script to build and install my project:
mkdir -p build_dir
cd build_dir
cmake -DFIB_PREFIX=/path/to/my/lib \
-DCMAKE_INSTALL_PREFIX=/path/to/my/project \
..
make
make install
cd ..
现在,在运行安装脚本后,我得到了两个可执行文件,一个在 build_dir
中,一个在安装中location path / to / my / project / bin
,在 build_dir
中运行程序时,一切都很好,但是当运行已安装的程序,我得到:
Now, after running the install script, I got two executables, one in build_dir
, one in the install location path/to/my/project/bin
, when running the program in build_dir
, everything is fine, but when running the installed program, I get:
./ bin / mybin:加载共享库时出错:libfib.so:无法打开共享对象文件:没有这样的文件或目录
./bin/mybin: error while loading shared libraries: libfib.so: cannot open shared object file: No such file or directory
在google和stackoverflow上进行一些搜索之后,我知道它似乎 CMake
删除了构建时绑定到可执行文件的运行时搜索路径。我现在知道两种方法来解决它:
After some searching on google and stackoverflow, I knew it seems that CMake
removed the runtime search path that is tied to the executable when building. I now know two ways to get it around:
- 添加库路径
libfib.so
定位到环境变量LD_LIBRARY_PATH
- 添加
set_target_properties($ {EXE} PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)
进入我的CMakeLists.txt
- Add the library path where
libfib.so
locates to the environment variableLD_LIBRARY_PATH
- Add
set_target_properties(${EXE} PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)
into myCMakeLists.txt
所以,我的问题是:
- 为什么
CMake
这样设计?安装时,为什么它会从可执行文件中删除运行时路径,而不是仅仅将构建的可执行文件复制到安装目标或保留已安装程序的链接路径? - 哪种方式是最佳实践(或者有最好的做法)来消除这个问题吗?设置环境或将
set_target_properties(...)
添加到CMakeLists.txt
?
- Why is
CMake
designed so? When installing, why would it remove runtime path from executables instead of just copying the built executables to the install destination or whatever keeping the link path for the installed program? - Which way is the best practice(or is there a best practice) to eliminate this problem? To set the environment or to add
set_target_properties(...)
intoCMakeLists.txt
?
推荐答案
您可能需要查看 CMake的RPATH处理设置
此报价特别与您的困境相关:
This quote in particular seems relevant to your predicament:
默认情况下,如果不更改任何与RPATH相关的设置,CMake会将可执行文件和共享库与完整的RPATH链接到构建中的所有已使用的库树。安装时,它将清除这些目标的RPATH,以便安装空RPATH。
By default if you don't change any RPATH related settings, CMake will link the executables and shared libraries with full RPATH to all used libraries in the build tree. When installing, it will clear the RPATH of these targets so they are installed with an empty RPATH.
您可以设置RPATH使用 CMAKE_INSTALL_RPATH
变量为已安装的二进制文件设置,例如:
You can set the RPATH that is set for installed binaries using the CMAKE_INSTALL_RPATH
variable, for example:
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
您还可以在安装期间禁用RPATH剥离:
and you can also disable the RPATH stripping during installation:
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
这篇关于为什么设计CMake以便在安装时删除运行时路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!