You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows 11下CMake编译失败求助:g++.exe相关问题排查

编译OpenCV时CMake报错问题排查

问题描述

在Windows 11上使用CMake编译OpenCV 4.5.5时持续失败,核心错误指向MinGW的g++.exe无法编译测试程序,更换不同版本的相关工具后问题依旧。

完整错误日志

'Release' build type is used by default. Use CMAKE_BUILD_TYPE to specify build type (Release or Debug)
The CXX compiler identification is GNU 8.1.0
The C compiler identification is unknown
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - failed
Check for working CXX compiler: D:/Program Files/MinGW/mingw64/bin/g++.exe
Check for working CXX compiler: D:/Program Files/MinGW/mingw64/bin/g++.exe - broken
CMake Error at D:/Program Files/cmake-3.18.4-win64-x64/share/cmake-3.18/Modules/CMakeTestCXXCompiler.cmake:59 (message):
The C++ compiler

"D:/Program Files/MinGW/mingw64/bin/g++.exe"

is not able to compile a simple test program.

It fails with the following output:

Change Dir: D:/Opencv/opencv4.5.5/sources/_output/CMakeFiles/CMakeTmp

Run Build Command(s):D:/PROGRA~2/MinGW/mingw64/bin/mingw32-make.exe cmTC_76d6e/fast && D:/PROGRA~2/MinGW/mingw64/bin/mingw32-make.exe  -f CMakeFiles\cmTC_76d6e.dir\build.make CMakeFiles/cmTC_76d6e.dir/build

mingw32-make.exe[1]: Entering directory 'D:/Opencv/opencv4.5.5/sources/_output/CMakeFiles/CMakeTmp'

Building CXX object CMakeFiles/cmTC_76d6e.dir/testCXXCompiler.cxx.obj

D:\PROGRA~2\MinGW\mingw64\bin\G__~1.EXE    -o CMakeFiles\cmTC_76d6e.dir\testCXXCompiler.cxx.obj -c D:\Opencv\opencv4.5.5\sources\_output\CMakeFiles\CMakeTmp\testCXXCompiler.cxx

Linking CXX executable cmTC_76d6e.exe

"D:\Program Files\cmake-3.18.4-win64-x64\bin\cmake.exe" -E cmake_link_script CMakeFiles\cmTC_76d6e.dir\link.txt --verbose=1

%TSD-Header-###%by锟斤拷f锟斤拷锟絔锟斤拷J锟?锟?  锟?          @  锟絣3 锟? 锟紾P 锟斤拷锟?Z锟?v锟斤拷锟斤拷;_~锟揭?)b&膸锟?锟?锟?T锟絜>}锟紷_鈥诧拷F锟斤拷锟絲锟斤拷7锟斤拷|锟?4锟斤拷A锟斤拷锟絨锟斤拷锟斤拷锟?a$崭锟組?E2锟?v锟斤拷锟斤拷;_锟斤拷聻)c'锟?锟斤拷T8T锟絜.}锟紸_鈥诧拷F锟絓锟?锟斤拷7锟缴ふ?Error running link command: 绯荤粺鎵句笉鍒版寚瀹氱殑鏂囦欢銆?mingw32-make.exe[1]: *** [CMakeFiles\cmTC_76d6e.dir\build.make:107: cmTC_76d6e.exe] Error 2

mingw32-make.exe[1]: Leaving directory 'D:/Opencv/opencv4.5.5/sources/_output/CMakeFiles/CMakeTmp'

mingw32-make.exe: *** [Makefile:139: cmTC_76d6e/fast] Error 2

CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:113 (enable_language)

Configuring incomplete, errors occurred!
See also "D:/Opencv/opencv4.5.5/sources/_output/CMakeFiles/CMakeOutput.log".
See also "D:/Opencv/opencv4.5.5/sources/_output/CMakeFiles/CMakeError.log".

环境配置

  • 操作系统:Windows 11 Professional
  • CMake版本:cmake-3.18.4-win64-x64
  • OpenCV版本:4.5.5
  • MinGW64:MinGW-W64 GCC-8.1.0

可能的问题原因及解决办法

1. MinGW安装不完整或损坏

错误日志中出现乱码和"系统找不到指定文件"的提示,大概率是MinGW的链接器(ld.exe)缺失或损坏,或者安装包本身不完整。

  • 解决:卸载现有MinGW,下载完整的MinGW-W64安装包(推荐x86_64-posix-seh版本,避免win32线程模型的兼容性问题),安装路径选择不含空格的目录(比如D:\MinGW64,而非D:\Program Files\MinGW)。

2. 环境变量配置错误

CMake无法正确识别C编译器(日志显示"The C compiler identification is unknown"),可能是MinGW的bin目录未添加到系统环境变量PATH的最前端,或者被其他编译器路径干扰。

  • 解决:
    1. 打开系统环境变量设置,将D:\MinGW64\bin(你的MinGW实际bin路径)添加到PATH的最顶部;
    2. 重启命令提示符或CMake GUI,确保环境变量生效;
    3. 在命令行输入gcc --version和g++ --version,确认能正常输出版本信息,验证编译器可被系统调用。

3. CMake版本与MinGW/OpenCV不兼容

CMake 3.18.4版本较老,可能与Windows 11或MinGW 8.1.0存在兼容性问题。

  • 解决:升级CMake到较新的稳定版本(比如3.25+),再重新尝试配置编译。

4. 路径含空格导致的问题

MinGW和OpenCV的安装路径存在空格(比如D:\Program Files\MinGW),可能导致链接时路径解析错误,出现乱码和文件找不到的问题。

  • 解决:将MinGW和OpenCV移动到不含空格和中文的路径下,比如D:\Dev\MinGW64、D:\Dev\OpenCV4.5.5。

5. 链接器配置错误

日志中链接阶段出现乱码,可能是链接器的配置文件损坏或路径错误。

  • 解决:检查MinGW的bin目录下是否存在ld.exe,若不存在则重新安装MinGW;若存在,尝试在命令行手动编译一个简单的C++程序验证链接功能:
    // test.cpp
    #include <iostream>
    int main() {
        std::cout << "Hello World" << std::endl;
        return 0;
    }
    
    执行命令:g++ test.cpp -o test.exe,如果能生成可执行文件并正常运行,说明编译器本身没问题,问题出在CMake的配置上;如果失败,说明编译器本身有问题,需要重新安装。

内容的提问来源于stack exchange,提问作者DDanging

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 20:43:24