[MinGW64 on Win11]Failed to build OpenCV-Python-headless via pip install

Mingw64 on Windows11

Python 3.12.12 [GCC 15.2.0 64 bit (AMD64)] on win32

MSYS2 Package ,mingw-w64-x86_64-python-opencv goes very well with no trouble,

However, I was encountered one issue: the import process takes an very long time.

First time import takes over 60 seconds…especially on newer PC (Core Ultra)

So I decided to build “headless version“ as light module

Following is the procedure.

(1)Remove MSYS2 packages for opencv-python

pacman -R mingw-w64-x86_64-python-opencv

(2)Switching venv(named venv-trw)

source venv-trw/bin/activate

(3)Pip Install

pip install opencv-python-headless

#Failed#


(Log with Error Message)

Installing build dependencies … done

Getting requirements to build wheel … done

Installing backend dependencies … done

Preparing metadata (pyproject.toml) … -

Building wheels for collected packages: opencv-python-headless, numpy

Building wheel for opencv-python-headless (pyproject.toml) … error

error: subprocess-exited-with-error

× Building wheel for opencv-python-headless (pyproject.toml) did not run successfully.

│ exit code: 1

╰─> [525 lines of output]

--------------------------------------------------------------------------------

-- Trying ‘Ninja (Visual Studio 17 2022 x64 v144)’ generator

--------------------------------

---------------------------

----------------------

-----------------

------------

-------

--

Not searching for unused variables given on the command line.

CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):

Compatibility with CMake < 3.10 will be removed from a future version of

CMake.

Update the VERSION argument value. Or, use the … syntax

to tell CMake that the project requires at least but has been updated

to work with policies introduced by or earlier.

CMake Error at CMakeLists.txt:2 (PROJECT):

Generator

Ninja

does not support platform specification, but platform

x64 was specified.

-- Configuring incomplete, errors occurred!

(Repeated)


“Why not Mingw64 but Visual Studio(Windows)?”

I have tried various methods to resolve this error, but none of them were effective.

specifying pydistutils.cfg

[build]

compiler=mingw32

[build_ext]

compiler=mingw32

(4)Build from Source and pip wheel

Next, I attempted to manually build the source code within the MinGW64 environment,

and I‘m currently stuck at the final build with ninja.


(i)Get Sourcecode from Github

git clone --recursive https://github.com/opencv/opencv-python.git

(ii)Setting Option as Env

export ENABLE_HEADLESS=1 #Headless version

export CMAKE_ARGS=“-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++”

#Use Mingw64 build tools

(iii)Build it via pip wheel(mingw64) :FAILED

cd ~/opencv-python

pip wheel . —verbose

(Error Message

“Ninja does not support platform specification, but platform

x64 was specified.” )

←-I’m quite stuck now.

Any suggestions would be greatly appreciated.

I can’t help you with that mess.

I can only ask you what the actual goal is. There are binary packages for everything. Nothing should need to be built here.

On Windows, you can and should use native Python and packages, not anything involving “mingw”. On Windows, you can also use WSL, which is no different from an arbitrary Linux environment.

Thanks again for the quick reply. I’m sorry for not explaining well.

My actual goal is “Getting OpenCV-python-headless module for my Windows app.”

This Apps depends on Gtk+3 (GUI), So I choose MinGW64 Enviroment.

I totally get why you suggested using `pip install` or native Windows Python. That would be the easy way, and I know about the binary packages available via `pip`, `pacman`, etc.

My Windows app uses Gtk+3 for the GUI. To make Gtk work correctly on Windows, all my Python modules—including OpenCV-Python—have to be compiled inside the MinGW64/MSYS2 environment. Using native Windows Python or pre-built binaries simply isn’t an option for me due to this Gtk+3 dependency.

So, my main question is really specific to the source build process in MinGW64:

What are the specific steps, CMake flags, or configs needed to successfully compile the headless OpenCV-Python bindings when targeting the MinGW Python interpreter?

The main issue is that CMake/Ninja is defaulting to the MSVC compiler even though I’m running the command from the MinGW64 shell.

It seems related to the MinGW configuration.

I’m also looking into how to build the headless module from

MSYS2 Packages source code(mingw-w64-x86_64-python-opencv) , as an alternative approach.

Any help or advice focused just on the MinGW source build would be hugely appreciated!

ah, I understand now.

you could contact whoever maintains the python-opencv MSYS2 package. they’ll at least have a build script and environment for their package. that could be a start for your own build that would then only need to exclude highgui from the build process.

unfortunately, I can’t help with MSYS2/MinGW builds. I’ve never done that. others on the forum might have done that previously.

did the existing python-opencv package for MSYS2 (with GUI) conflict with your GTK 3? according to that web page, it depends on Qt 6. that might conflict.

Thanks for quick reply again and giving wise advice.
The issue is about MSYS2/MinGW builds, not OpenCV Features.
I’ll promptly contact maintainers of MSYS2.
Thanks a lot.