Jon Olav Vik <jono...@...> writes: > Background: I'd like to compute second derivatives of a Python-implemented > function with the help of a Fortran library called NDL [1]. The function to be > differentiated can be implemented in C (an "external f" statement in the main > Fortran program will make available the functions in the "f.o" object file). > Now, I'm hoping to generate the requisite C code from a thin Cython wrapper > around my function, as in [2]. > > As a first step, I have declared a Cython function "cdef public", compiled it > (command-line output of compilation follows at the end), and am trying to call > it from C. However, the compilation fails at the line '#include "fun.h"' and I > don't understand why. [...]
Maybe a step in a relevant direction: While Googling for examples of how to call Cython code from pure C, I stumbled across this posting: http://article.gmane.org/gmane.comp.python.cython.devel/5515 It describes how to "embed the interpreter and statically link your module", and allows a C program to run the main() function of a Cython module called mylib.pyx. In deciding whether to spend more time on this, it would be nice to know: 1) Is this how I need to proceed to call a Cython function from a C program? 2) Can a function implemented in Cython be used as a callback for a C or Fortran optimization or differentiation routine? 3) If 2), can I have once-only initialization in the Cython module that's not repeated with every function evaluation? Best regards, Jon Olav PS. I tried compiling the example in the other post (the C program is quoted below), and didn't quite get there. Details follow in case anyone is interested. After adjusting the -I (include dir) parameter to my system, the first few lines of compilation went okay: <quote> cython mylib.pyx gcc -fPIC -g -O2 -I /usr/include/python2.5 -c -o main.o main.c gcc -fPIC -g -O2 -I /usr/include/python2.5 -c -o mylib.o mylib.c </quote> However, the final step failed: gcc -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions \ main.o mylib.o /usr/lib/python2.5/config/libpython2.5.a \ -lm -ldl -pthread -lutil \ -o main I changed the path to libpython2.5.a and added the -I switch, but got /usr/bin/ld: unrecognized option '-Bsymbolic-functions' /usr/bin/ld: use the --help option for usage information collect2: ld returned 1 exit status Removing the -Bsymbolic-functions or replacing it with -Bsymbolic gave the error: /xanadu/site/common/VERSIONS/compython-2.5/Linux/lib/python2.5/config/ libpython2.5.a(posixmodule.o): In function `posix_tmpnam': /site/VERSIONS/compython-2.5/src/python/Python-2.5.2/./Modules/ posixmodule.c:6862: warning: the use of `tmpnam_r' is dangerous, better use `mkstemp' /xanadu/site/common/VERSIONS/compython-2.5/Linux/lib/python2.5/config/ libpython2.5.a(posixmodule.o): In function `posix_tempnam': /site/VERSIONS/compython-2.5/src/python/Python-2.5.2/./Modules/ posixmodule.c:6817: warning: the use of `tempnam' is dangerous, better use `mkstemp' /xanadu/site/common/VERSIONS/compython-2.5/Linux/lib/python2.5/config/ libpython2.5.a(zlibmodule.o): In function `PyZlib_compress': /site/VERSIONS/compython-2.5/src/python/Python-2.5.2/./Modules/ zlibmodule.c:146: undefined reference to `deflateInit_' etc etc <quote> ----------------------------------- main.c --------------------------- #include <Python.h> // For each Cython module you want to embed, you must declare an // init<module> function, like so: PyMODINIT_FUNC initmylib(void); int main(int argc, char *argv[]) { // The first step is to set up the Python interpreter: Py_Initialize(); PySys_SetArgv(argc, argv); // Next, we need to tell Python that our module exists. Call each // of the functions you declared above. initmylib(); // Now do some Python stuff. The easiest thing to do is to give // the interpreter a string of Python code that imports your // module and calls it. PyRun_SimpleString("from mylib import main\n" "main()\n"); // When we're done, tell Python to clean up. Py_Finalize(); return 0; } </quote> _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
