I wrote a minimal C function (with accompanying header file) with the goal of creating a Cython wrapper that would enable the C code's use from a Python file somewhere else.
My files are as such:
C file:
/* engine.c */
#include <stdio.h>
#include "customlib.h"
int engine(int value, int iterations) {
/* declare iteration index */
int idx;
for (idx = 0; idx < iterations; idx = idx + 1) {
value = value * 3;
}
/* return value that should be accessible by the Python module */
return value;
}
C header file:
/* customlib.h */
#ifndef CUSTOM_HEADER_H
#define CUSTOM_HEADER_H
/* engine function */
int engine(int value, int iterations);
#endif
Cython module that wraps the C code:
# wrapper.pyx
cdef extern from "customlib.h":
int engine(int value, int iterations)
def call_c_code(value, iterations):
output_value = engine(value, iterations)
print(output_value)
Python module that calls the C code via the Cython wrapper:
# caller.py
import wrapper
wrapper.call_c_code(1, 2) /* values not important; just an example */
Setup code for generating the *.so from the *.pyx file:
# setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
# definitions
sourcefiles = ['engine.c']
extensions = [Extension('wrapper', sourcefiles)]
# setup function
setup(name='wrapper', ext_modules=cythonize('wrapper.pyx'))
THE PROBLEM: The shared object (*.so) seems to compile without any issues. However, even just importing the wrapper.py throws the following error:
Traceback (most recent call last):
File "caller.py", line 10, in <module>
import wrapper
ImportError: /home/alex/Documents/Projects/Cython/wrapper.cpython-36m-x86_64-linux-gnu.so: undefined symbol: engine
undefined symbolmessage. - Andrej Kesely