我想链接第三方 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)
However, what I get is my game_executable
depends on the .../game/bin/libLibrary.so
, not on the ./libLibrary.so
that is in the folder with game_executable
, making这完全不便携!
如何使链接路径相对而不是绝对?
原文由 The Saber Cat 发布,翻译遵循 CC BY-SA 4.0 许可协议
从 文档 中:
这是您所看到的行为。
但是,有多种方法可以更改它以匹配您需要的行为。
上述链接文档中的一些示例:
使用上述内容,您可能需要设置
CMAKE_INSTALL_RPATH
然后分发 安装 的二进制文件。如果您想从构建树中的二进制文件分发,也可以绕过 CMake 的 rpath 处理并使用链接器标志直接修改 rpath: