cmake 设置RPATH

cmake 设置RPATH

RPATH

就是动态链接器查找动态库的搜索路径。在支持RPATH的平台上可以通过以下命令查看:

readelf --dynamic path_to_your_exe_or_shared_lib

一般来说就在前几行。

note: cmake的build和install阶段一般需要不同的RPATH。

一、RPATH相关variables和properties

variables

下列中用于初始化target proptertiesvariables会统一在propterties讲解。

  1. CMAKE_SKIP_RPATH

    If this is set to TRUE, then the rpath information is not added to compiled executables. The default is to add rpath information if the platform supports it.

    简单来说就是禁止cmake设置RPATH,但这个变量我认为最主要的作用有以下两点:

    • 如上面引用的cmake文档原文,就是告知平台是否支持设置RPATH.

    • 简简单单直接禁止所有target的RPATH设置.

  2. CMAKE_BUILD_RPATH

    初始化所有target的BUILD_RPATH

  3. CMAKE_BUILD_RPATH_USE_ORIGIN

    初始化所有target的BUILD_RPATH_USE_ORIGIN

  4. CMAKE_BUILD_WITH_INSTALL_RPATH

    初始化所有target的BUILD_WITH_INSTALL_RPATH

  5. CMAKE_INSTALL_REMOVE_ENVIRONMENT_RPATH

    初始化所有target的INSTALL_REMOVE_ENVIRONMENT_RPATH

  6. CMAKE_INSTALL_RPATH

    初始化所有target的INSTALL_RPATH

  7. CMAKE_INSTALL_RPATH_USE_LINK_PATH

    初始化所有target的INSTALL_RPATH_USE_LINK_PATH

  8. CMAKE_MACOSX_RPATH

    初始化所有target的MACOSX_RPATH

  9. CMAKE_NO_BUILTIN_CHRPATH

    Do not use the builtin binary editor to fix runtime library search paths on installation.
    When an ELF or XCOFF binary needs to have a different runtime library search path after installation than it does in the build tree, CMake uses a builtin editor to change the runtime search path in the installed copy. If this variable is set to true then CMake will relink the binary before installation instead of using its builtin editor.

    在install时cmake会使用自己内建的工具更改RPATH。但当这个variable设置为TRUE时,会采用重新链接的方式来设置RPATH。这个需求一般只有两种情况:

    • install rpath的字符串长度比build rpath的字符串长度大。比如build rpath = "~/source/xxx",而install rpath = "/opt/third_package/local/xxx"

    • 你的编译套件中的linker比较特殊(具体特殊在哪,我也没经历过)。

    总的来说是一个相当没用的variable

  10. CMAKE_SKIP_BUILD_RPATH

    初始化所有target的SKIP_BUILD_RPATH

  11. CMAKE_SKIP_INSTALL_RPATH

    初始化所有target的SKIP_INSTALL_RPATH

propterties

  1. BUILD_RPATH

    A semicolon-separated list specifying runtime path (RPATH) entries to add to binaries linked in the build tree (for platforms that support it). By default, CMake sets the runtime path of binaries in the build tree to contain search paths it knows are needed to find the shared libraries they link. Projects may set BUILD_RPATH to specify additional search paths.

    在开头就已经说明过了RPATH。这里的BUILD_RPATH就是指构建产物的RPATH,即执行cmake --build . 后的产物的RPATH。cmake会自动生成BUILD_RPATH

  2. BUILD_RPATH_USE_ORIGIN

    BUILD_RPATH中的绝对路径(默认是绝对路径)改为相对路径。仅在支持$ORIGIN的平台上生效。

    $ORIGIN其实就是exe或者shared_lib本身自己,这个就是告诉动态链接器,这个文件的RPATH使用的是相对路径。

    ├── CMakeLists.txt
    ├── main.cpp
    └── message
    	├── CMakeLists.txt
    	├── include
    	│   └── message.hpp
    	└── message.cpp
    

    上面是一个库的文件树,message库是shared library,将BUILD_RPATH_USE_ORIGIN设置为TRUE后,查看(开头有cmd)Main的RPARH为:

     0x000000000000001d (RUNPATH) Library runpath: [$ORIGIN/../message/Debug]
    

    Normally the build RPATH of a binary contains absolute paths to the directory of each shared library it links to. The RPATH entries for directories contained within the build tree can be made relative to enable relocatable builds and to help achieve reproducible builds by omitting the build directory from the build environment.

    文档说是在省去build目录的构建环境中,实现可重现构建。

  3. SKIP_BUILD_RPATH

    不自动生成BUILD_RPATH

  4. BUILD_WITH_INSTALL_RPATH

    BUILD_RPATH设置为INSTALL_RPATH。注意即使设置了SKIP_BUILD_RPATHBUILD_WITH_INSTALL_RPATH也会生成BUILD_RPATH

  5. INSTALL_RPATH

    执行cmake --install . --prefix path/to/xxxx后在path/to/xxxx下的RPATH。默认为空。

  6. INSTALL_REMOVE_ENVIRONMENT_RPATH

    移除工具链相关的RPATH

  7. INSTALL_RPATH_USE_LINK_PATH

    将项目外部的动态库的目录添加进INSTALL_RPATH

  8. MACOSX_RPATH

    不用MacOS

posted @ 2026-07-29 06:29  杨忆州  阅读(14)  评论(0)    收藏  举报