Sitemap

Compiling OpenCV 5.0 with DNN Module on Windows Using MinGW64

2 min readAug 26, 2026

--

The DNN module of OpenCV-5 is a little difficult to compile on Windows (some of the 3rdparty libraries are a nuisance), so I decided to write a short guide to it. If you want to compile OpenCV 5.0.0 on Windows with DNN using MinGW64 check this.

Get mdsk0x44’s stories in your inbox

Join Medium for free to get updates from this writer.

When you will compile with a plain cmake and ninja command, you will end up with the following errors (in the same order). Below each error there is a solution that shows how to fix that error.

Error 1

C:/Users/mdskh/rdc/ws-all/opencv-5.0.0/3rdparty/mlas/lib/mlasi.h:3046:19: error: 'posix_memalign' was not declared in this scope
3046 | int err = posix_memalign(&ptr, ThreadedBufAlignment, size);
| ^~~~~~~~~~~~~~

Solution

  • Open 3rdparty/mlas/lib/mlasi.h
  • Replace ifdef _MSC_VER line with following at 3 places (line number - 100, 3017, 3037)
#if defined(_MSC_VER) || defined(_WIN32)

This will make sure it still compiles under Windows for inlining. This is just a way to break into Windows line without using MSVC.

Error 2

Next you might run into error something like this :

FAILED: [code=1] 3rdparty/mlas/CMakeFiles/opencv_dnn_mlas.dir/lib/x86_64/SgemmKernelAvx512F.S.obj
...

C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.3.0/../../../../x86_64-w64-mingw32/bin/as.exe: unrecognized option `--noexecstack'

Solution

  • Comment line 252–254 in 3rdparty/mlas/CMakelists.txt as below
 #target_compile_options(opencv_dnn_mlas PRIVATE
#"$<$<COMPILE_LANGUAGE:ASM>:-Wa, - noexecstack>"
#)

Error 3

FAILED: [code=1] 3rdparty/mlas/CMakeFiles/opencv_dnn_mlas.dir/lib/platform.cpp.obj
C:\mingw64\bin\c++.exe -DBUILD_MLAS_NO_ONNXRUNTIME=1 -DMLAS_GEMM_ONLY=1 -DMLAS_OPENCV_THREADING=1 -D_USE_MATH_DEFINES ... -c C:/opencv-5.0.0/3rdparty/mlas/lib/platform.cpp
...

C:/mingw64/lib/gcc/x86_64-w64-mingw32/15.3.0/include/xsaveintrin.h:60:1: error: inlining failed in call to 'always_inline' 'long long int _xgetbv(unsigned int)': target specific option mismatch
60 | _xgetbv (unsigned int __A)
| ^~~~~~~

Solution

  • Pass -mavx to your CMAKE_CXX_FLAGS and restart building
cmake -S . -G "Ninja" -D CMAKE_BUILD_TYPE=Release -DCMAKE_CXX_FLAGS=-mavx -B build/

Error 4

FAILED: [code=1] 3rdparty/mlas/CMakeFiles/opencv_dnn_mlas.dir/lib/x86_64/SgemmKernelSse2.S.obj
C:\mingw64\bin\gcc.exe -DBUILD_MLAS_NO_ONNXRUNTIME=1 -DMLAS_GEMM_ONLY=1 -DMLAS_OPENCV_THREADING=1 -D_USE_MATH_DEFINES -o 3rdparty/mlas/CMakeFiles/opencv_dnn_mlas.dir/lib/x86_64/SgemmKernelSse2.S.obj -c C:/opencv-5.0.0/3rdparty/mlas/lib/x86_64/SgemmKernelSse2.S
...

C:/opencv-5.0.0/3rdparty/mlas/lib/x86_64/asmmacro.h:43: Error: junk at end of line, first unrecognized character is `M'

Solution

  • Open 3rdparty/mlas/lib/x86_64/asmmacro.h
  • Locate line 43 where .type is defined
  • Wrap Linux-specific ELF assembly directives in guard checks so it is skipped.
#if !defined(_WIN32) && !defined(__CYGWIN__)
.type \FunctionName, @function
#endif

Error 5

FAILED: [code=1] bin/libopencv_dnn500.dll lib/libopencv_dnn500.dll.a
...

C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/15.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: 3rdparty/mlas/CMakeFiles/opencv_dnn_mlas.dir/lib/compute.cpp.obj:compute.cpp:(.text$_Z16MlasGQASupportedIN11onnxruntime9MLFloat16EEb15CBLAS_TRANSPOSES2_+0x6): undefined reference to `MlasHGemmSupported(CBLAS_TRANSPOSE, CBLAS_TRANSPOSE)'

Solution

  • Open 3rdparty/mlas/lib/compute.cpp
  • Add a fallback definition for non-MSVC Windows GCC builds near the top or bottom of the file
#if !defined(_MSC_VER)
bool MlasHGemmSupported(CBLAS_TRANSPOSE A, CBLAS_TRANSPOSE B) {
return false; // Tells MLAS that FP16 hardware acceleration is not available
}
#endif

--

--

mdsk0x44
mdsk0x44

Written by mdsk0x44

I am an Electronics Engr. with a masters degree in Artificial Intelligence. I enjoy designing coding solutions and sometimes debugging.