cmake
config_error
ai_generated
true
CMake 错误:CMAKE_CROSSCOMPILING 为 TRUE,但未设置 CMAKE_SYSTEM_NAME。
CMake Error: CMAKE_CROSSCOMPILING is TRUE but CMAKE_SYSTEM_NAME is not set.
ID: cmake/cross-compiling-toolchain-cmake-variable-not-set
82%修复率
85%置信度
1证据数
2023-04-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| cmake 3.22 | active | — | — | — |
| cmake 3.25 | active | — | — | — |
| cmake 3.28 | active | — | — | — |
根因分析
在进行交叉编译时,CMake 需要在工具链文件或命令行中设置 CMAKE_SYSTEM_NAME,否则默认为主机系统,导致不匹配。
English
When cross-compiling, CMake requires CMAKE_SYSTEM_NAME to be set in the toolchain file or command line; otherwise, it defaults to the host system, causing a mismatch.
官方文档
https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling解决方案
-
在工具链文件中设置 CMAKE_SYSTEM_NAME,例如 set(CMAKE_SYSTEM_NAME Linux)。然后通过 -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain.cmake 传递工具链文件。
-
或者在命令行中设置 CMAKE_SYSTEM_NAME:cmake -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc ..
无效尝试
常见但无效的做法:
-
Setting CMAKE_CROSSCOMPILING to FALSE manually in CMakeLists.txt
95% 失败
Overriding CMAKE_CROSSCOMPILING is a read-only variable; CMake ignores user attempts to set it.
-
Adding set(CMAKE_SYSTEM_NAME Linux) in CMakeLists.txt instead of toolchain file
90% 失败
CMAKE_SYSTEM_NAME must be set before the project() call; CMakeLists.txt executes after, so it has no effect.