#StackBounty: #python #setup.py #mingw-w64 #distutils #pybind11 How can I build a setup.py to compile C++ extension using Python, pybin…

Bounty: 50

I’m currently trying to write a setup.py script that when the user installs the python package automatically compile my C++ extension binded with pybind11. In Windows I haven’t got any problem making it happen with VS19 MSVC compiler. But I’m trying to make it happen if the user has installed MinGW-w64 instead.

These are the package files:


#include <pybind11/pybind11.h>

int add(int i, int j) {
    return i + j;

namespace py = pybind11;

PYBIND11_MODULE(pybind11_example, m) {

    m.def("add", &add);


from setuptools import setup, Extension
import pybind11

ext_modules = [
        sources = ['main.cpp'],


Having the two files in the same folder and running from the command prompt:

python setup.py build

If the user has VS19 MSVC compiler installed it succesfully generates pybind11_example.pyd that can be tested to work running with python:

import pybind11_example as m
print(m.add(1, 2))

But if the user has Mingw-w64 compiler installed raises an error saying that Visual Studio 2015 is required.

Note that I can easily compile main.cpp in to pybind11_example.pyd manually with Mingw-w64 running:

g++ -static -shared -std=c++11 -DMS_WIN64 -fPIC -I C:...PythonPython38Libsite-packagespybind11include -I C: ... PythonPython38include -L C: ... PythonPython38libs main.cpp -o pybind11_example.pyd -lPython38

Is there a way to write setup.py in a way that if the user has Windows with MinGW-w64 compiler automatically compile main.cpp in to pybind11_example.pyd when installing the package without needing to make it manually?

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.