# CMake 错误：安装 RPATH 文件 /usr/local/lib/libfoo.so 在构建树中未找到。

- **ID:** `cmake/install-rpath-not-found`
- **领域:** cmake
- **类别:** install_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

目标的 INSTALL_RPATH 属性引用了构建目录中不存在的文件路径，通常是由于缺少依赖构建或路径变量展开不正确。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.10 | active | — | — |
| CMake 3.14 | active | — | — |
| CMake 3.20 | active | — | — |

## 解决方案

1. ```
   更正 INSTALL_RPATH 目标属性以使用有效的构建树路径，例如 `set_target_properties(mylib PROPERTIES INSTALL_RPATH "${CMAKE_BINARY_DIR}/lib")`。
   ```
2. ```
   通过添加依赖确保引用的库在目标之前构建：`add_dependencies(mylib libfoo)`。
   ```
3. ```
   在 INSTALL_RPATH 中使用生成器表达式有条件地包含路径（仅当存在时），例如 `$<TARGET_FILE_DIR:libfoo>`。
   ```

## 无效尝试

- **Setting CMAKE_PREFIX_PATH to include the missing library directory** — 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. (75% 失败率)
- **Creating a symbolic link in the build tree to the missing library** — Manually creating a symlink in the build directory may bypass the error but will break on clean builds or different systems. (90% 失败率)
- **Setting CMAKE_SKIP_RPATH to ON to skip RPATH installation** — Disabling RPATH entirely with CMAKE_SKIP_RPATH removes the error but causes runtime linking failures on systems without the library in standard paths. (70% 失败率)
