# CMake 错误：工具链文件未找到或无效：/path/to/toolchain.cmake

- **ID:** `cmake/toolchain-file-not-found`
- **领域:** cmake
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

通过 -DCMAKE_TOOLCHAIN_FILE 指定的路径不存在、不是文件或包含语法错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.10 | active | — | — |
| CMake 3.18 | active | — | — |
| CMake 3.26 | active | — | — |

## 解决方案

1. ```
   Use absolute path: cmake -DCMAKE_TOOLCHAIN_FILE=/full/path/to/toolchain.cmake /path/to/source
   ```
2. ```
   Check file existence: ls -la /path/to/toolchain.cmake and verify it has no syntax errors by running cmake -P /path/to/toolchain.cmake.
   ```
3. ```
   Set the toolchain file in a cache variable before project(): set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/toolchain.cmake" CACHE PATH "")
   ```

## 无效尝试

- **** — CMake resolves relative paths from the build directory, not the source directory; leads to 'not found'. (70% 失败率)
- **** — Toolchain file must be set before the first project() command; otherwise CMake ignores it. (85% 失败率)
