I ran into a similar problem. This is not exactly the same problem, but it may be related.
I posted my question here: Distributing exceptions through dlsym cython . The part you are interested in is the keyword "public":
#hello.pyx cdef public say_hello(): print "Hello World!"
This will create a function like
Edit: I added a working temp.cpp:
#include "Python.h" #include <iostream> #include "hello.h" using namespace std; int main(){ Py_Initialize(); inithello(); say_hello(); Py_Finalize(); return 1; };
Compilation is done with
g++ -I/usr/include/python2.6/ -lpython2.6 -L./ -lhello temp.cpp -c -o temp.o g++ temp.o -L. -lhello -lpython2.6 -o temp
(Interestingly, it will not link to one step, complaining about undefined links). This will successfully print "Hello world" after execution.
Note. The parameters Py_Initialize () and inithello () are necessary, otherwise your code will work. I could not get it to work without including "Python.h" and without initialization parts (ie Using only extern "C" {void sayhello ();}, as you mention). This fails when linking. The solution could be to use dlsym and dynamically load your function, as I will demonstrate in my question. But there is probably another solution where you try to successfully export this method (to the hello.h header): __PYX_EXTERN_C DL_IMPORT (int) say_hello (void);
source share