1

This is a simple question but has been bothering me for 3 months now. When I use the setuptools/setup.py method to compile C++ code into a Python package on my windows os, it always defaults to MSVC, but part of the code uses stdlibc++ which is only accessible with GNU. Is there some way to specify it to MinGW, or somehow change the default behavior? I have looked into other methods, cppimport does not support windows, and the cmake method seems very complex to me.

For reference, a simple test to check whether the compiler is MSVC:

check_compiler.cpp

#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
namespace py = pybind11;


void test(){
    #ifdef _MSVC_LANG
    py::print("Compiled with MSVC. ");
    #else
    py::print("Not compiled with MSVC. ");
    #endif
}


PYBIND11_MODULE(check_compiler, m) {
    m.def("test", &test);
}

setup.py

"""
python setup.py install
"""

from pybind11.setup_helpers import Pybind11Extension
from setuptools import setup
import os
import shutil


ext_modules = [
    Pybind11Extension(
        'check_compiler',
        sources=['check_compiler.cpp'],
        language='c++',
        cxx_std=11
    ),
]

setup(
    name='check_compiler',
    author='XXX',
    ext_modules=ext_modules
)

# copy the package
for filename in os.listdir("dist"):
    if filename.endswith(".egg"):
        shutil.copy(os.path.join("dist", filename), ".")

Then run python setup.py install, an .egg file will be copied from subfolder to the current directory. Finally, run the following:

main.py

import check_compiler

check_compiler.test()

Similar question, but no accepted answer: How can I build a setup.py to compile C++ extension using Python, pybind11 and Mingw-w64?

Update: I was able to specify MinGW with cmake by adding -G "MinGW Makefiles" in the camke command. Still woudl welcome an answer of how to do this with setuptools, as it is the most convenient method.

Jerry
  • 107
  • 7
  • If you use MSYS2 you could open their terminal and compile. However, it will link against and probably require their own python so not sure it will be usable from windows/native python. – Something Something Sep 15 '22 at 06:56
  • You should try to create a minimum viable example that displays the problem. Say hello world for example. You will have much better chance of someone picking up on your question if they can replicate it. Perhaps the lack of answer is that folks do not see an easy answer and find it impossible to dig in. – Something Something Sep 15 '22 at 06:58
  • 1
    @HenriqueBucher done, although the result would obviously depend on people's system and compiler installed. – Jerry Sep 15 '22 at 13:57
  • Does this help? https://shwina.github.io/custom-compiler-linker-extensions/ – Something Something Sep 16 '22 at 07:34

0 Answers0