cmake config_error ai_generated true

CMake Error: CMAKE_CROSSCOMPILING is TRUE but CMAKE_SYSTEM_NAME is not set.

ID: cmake/cross-compiling-toolchain-cmake-variable-not-set

Also available as: JSON · Markdown · 中文
82%Fix Rate
85%Confidence
1Evidence
2023-04-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
cmake 3.22 active
cmake 3.25 active
cmake 3.28 active

Root Cause

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.

generic

中文

在进行交叉编译时,CMake 需要在工具链文件或命令行中设置 CMAKE_SYSTEM_NAME,否则默认为主机系统,导致不匹配。

Official Documentation

https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling

Workarounds

  1. 85% success Define CMAKE_SYSTEM_NAME in your toolchain file, e.g., set(CMAKE_SYSTEM_NAME Linux). Then pass the toolchain file via -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain.cmake.
    Define CMAKE_SYSTEM_NAME in your toolchain file, e.g., set(CMAKE_SYSTEM_NAME Linux). Then pass the toolchain file via -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain.cmake.
  2. 80% success Alternatively, set CMAKE_SYSTEM_NAME on the command line: cmake -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc ..
    Alternatively, set CMAKE_SYSTEM_NAME on the command line: cmake -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc ..

中文步骤

  1. 在工具链文件中设置 CMAKE_SYSTEM_NAME,例如 set(CMAKE_SYSTEM_NAME Linux)。然后通过 -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain.cmake 传递工具链文件。
  2. 或者在命令行中设置 CMAKE_SYSTEM_NAME:cmake -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc ..

Dead Ends

Common approaches that don't work:

  1. Setting CMAKE_CROSSCOMPILING to FALSE manually in CMakeLists.txt 95% fail

    Overriding CMAKE_CROSSCOMPILING is a read-only variable; CMake ignores user attempts to set it.

  2. Adding set(CMAKE_SYSTEM_NAME Linux) in CMakeLists.txt instead of toolchain file 90% fail

    CMAKE_SYSTEM_NAME must be set before the project() call; CMakeLists.txt executes after, so it has no effect.