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

Windows下使用LLVM与MinGW编译链接OpenCV时出现错误

Windows下用LLVM+MinGW编译OpenCV的问题

环境配置

  • 系统:Windows
  • 工具链:从LLVM官网获取的LLVM,从mingw-builds-binaries获取的MinGW
  • bash环境中设置的变量:
export CC="clang.exe -target x86_64-pc-windows-gnu"
export CXX="clang++.exe -target x86_64-pc-windows-gnu"
export CMAKE_GENERATOR="MinGW Makefiles"

注:不给clang添加-target参数时,用MSVC可正常编译

链接阶段报错:pthread符号未定义

编译过程前9%正常,到链接libopencv_core460.dll时终止,输出如下:

[  9%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/split.avx2.cpp.obj
[  9%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/sum.avx2.cpp.obj
[  9%] Building CXX object modules/core/CMakeFiles/opencv_core.dir/matmul.avx512_skx.cpp.obj
[  9%] Building RC object modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj
[  9%] Linking CXX shared library ..\..\bin\libopencv_core460.dll

伴随大量pthread相关未定义引用错误,示例:

C:\Users\akar\bin\mingw\mingw64\bin\ld.exe: CMakeFiles\opencv_core.dir/objects.a(system.cpp.obj):system.cpp:(.text$_ZN2cv5errorERKNS_9ExceptionE[_ZN2cv5errorERKNS_9ExceptionE]+0x34): undefined reference to `pthread_mutex_lock'
C:\Users\akar\bin\mingw\mingw64\bin\ld.exe: CMakeFiles\opencv_core.dir/objects.a(system.cpp.obj):system.cpp:(.text$_ZN2cv5errorERKNS_9ExceptionE[_ZN2cv5errorERKNS_9ExceptionE]+0xc5): undefined reference to `pthread_mutex_unlock'
C:\Users\akar\bin\mingw\mingw64\bin\ld.exe: CMakeFiles\opencv_core.dir/objects.a(system.cpp.obj):system.cpp:(.text$_ZN2cv5errorERKNS_9ExceptionE[_ZN2cv5errorERKNS_9ExceptionE]+0x176): undefined reference to `pthread_mutex_unlock'

修改链接器后出现标准库错误

尝试通过设置alias让链接器默认链接pthread:

alias ld="ld -lpthread"

编译时出现新错误:

C:/Users/akar/source/repos/opencv/modules/core/include\opencv2/core/utility.hpp:718:9: error: no type named 'recursive_mutex' in namespace 'std'; did you mean '__gnu_cxx::__recursive_mutex'?
typedef std::recursive_mutex Mutex;
        ^~~~~~~~~~~~~~~~~~~~
        __gnu_cxx::__recursive_mutex
C:\Users\akar\bin\mingw\mingw64\lib\gcc\x86_64-w64-mingw32\12.1.0\include\c++\ext/concurrence.h:170:9: note: '__gnu_cxx::__recursive_mutex' declared here
  class __recursive_mutex
        ^
1 error generated.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 11:54:26