Compiling and Linking pre-built Windows Python libraries with C++ files on Linux for Windows

Eryk Sun eryksun at gmail.com
Sat Mar 19 13:09:46 EDT 2022


On 3/18/22, Ankit Agarwal <ankit at applied.co> wrote:
> Hi,
>
> This is a very specific question. I am trying to figure out whether or not
> I can use pre-built python libraries and headers on Windows in a MinGW
> build on Linux. Essentially I have some python and C++ code which interface
> via cython and pybind. I want to build a self contained C++ binary for
> Windows with the MinGW compiler that runs on Linux. For both Cython and
> PyBind, they need to compile with the python headers and link against the
> python DLL in order to run on Windows.
>
> I know that the python DLL specifically are compiled with the MSVC
> compiler, however since it is in C, the ABI between the DLL should be
> compatible with MinGW, and I should be able to import and link against it.
> My question is will this work, or will there be some other problem that I
> might run into.

MinGW used to link with msvcrt (the private CRT for system components)
instead of ucrt (the universal CRT). If it still does that, then you
won't be able to share some of the POSIX compatibility features
between the two CRTs, such as file descriptors and the locale. Their
FILE stream records are also incompatible. Also, you'll have to be
certain to never free() memory that was allocated by a different CRT.
msvcrt uses a private heap, and ucrt uses the main process heap. For
example (with a debugger attached):

    import ctypes
    ucrt = ctypes.CDLL('ucrtbase', use_errno=True)
    msvcrt = ctypes.CDLL('msvcrt', use_errno=True)
    ucrt.malloc.restype = ctypes.c_void_p
    msvcrt.free.argtypes = (ctypes.c_void_p,)

    >>> b = ucrt.malloc(4096)
    >>> msvcrt.free(b)

    HEAP[python.exe]: Invalid address specified to
        RtlFreeHeap( 0000024389C10000, 0000024389A58FB0 )
    (1b44.1ca0): Break instruction exception - code 80000003 (first chance)
    ntdll!RtlpBreakPointHeap+0x16:
    00007ff8`8baa511e cc              int     3
    0:000>


More information about the Python-list mailing list