Just declare the stuff you want to call in c/c++ as cdef public
for example:
# cymod.pyx
from datetime import datetime
cdef public void print_time():
print(datetime.now().ctime())
When cythonizing cymod.pyx
to cymod.c
, a cymod.h
will be generated as well.
Then make a library, eg: cymod.lib
(on windows).
In the c codes(main.c):
#include "Python.h"
#include "cymod.h"
int main(int argc, char **argv)
{
Py_Initialize();
PyInit_cymod(); // in cymod.h
print_time(); // call the function from cython
Py_Finalize();
return 0;
}
Compile and run(main.exe)
Note: main.exe is highly bound to the python environments, one may run into errors such as cannot find pythonxx.dll
, Fatal Python error: Py_Initialize: unable to load the file system codec
. There are many solutions on this site.