Undefined when importing a cython module using another cython module

I am working on porting a set of C functions using Cython to a module. I would like to be able to import this first module into subsequent projects based on Cython, but when I started importing derived modules in a python script, I ran into the "undefined" problem.

Consider the following minimum working example of two modules developed in separate directories:

# exModA wraps the functions provided in exModAFxn.c
exModA/
    __init__.pxd
    __init__.py
    exModAFxn.c
    exModA.pyx
    setup.py
# exModB attempts to use the cdef functions from exModA
exModB/
   __init__.py
   exModB.pyx
   setup.py
# The test script attempts to make use of exModB
test.py
.

exModA / __ __ INIT PXD:

cdef extern void myCMessage()
.

exModA / __ __ INIT ru:

from exModA import *

exModA / exModAFxn.c:

#include <stdio.h>
void myCMessage() { printf( "This is a test!\n" ); }

exModA / exModA.pyx:

cdef extern from "exModAFxn.c" :
    void myCMessage()
# Use myCMessage in a python function
def foo() :
    myCMessage()

exModA / setup.py:

from distutils.core import setup, Extension
from Cython.Build import cythonize
setup( 
    name = 'exModA',
    ext_modules = cythonize( ['exModA/exModA.pyx'] ),
)

exModB / __ __ INIT ru :.

from exModB import *

exModB / exModB.pyx:

cimport exModA
# Use a cdef function from exModA in a python function
def bar() :
    exModA.myCMessage()

exModB / setup.py:

from distutils.core import setup, Extension
from Cython.Build import cythonize
setup( 
    name = 'exModB',
    ext_modules = cythonize( ['exModB/exModB.pyx'] ),
)

After compiling the two cython modules, test.py script is called.

python extModA/setup.py build_ext --inplace
python extModB/setup.py build_ext --inplace

test.py:

import exModA
exModA.foo()    # successfully prints message
import exModB   # raises ImportError: undefined symbol: myCMessage
exModB.bar()    # we never get to this line :(

exModA.foo , myCMessage , , exModB . , , exModA exModB , , . C .

+4
1

, :

exModA exModB/setup.py :

from distutils.core import setup, Extension
from Cython.Build import cythonize

ext = Extension('exModB/exModB',
                sources=['exModB/exModB.pyx'],
                libraries=['exModA'],
                library_dirs=['/home/MyFolder/exModA'],
                runtime_library_dirs=['/home/MyFolder/exModA']
                )
setup(name='exModB', ext_modules = cythonize(ext))

( exModA.so .)

exModA.so libexModA.so, gcc . exModA.so, exModB.so:

python exModA/setup.py build_ext --inplace
cp exModA/exModA.so exModA/libexModA.so
python exModB/setup.py build_ext --inplace

, , , .

+2

Source: https://habr.com/ru/post/1623475/


All Articles