Gradle Wrapper构建CMake报错:无法找到CMAKE_CXX_COMPILER
解决Gradle Wrapper构建时CMake检测不到C/C++编译器的问题
系统已安装环境
- Python版本:2.7.18(项目要求)
- CMake版本:3.22.6
- Java版本:11.0.16.1 2022-08-12 LTS
- Visual Studio 2022 Community(已安装C++ 2012开发工具 Redistributable (x64))
- 手动设置CMAKE_CXX_COMPILER路径:
2022\Community\VC\Tools\MSVC\14.37.32822\bin\Hostx64\x64\cl.exe - Android SDK Build Tools版本:34
构建错误日志
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/bin/Hostx64/x64/cl.exe - broken CMake Error at C:/Program Files/CMake/share/cmake-3.22/Modules/CMakeTestCCompiler.cmake:69 (message): The C compiler "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.37.32822/bin/Hostx64/x64/cl.exe" is not able to compile a simple test program. CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:2 (project) CMake Error at CMakeLists.txt:2 (project): No CMAKE_CXX_COMPILER could be found. -- Configuring incomplete, errors occurred!
解决步骤
1. 补全Visual Studio C++开发组件
打开Visual Studio Installer,确认安装**「Desktop development with C++」**工作负载,必须包含以下组件:
- MSVC v143 - VS 2022 C++ x64/x86 build tools
- 对应版本的Windows SDK(建议选项目兼容的最新版)
- C++ CMake tools for Windows
仅安装C++ 2012 Redistributable无法满足编译需求,必须安装完整的构建工具链。
2. 通过VS命令行环境启动构建
不要手动指定CMAKE_CXX_COMPILER路径,改用Visual Studio自带的命令行环境:
- 打开x64 Native Tools Command Prompt for VS 2022
- 切换到项目根目录,执行Gradle构建命令:
./gradlew build
该命令行已自动配置好编译器的环境变量,CMake可直接检测到工具链。
3. 验证编译器环境有效性
在上述命令行中执行以下命令,确认编译器可正常调用:
cmake --version cl
若cl命令输出编译器版本信息,说明环境配置正确。
4. 检查CMakeLists.txt语言配置
确保项目的CMakeLists.txt开头同时指定C和C++语言:
project(YourProjectName C CXX)
避免仅指定单一语言导致CXX编译器检测失败。
5. 清理缓存后重新构建
删除项目下的build目录、CMakeFiles目录及.cmake缓存文件,再重新执行构建命令,排除旧缓存干扰。
内容的提问来源于stack exchange,提问作者Narmadha
相关产品推荐
相关产品推荐

