我想链接第三方libLibrary.so
并将其与我的程序一起分发。如果用户解压缩我的存档,他将获得以下文件夹结构:
game
libLibrary.so
game_executable
game_executable
取决于./libLibrary.so
。
我的项目结构:
game
bin
libLibrary.so
lib
Library.h
src
game_executable.cpp
CMakeLists.txt
我的CMakeLists.txt
:
cmake_minimum_required(VERSION 3.7)
project(game)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(SOURCE_FILES src/game_executable.cpp)
include_directories(${CMAKE_SOURCE_DIR}/lib)
add_executable(game ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${CMAKE_BINARY_DIR}/libLibrary.so)
但是,我得到的是我的game_executable
依赖于.../game/bin/libLibrary.so
,而不是依赖于./libLibrary.so
文件夹中的game_executable
,这使得它完全不可移植!
如何使链接路径是相对的而不是绝对的?
从文档中:
默认情况下,如果您不更改任何与RPATH相关的设置,则CMake会将具有完整RPATH的可执行文件和共享库链接到构建树中所有使用的库。
这是您看到的行为。
但是,有多种方法可以更改此设置以匹配所需的行为。
以上链接文档中的一些示例:
# use, i.e. don't skip the full RPATH for the build tree
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
# when building, don't use the install RPATH already
# (but later on when installing)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
# the RPATH to be used when installing
SET(CMAKE_INSTALL_RPATH "")
# don't add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
使用上述方法,您可能需要设置CMAKE_INSTALL_RPATH
然后分发已安装的二进制文件。
如果要从构建树中的二进制文件分发,则还可以绕过CMake的rpath处理并使用链接器标志直接修改rpath:
set_target_properties(game PROPERTIES LINK_FLAGS "-Wl,-rpath,./")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句