title: How can I build a setup.py to compile C++ extension using Python, pybind11 and Mingw-w64?
body: <p>I'm currently trying to write a 'setup.py' script that when the user installs the python package automatically compiles my C++ extension bound with 'pybind11'. In Windows, I haven't got any problem making it happen with the 'VS19 MSVC' compiler. But I'm trying to make it happen if the user has installed 'MinGW-w64' instead.</p>
  <p>These are the package files:</p>
  <pre>**main.cpp**
  
      #include &lt;pybind11/pybind11.h&gt;
      
      int add(int i, int j) {
          return i + j;
      }
      
      namespace py = pybind11;
      
      PYBIND11_MODULE(pybind11_example, m) {
      
          m.def(&quot;add&quot;, &amp;add);
      }
  </pre>
  <pre class="lang-py prettyprint-override"><code>**setup.py**
  
      from setuptools import setup, Extension
      import pybind11
      
      ext_modules = [
          Extension(
              'pybind11_example',
              sources = ['main.cpp'],
              include_dirs=[pybind11.get_include()],
              language='c++'
          ),
      ]
      
      setup(
          name='pybind11_example',
          ext_modules=ext_modules
      )
  </code></pre>
  <p>Having the two files in the same folder and running from the command prompt:</p>
  <pre class="lang-sh prettyprint-override"><code>    python setup.py build
  </code></pre>
  <p>If the user has <code>VS19 MSVC</code> compiler installed it successfully generates <code>**pybind11_example.pyd**</code> that can be tested to work running with python:</p>
  <pre class="lang-py prettyprint-override"><code>    import pybind11_example as m
      print(m.add(1, 2))
  </code></pre>
  <p>But if the user has a <code>Mingw-w64</code> compiler installed raises an error saying that Visual Studio 2015 is required.</p>
  <p>Note that I can easily compile <code>**main.cpp**</code> in to <code>**pybind11_example.pyd**</code> manually with <code>Mingw-w64</code> running:</p>
  <pre><code>    g++ -static -shared -std=c++11 -DMS_WIN64 -fPIC -I C:\...\Python\Python38\Lib\site-packages\pybind11\include -I C:\ ... \Python\Python38\include -L C:\ ... \Python\Python38\libs main.cpp -o pybind11_example.pyd -lPython38
  </code></pre>
  <p>Is there a way to write <code>**setup.py**</code> in a way that if the user has Windows with a <code>MinGW-w64</code> compiler automatically compile <code>**main.cpp**</code> into <code>**pybind11_example.pyd**</code> when installing the package without needing to make it manually?</p>