CMake 错误:安装 RPATH 文件 /usr/local/lib/libfoo.so 在构建树中未找到。RPATH 设置可能不正确。
CMake Error: install RPATH file /usr/local/lib/libfoo.so not found in build tree. RPATH settings may be incorrect.
ID: cmake/install-rpath-missing
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| CMake 3.0+ | active | — | — | — |
| Linux x86_64 | active | — | — | — |
根因分析
目标的 INSTALL_RPATH 属性引用了构建树中不存在的库路径,通常是因为拼写错误或使用了主机系统的绝对路径而非构建相对路径。
English
The INSTALL_RPATH property of a target references a library path that does not exist in the build tree, often due to a typo or using an absolute path from the host system instead of a build-relative path.
官方文档
https://cmake.org/cmake/help/latest/prop_tgt/INSTALL_RPATH.html解决方案
-
更正 INSTALL_RPATH 属性以使用构建相对路径。例如,如果库在 ${CMAKE_BINARY_DIR}/lib 中,设置:set_target_properties(mytarget PROPERTIES INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")。 -
使用生成器表达式条件设置 RPATH:set_target_properties(mytarget PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")。这使用了已安装二进制文件的位置。
-
移除错误的 INSTALL_RPATH 并依赖 CMake 的默认 RPATH 处理:直接删除 set_target_properties 行并重新配置。
无效尝试
常见但无效的做法:
-
Set CMAKE_SKIP_RPATH to ON to skip RPATH processing entirely
70% 失败
Skips the check but may cause runtime failures because the installed binary cannot find its shared libraries.
-
Manually create the missing file as a symlink
85% 失败
The build tree is ephemeral; symlinks break after clean build. Also, the error is about the path not existing, not the file.
-
Change INSTALL_RPATH to use $ORIGIN (relative path)
50% 失败
While this is a good practice, simply changing to $ORIGIN without correcting the actual path still fails if the path is malformed.