0

I wrote a C wrapper to access the ezdxf Python package from my C++ program using the embedded interpreter. When I compile the wrapper by itself it works as expected (loads the module below which loads a dxf file and prints the coordinates of all the lines in the file).

ez_dxf_wrap.py:

import ezdxf
doc = None
msp = None
def print_entity(e):
    print("LINE on layer: %s\n" % e.dxf.layer)
    print("start point: %s\n" % e.dxf.start)
    print("end point: %s\n" % e.dxf.end)
def load_dxf_document(filepath):
    global doc
    global msp
    doc = ezdxf.readfile(filepath)
    msp = doc.modelspace()
    print(msp)
    for e in msp:
        print(e)
        if e.dxftype() == "LINE":
            print_entity(e)
    return True

When I compile the exact same C wrapper and same Python module but then link it into a larger library I get the following error when Python tries to import ezdxf:

Traceback (most recent call last):
  File "/home/sean/development/Auto_layout/py_dev_env/lib/python3.11/site-packages/ez_dxf_wrap.py", line 10, in <module>
    import ezdxf
  File "/home/sean/development/Auto_layout/py_dev_env/lib/python3.11/site-packages/ezdxf/__init__.py", line 17, in <module>
    from ezdxf.colors import (
  File "/home/sean/development/Auto_layout/py_dev_env/lib/python3.11/site-packages/ezdxf/colors.py", line 6, in <module>
    import math
ImportError: /usr/lib/python3.11/lib-dynload/math.cpython-311-x86_64-linux-gnu.so: undefined symbol: PyLong_AsLongLongAndOverflow
Failed to load "ez_dxf_wrap.py" 

Using same machine, same python compile flags and linker flags and same virtual environment. Any ideas what would be causing this?

Wrote C wrapper and Python module to use ezdxf Python library to load dxf file. C wrapper compiles and works by itself. When linked to larger shared library the Python interpreter fails to load the module.

EDIT: In case someone else finds this, the bug in this link and the workaround are relevant: https://github.com/python/cpython/issues/48684

Adding to my C library code:

#include <dlfcn.h>
dlopen("libpython3.11.so", RTLD_LAZY | RTLD_GLOBAL);

forces the loading of the correct shared library. This is hacky though and I will have to figure out something else for getting my project to function on Windows.

0 Answers0