CMake 错误:安装 RPATH 文件 /usr/local/lib/libfoo.so 在构建树中未找到。
CMake Error: install RPATH file /usr/local/lib/libfoo.so not found in build tree.
ID: cmake/install-rpath-not-found
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| CMake 3.10 | active | — | — | — |
| CMake 3.14 | active | — | — | — |
| CMake 3.20 | active | — | — | — |
根因分析
目标的 INSTALL_RPATH 属性引用了构建目录中不存在的文件路径,通常是由于缺少依赖构建或路径变量展开不正确。
English
The INSTALL_RPATH property of a target references a file path that does not exist in the build directory, often due to a missing dependency build or incorrect path variable expansion.
官方文档
https://cmake.org/cmake/help/latest/prop_tgt/INSTALL_RPATH.html解决方案
-
更正 INSTALL_RPATH 目标属性以使用有效的构建树路径,例如 `set_target_properties(mylib PROPERTIES INSTALL_RPATH "${CMAKE_BINARY_DIR}/lib")`。 -
通过添加依赖确保引用的库在目标之前构建:`add_dependencies(mylib libfoo)`。
-
在 INSTALL_RPATH 中使用生成器表达式有条件地包含路径(仅当存在时),例如 `$<TARGET_FILE_DIR:libfoo>`。
无效尝试
常见但无效的做法:
-
Setting CMAKE_PREFIX_PATH to include the missing library directory
75% 失败
Adding the missing library path to CMAKE_PREFIX_PATH does not affect INSTALL_RPATH, which is a target property, not a find_package search path.
-
Creating a symbolic link in the build tree to the missing library
90% 失败
Manually creating a symlink in the build directory may bypass the error but will break on clean builds or different systems.
-
Setting CMAKE_SKIP_RPATH to ON to skip RPATH installation
70% 失败
Disabling RPATH entirely with CMAKE_SKIP_RPATH removes the error but causes runtime linking failures on systems without the library in standard paths.