# CMake Error: install RPATH file /usr/local/lib/libfoo.so not found in build tree.

- **ID:** `cmake/install-rpath-not-found`
- **Domain:** cmake
- **Category:** install_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| CMake 3.10 | active | — | — |
| CMake 3.14 | active | — | — |
| CMake 3.20 | active | — | — |

## Workarounds

1. **Correct the INSTALL_RPATH target property to use a valid build-tree path, e.g., `set_target_properties(mylib PROPERTIES INSTALL_RPATH "${CMAKE_BINARY_DIR}/lib")`.** (90% success)
   ```
   Correct the INSTALL_RPATH target property to use a valid build-tree path, e.g., `set_target_properties(mylib PROPERTIES INSTALL_RPATH "${CMAKE_BINARY_DIR}/lib")`.
   ```
2. **Ensure the referenced library is built before the target by adding a dependency: `add_dependencies(mylib libfoo)`.** (85% success)
   ```
   Ensure the referenced library is built before the target by adding a dependency: `add_dependencies(mylib libfoo)`.
   ```
3. **Use generator expressions in INSTALL_RPATH to conditionally include paths only when they exist, e.g., `$<TARGET_FILE_DIR:libfoo>`.** (80% success)
   ```
   Use generator expressions in INSTALL_RPATH to conditionally include paths only when they exist, e.g., `$<TARGET_FILE_DIR:libfoo>`.
   ```

## Dead Ends

- **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% fail)
- **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% fail)
- **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% fail)
