# CMake 错误：安装 RPATH 文件 /usr/local/lib/libfoo.so 在构建树中未找到。RPATH 设置可能不正确。

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

## 根因

目标的 INSTALL_RPATH 属性引用了构建树中不存在的库路径，通常是因为拼写错误或使用了主机系统的绝对路径而非构建相对路径。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.0+ | active | — | — |
| Linux x86_64 | active | — | — |

## 解决方案

1. ```
   更正 INSTALL_RPATH 属性以使用构建相对路径。例如，如果库在 ${CMAKE_BINARY_DIR}/lib 中，设置：set_target_properties(mytarget PROPERTIES INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")。
   ```
2. ```
   使用生成器表达式条件设置 RPATH：set_target_properties(mytarget PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")。这使用了已安装二进制文件的位置。
   ```
3. ```
   移除错误的 INSTALL_RPATH 并依赖 CMake 的默认 RPATH 处理：直接删除 set_target_properties 行并重新配置。
   ```

## 无效尝试

- **Set CMAKE_SKIP_RPATH to ON to skip RPATH processing entirely** — Skips the check but may cause runtime failures because the installed binary cannot find its shared libraries. (70% 失败率)
- **Manually create the missing file as a symlink** — The build tree is ephemeral; symlinks break after clean build. Also, the error is about the path not existing, not the file. (85% 失败率)
- **Change INSTALL_RPATH to use $ORIGIN (relative path)** — While this is a good practice, simply changing to $ORIGIN without correcting the actual path still fails if the path is malformed. (50% 失败率)
