Could you tell me what language are you going to use for your "main" application? I mean, are you coding in Python, and then want to reuse the Fortran library? Or perhaps you are coding in C/Fortran, but want to be able to write your functions in Python? Depending on that, the approach is quite different (at least from my POV).
On Wed, Aug 5, 2009 at 10:47 AM, Jon Olav Vik<[email protected]> wrote: > 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 > -- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594 _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
