CMake 错误:Ninja 生成器不支持 CXX 编译器 'clang++',因为它不是已知的编译器。
CMake Error: The Ninja generator does not support the CXX compiler 'clang++' because it is not a known compiler.
ID: cmake/generator-ninja-missing-compiler
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| CMake 3.10 | active | — | — | — |
| CMake 3.16 | active | — | — | — |
| CMake 3.20 | active | — | — | — |
| Ninja 1.10 | active | — | — | — |
| Ninja 1.11 | active | — | — | — |
根因分析
CMake 的 Ninja 生成器有一个硬编码的支持编译器列表;如果编译器不在该列表中(例如自定义构建的 clang 或不同版本),CMake 会拒绝它。
English
CMake's Ninja generator has a hardcoded list of supported compilers; if the compiler is not in that list (e.g., a custom build of clang or a different version), CMake rejects it.
官方文档
https://cmake.org/cmake/help/latest/generator/Ninja.html解决方案
-
改用 'Unix Makefiles' 生成器:cmake -G "Unix Makefiles" ..
-
设置 CMAKE_GENERATOR 为 Ninja 并设置 CMAKE_CXX_COMPILER 为已知编译器如 g++:cmake -G Ninja -DCMAKE_CXX_COMPILER=g++ ..
-
升级 CMake 到支持你的编译器的版本。例如,CMake 3.20+ 增加了对更新 clang 版本的支持。检查发布说明。
无效尝试
常见但无效的做法:
-
60% 失败
If the project requires clang-specific features (e.g., libc++), using g++ may cause compilation errors.
-
70% 失败
The issue is not version-specific; CMake's Ninja generator has a whitelist of compiler IDs. Even standard clang may fail if CMake version is old.
-
30% 失败
This works but may not preserve Ninja's build speed; also, the project may have Ninja-specific custom commands.