Clion MinGW之The C compiler "MinGW/bin/gcc.exe" is not able to compile a simple test program.

在安装Clion后配置MinGW环境时,gcc.exe显示有错误。

查看到类似以下内容(当时忘记记录了,这是某位提问者的提示):



    -- The C compiler identification is unknown
    -- The CXX compiler identification is unknown
    -- Check for working C compiler: C:/MinGW/bin/gcc.exe
    CMake Error: Generator: execution of make failed. Make command was: "C:/MinGW/bin/mingw32-make.exe" "cmTC_87f99/fast"
    -- Check for working C compiler: C:/MinGW/bin/gcc.exe -- broken
    CMake Error at C:/Program Files/CMake/share/cmake-3.12/Modules/CMakeTestCCompiler.cmake:52 (message):
    The C compiler

    "C:/MinGW/bin/gcc.exe"

    is not able to compile a simple test program.

    It fails with the following output:

    Change Dir: C:/Users/鍙惰惤鐭ョ/AppData/Local/Temp/cmake_check_environment/_build/CMakeFiles/CMakeTmp

    Run Build Command:"C:/MinGW/bin/mingw32-make.exe" "cmTC_87f99/fast"
    Exit code 0xc0000135

    Generator: execution of make failed. Make command was: "C:/MinGW/bin/mingw32-make.exe" "cmTC_87
"D:\Sketchpad5.06\Sketchpad5\Evoker12345\Creation\c-c++\JetBrains CLion\CLion 2020.3.4\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" D:\\编辑\C++\时间显示\工作区0 -- The C compiler identification is GNU 8.1.0 -- The CXX compiler identification is GNU 8.1.0 -- Check for working C compiler: D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/gcc.exe -- Check for working C compiler: D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/gcc.exe - broken CMake Error at D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/JetBrains CLion/CLion 2020.3.4/bin/cmake/win/share/cmake-3.17/Modules/CMakeTestCCompiler.cmake:60 (message): The C compiler "D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/gcc.exe" is not able to compile a simple test program. It fails with the following output: Change Dir: D:/顏涱亼顏旑伋顏旑伨/缂栬緫/C++/鏃堕棿鏄剧ず/宸ヤ綔鍖�0/cmake-build-debug/CMakeFiles/CMakeTmp Run Build Command(s):D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/mingw32-make.exe cmTC_37cc5/fast && D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/mingw32-make.exe -f CMakeFiles\cmTC_37cc5.dir\build.make CMakeFiles/cmTC_37cc5.dir/build mingw32-make.exe[1]: Entering directory 'D:/顏涱亼顏旑伋顏旑伨/缂栬緫/C++/鏃堕棿鏄剧ず/宸ヤ綔鍖�0/cmake-build-debug/CMakeFiles/CMakeTmp' Building C object CMakeFiles/cmTC_37cc5.dir/testCCompiler.c.obj D:\Sketchpad5.06\Sketchpad5\Evoker12345\Creation\c-c++\MinGW\mingw-18.0\MinGW\bin\gcc.exe -o CMakeFiles\cmTC_37cc5.dir\testCCompiler.c.obj -c D:\椤忔侗浜奸鏃戜紜椤忔棏浼╘缂傛牞绶玕C++\閺冨爼妫块弰鍓с仛\瀹搞儰缍旈崠?\cmake-build-debug\CMakeFiles\CMakeTmp\testCCompiler.c gcc.exe: error: D:\椤忔侗浜奸鏃戜紜椤忔棏浼╘缂傛牞绶玕C++\閺冨爼妫块弰鍓с仛\瀹搞儰缍旈崠?\cmake-build-debug\CMakeFiles\CMakeTmp\testCCompiler.c: No such file or directory gcc.exe: fatal error: no input files compilation terminated. mingw32-make.exe[1]: *** [CMakeFiles\cmTC_37cc5.dir\build.make:85: CMakeFiles/cmTC_37cc5.dir/testCCompiler.c.obj] Error 1 mingw32-make.exe[1]: Leaving directory 'D:/顏涱亼顏旑伋顏旑伨/缂栬緫/C++/鏃堕棿鏄剧ず/宸ヤ綔鍖�0/cmake-build-debug/CMakeFiles/CMakeTmp' mingw32-make.exe: *** [Makefile:140: cmTC_37cc5/fast] Error 2 CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:2 (project) -- Configuring incomplete, errors occurred! See also "D:/顏涱亼顏旑伋顏旑伨/缂栬緫/C++/鏃堕棿鏄剧ず/宸ヤ綔鍖�0/cmake-build-debug/CMakeFiles/CMakeOutput.log". See also "D:/顏涱亼顏旑伋顏旑伨/缂栬緫/C++/鏃堕棿鏄剧ず/宸ヤ綔鍖�0/cmake-build-debug/CMakeFiles/CMakeError.log". [Finished]帮我诊断一下,谢谢!
06-16
"C:\stm32cubemx\Clion\CLion 2025.1.2\bin\cmake\win\x64\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=C:/stm32cubemx/tool/STM32CubeCLT_1.18.0/Ninja/bin/ninja.exe -DCMAKE_C_COMPILER=C:/stm32cubemx/tool/STM32CubeCLT_1.18.0/GNU-tools-for-STM32/bin/arm-none-eabi-gcc.exe -DCMAKE_CXX_COMPILER=C:/stm32cubemx/tool/STM32CubeCLT_1.18.0/GNU-tools-for-STM32/bin/arm-none-eabi-c++.exe -G Ninja -S C:\Users\Administrator\Desktop\bisai\led -B C:\Users\Administrator\Desktop\bisai\led\cmake-build-debug-stm32 -- The C compiler identification is GNU 13.3.1 -- The CXX compiler identification is GNU 13.3.1 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - failed -- Check for working C compiler: C:/stm32cubemx/tool/STM32CubeCLT_1.18.0/GNU-tools-for-STM32/bin/arm-none-eabi-gcc.exe -- Check for working C compiler: C:/stm32cubemx/tool/STM32CubeCLT_1.18.0/GNU-tools-for-STM32/bin/arm-none-eabi-gcc.exe - broken CMake Error at C:/stm32cubemx/Clion/CLion 2025.1.2/bin/cmake/win/x64/share/cmake-3.31/Modules/CMakeTestCCompiler.cmake:67 (message): The C compiler "C:/stm32cubemx/tool/STM32CubeCLT_1.18.0/GNU-tools-for-STM32/bin/arm-none-eabi-gcc.exe" is not able to compile a simple test program. It fails with the following output: Change Dir: 'C:/Users/Administrator/Desktop/bisai/led/cmake-build-debug-stm32/CMakeFiles/CMakeScratch/TryCompile-poisww' Run Build Command(s): C:/stm32cubemx/tool/STM32CubeCLT_1.18.0/Ninja/bin/ninja.exe -v cmTC_2ffe4 [1/2] C:\stm32cubemx\tool\STM32CubeCLT_1.18.0\GNU-tools-for-STM32\bin\arm-none-eabi-gcc.exe -std=gnu11 -fdiagnostics-color=always -o CMakeFiles/cmTC_2ffe4.dir/testCCompiler.c.obj -c C:/Users/Administrator/Desktop/bisai/led/cmake-build-debug-stm32/CMakeFiles/CMakeScratch/TryCompile-poisww/testCCompiler.c [2/2] C:\WINDOWS\system32\cmd.exe /C "cd . && C:\stm32cubemx\tool\STM32CubeCLT_1.18.0\GNU-tools-for-STM32\bin\arm-none-eabi-gcc.exe CMakeFiles/cmTC_2ffe4.dir/testCCompiler.c.obj -o cmTC_2ffe4.exe -Wl,--out-implib,libcmTC_2ffe4.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ." FAILED: cmTC_2ffe4.exe C:\WINDOWS\system32\cmd.exe /C "cd . && C:\stm32cubemx\tool\STM32CubeCLT_1.18.0\GNU-tools-for-STM32\bin\arm-none-eabi-gcc.exe CMakeFiles/cmTC_2ffe4.dir/testCCompiler.c.obj -o cmTC_2ffe4.exe -Wl,--out-implib,libcmTC_2ffe4.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ." C:/stm32cubemx/tool/STM32CubeCLT_1.18.0/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/13.3.1/../../../../arm-none-eabi/bin/ld.exe: unrecognized option '--major-image-version' C:/stm32cubemx/tool/STM32CubeCLT_1.18.0/GNU-tools-for-STM32/bin/../lib/gcc/arm-none-eabi/13.3.1/../../../../arm-none-eabi/bin/ld.exe: use the --help option for usage information collect2.exe: error: ld returned 1 exit status ninja: build stopped: subcommand failed. CMake will not be able to correctly generate this project. Call Stack (most recent call first): CMakeLists.txt:28 (project) -- Configuring incomplete, errors occurred! [已完成]
07-11
<think>我们正在解决CLionCMake配置时出现的C编译器错误问题。错误信息指向CMakeTestCCompiler.cmake文件,通常表示CMake在测试C编译器时失败。根据引用[2]和引用[3]的提示,这可能是由于编译器路径配置不正确或编译器本身无法工作导致的。用户提供的编译器路径为:`D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/gcc.exe`解决方案步骤:1.**验证编译器路径**:首先确认该路径下确实存在`gcc.exe`。如果路径中有空格或特殊字符,可能会导致问题,但这里路径没有空格。注意:路径中的斜杠方向在Windows中可以是正斜杠或反斜杠,但最好使用正斜杠或双反斜杠。2.**检查编译器是否可用**:打开命令提示符,导航到上述路径,运行`gcc--version`,看是否能够正确输出GCC版本。如果不能,说明编译器可能损坏或环境变量有问题。3.**在CLion中配置工具链**:-打开CLion,进入`File->Settings->Build,Execution,Deployment->Toolchains`-在右侧,检查或添加一个MinGW工具链,将`Environment`设置为`MinGW`,然后指定`gcc.exe`的完整路径(即上面的路径)。同时,确保`CCompiler`和`C++Compiler`字段自动填充正确(通常会自动填充为相同目录下的`gcc.exe`和`g++.exe`)。4.**检查CMakeLists.txt**:确保没有错误的设置,特别是`project`命令之前的编译器设置。通常不需要在CMakeLists.txt中设置编译器路径,因为CLion会通过工具链设置。5.**清理并重新加载CMake项目**:-删除项目目录下的`cmake-build-debug`和`cmake-build-release`目录(或者任何你构建的目录)。-CLion中,点击`File->ReloadCMakeProject`。6.**如果问题仍然存在**,可能是由于编译器不兼容或损坏。尝试重新安装MinGW,并确保选择与系统匹配的版本(32位或64位)。安装后,重复上述步骤。7.**引用[4]中提到的问题**:如果之前运行的程序没有关闭,可能会导致无法重新编译。打开任务管理器,结束与项目相关的进程(例如你的程序名.exe),然后再重新构建。8.**查看错误日志**:根据引用[3],错误信息会记录在`CMakeError.log`中,该文件位于构建目录(如`cmake-build-debug/CMakeFiles/CMakeError.log`)。打开该文件,查看具体的错误原因,例如编译测试程序失败的原因。常见错误原因及解决:-编译器路径错误:重新指定正确的路径。-编译器损坏:重新安装。-权限问题:以管理员身份运行CLion,或者关闭占用文件的进程(引用[4])。-环境变量问题:确保MinGWbin目录已添加到系统PATH环境变量中。下面是一个具体的步骤示例:**步骤1:检查编译器路径**打开文件资源管理器,导航到`D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin`,确认`gcc.exe`存在。**步骤2:在CLion中设置工具链**-打开CLion,进入设置(File->Settings)-选择:Build,Execution,Deployment->Toolchains-点击“+”添加一个MinGW工具链-在Environment中选择MinGW-在“Make,CCompiler,C++Compiler”中,分别指定:Make:自动检测或手动指定为`D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/mingw32-make.exe`CCompiler:`D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/gcc.exe`C++Compiler:`D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/g++.exe`**步骤3:重新加载CMake项目**-删除项目下的cmake-build-*目录-CLion中,Tools->CMake->ResetCacheandReloadProject如果按照以上步骤操作后问题仍然存在,请检查MinGW版本是否与系统兼容(例如64位系统需要64位MinGW)以及是否安装了必要的组件(gcc-core,gcc-g++,binutils等)。希望这些步骤能帮助您解决问题。§§相关问题§§1.如何在CLion中配置多个构建工具链?2.如何解决CMake项目中的链接错误?3.如何优化CLion的构建速度?4.如何为CMake项目添加外部库依赖?5.如何调试CMake脚本?</think>以下是解决 CLionCMake C 编译器错误的完整指南,基于您提供的错误信息和引用内容: ### 一、问题诊断 根据引用[2]和引用[3],错误核心是: ```cmake The CMAKE_C_COMPILER: D:/.../gcc.exe is not a full path and was not found in the PATH ``` 这表示 CMake 无法找到有效的 C 编译器路径,可能由以下原因导致: 1. **路径格式错误**(引用[2]) 2. **编译器文件损坏**(引用[4]) 3. **进程占用导致权限问题**(引用[4]) ### 二、解决方案步骤 #### 步骤 1:验证编译器路径 1. 打开文件资源管理器,导航到: ``` D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/ ``` 2. 检查是否存在以下文件: - `gcc.exe`(C编译器) - `g++.exe`(C++编译器) - `mingw32-make.exe`(构建工具) #### 步骤 2:在 CLion 中配置工具链 1. 打开 CLion → `File` → `Settings` → `Build, Execution, Deployment` → `Toolchains` 2. 删除现有配置,点击 `+` 添加新配置 3. 按以下示例设置: ```plaintext Name: MinGW-18.0 Environment: MinGW Make: D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/mingw32-make.exe C Compiler: D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/gcc.exe C++ Compiler: D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/g++.exe ``` 4. 点击 `Apply` 保存 #### 步骤 3:解决进程占用问题(引用[4]) 1. 按 `Ctrl+Shift+Esc` 打开任务管理器 2. 查找并结束以下进程: - 您的程序名.exe - mingw32-make.exe - cmake.exe 3. 如果无法结束,使用强制删除: - 打开腾讯电脑管家 → 工具箱 → 文件粉碎 - 添加路径: ``` D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/ ``` #### 步骤 4:修改 CMakeLists.txt 在项目根目录的 `CMakeLists.txt` 中添加编译器路径强制指定: ```cmake cmake_minimum_required(VERSION 3.10) project(YourProjectName) # 强制指定编译器路径 [^2] set(CMAKE_C_COMPILER "D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/gcc.exe") set(CMAKE_CXX_COMPILER "D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW/bin/g++.exe") # 添加可执行文件 add_executable(YourProjectName main.cpp) ``` #### 步骤 5:清理并重建项目 1.CLion 中: - `File` → `Invalidate Caches` → 选择 `Invalidate and Restart` 2. 重启后: - 删除项目中的 `cmake-build-*` 目录 - 点击 `Reload CMake Project` 按钮(工具栏右侧) ### 三、验证解决方案 1. 创建测试文件 `main.cpp`: ```cpp #include <iostream> int main() { std::cout << "Compiler test successful!" << std::endl; return 0; } ``` 2. 运行程序,如果输出成功则表示配置正确 ### 四、预防措施 1. **路径管理最佳实践**: ```cmake # 使用CMake变量避免硬编码路径 set(MINGW_PATH "D:/Sketchpad5.06/Sketchpad5/Evoker12345/Creation/c-c++/MinGW/mingw-18.0/MinGW") set(CMAKE_C_COMPILER "${MINGW_PATH}/bin/gcc.exe") ``` 2. **环境变量配置**: - 添加系统环境变量: ``` Path += D:\Sketchpad5.06\Sketchpad5\Evoker12345\Creation\c-c++\MinGW\mingw-18.0\MinGW\bin ``` ### 五、常见错误处理表 | 错误现象 | 解决方案 | 引用来源 | |---------|---------|---------| | "is not a full path" | 检查路径斜杠方向(用`/`代替`\`) | [^2] | | "permission denied" | 结束占用进程或强制删除 | [^4] | | 编译器测试失败 | 重装MinGW或验证编译器版本 | [^3] | | 缓存冲突 | 执行`Invalidate Caches` | [^1] | > **重要提示**:如果使用 Qt 项目,确保在 `CMakeLists.txt` 中添加 `find_package(Qt5 REQUIRED COMPONENTS Core)` 并正确配置 Qt 路径(引用[1])
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值