0

I have tried to compile a piece of complex code with zig on Windows with no success (referencing this question as a starting point):

cython base.py
zig build-lib --verbose-link -DMS_WIN64 -dynamic -target x86_64-windows \
    -I c:\python38\include\ -lc c:\python38\python38.dll base.c

The output is not very optimistic:

LLD Link... lld-link: error: undefined symbol: __declspec(dllimport) _Py_RefTotal
>>> referenced by C:\Users\xxx\AppData\Local\zig\o\xxx\base.obj:(_Py_INCREF)
>>> referenced by C:\Users\xxx\AppData\Local\zig\o\xxx\base.obj:(_Py_INCREF)
>>> referenced by C:\Users\xxx\AppData\Local\zig\o\xxx\base.obj:(_Py_DECREF)
>>> referenced 3 more times

lld-link: error: undefined symbol: __declspec(dllimport) _Py_NegativeRefcount
>>> referenced by C:\Users\xxx\AppData\Local\zig\o\xxx\base.obj:(_Py_DECREF)

Same Python/C code is compiled and linked perfectly fine with mingw64. Internet is silent on this topic. Can anyone suggest a path to dig?

UPD 1: The problem seems to be related to enabled _DEBUG macro. _Py_RefTotal and _Py_NegativeRefcount are included when Py_REF_DEBUG is defined (object.h). This controlled like Py_REF_DEBUG <- Py_DEBUG <- _DEBUG

Source file, generated by Cython, includes Python.h:

ifdef MS_COREDLL -> 
    if defined(_MSC_VER) -> 
        if defined(_DEBUG) -> 
            pragma comment(lib,"python27_d.lib") -> 
                this debug library is missing on default Python setup

So the question is changed to: How to disable debug macro while compiling with zig (without Python imports modification)? This is something happened when compiling with mingw.

WORKAROUND

Unfortunately, -U option (undef macro) is not implemented in zig compiler. The only automated workaround I was able to figure out so far, is to update the code generated by Cython with a simple batch file:

@echo off

cython base.py

echo #ifdef _DEBUG>header.c
echo   #undef _DEBUG>>header.c
echo #endif>>header.c

type header.c >> base_new.c
type base.c >> base_new.c
del /Q base.c 1>nul 2>&1
del /Q header.c 1>nul 2>&1
move base_new.c base.c 1>nul 2>&1

zig build-lib --verbose-link -DMS_WIN64 -dynamic -target x86_64-windows \
    -I c:\python38\include\ -lc c:\python38\python38.dll base.c

0 Answers0