CLion配置Visual Studio工具链时CMake报错:cl.exe无法编译测试程序
CLion配置VS 2019工具链时CMake检测C/C++编译器失败的解决方法
问题重现
配置Visual Studio 2019社区版作为CLion工具链时,CMake抛出编译器检测失败错误:
-- The C compiler identification is unknown -- The CXX compiler identification is unknown -- Detecting C compiler ABI info -- Detecting C compiler ABI info - failed -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - broken CMake Error at S:/Clion/CLion 2023.3.4/bin/cmake/win/x64/share/cmake-3.27/Modules/CMakeTestCCompiler.cmake:67 (message): The C compiler "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe" is not able to compile a simple test program.
已尝试set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)和add_compile_options(-DCMAKE_C_COMPILER_FORCED=ON),均无效。
可行解决方案
1. 验证VS环境完整性并加载正确环境变量
- 打开Visual Studio 2019开发者命令提示符(x64),输入
cl.exe,若无法输出版本信息,说明:- 未安装完整的C开发组件:打开VS Installer,勾选“使用C的桌面开发”,确保包含MSVC v142工具集、Windows SDK等核心组件,执行修复或安装。
- 环境变量未正确加载:从开发者命令提示符中启动CLion(输入
clion.exe的完整路径),让CLion继承VS的环境变量。
2. 手动指定CMake参数并清理缓存
- 打开CLion的
Settings → Build, Execution, Deployment → CMake,在CMake options中添加:-DCMAKE_C_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe" -DCMAKE_CXX_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe" -DCMAKE_VS_VERSION=16.0 - 删除CLion自动生成的
cmake-build-*目录,点击File → Reload CMake Project重新配置。
3. 降低CMake版本适配VS 2019
CLion 2023.3.4自带的CMake 3.27可能与VS 2019的旧工具集存在兼容性问题:
- 下载CMake 3.20.x版本(与VS 2019兼容性更好),安装后在CLion的
Settings → Build, Execution, Deployment → CMake中,将CMake executable指定为新安装的CMake路径。
4. 检查权限与路径有效性
- 以管理员身份运行CLion,避免编译器路径访问权限不足。
- 确认
cl.exe路径无中文、空格以外的特殊字符,且文件确实存在于该路径下。
内容的提问来源于stack exchange,提问作者Lincan Xu
相关产品推荐
相关产品推荐

