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()
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
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 .