Skip to content Skip to sidebar Skip to footer

Embed Python Function In C++

I am experimenting with Cython to generate c code from python but there seems to be some issues with name mangling. I first generate convert the code from python to c code and then

Solution 1:

You're not going to be able to get the interoperation you want that way. If you open and inspect hello.c you won't find "static int say_hello" anywhere in there. Cython is designed for letting Python use C libraries, not letting C libraries use python.

You can look here in the documentation, but unfortunately this support is still for a python interpreter that is "in charge" and what you're looking for is the other way around.

http://docs.python.org/release/2.5.4/ext/callingPython.html

There's also the primer on "Embedding Python in Another Application"

http://docs.python.org/2/extending/embedding.html

I don't know what your requirements are, but in some cases you can successfully write data to a file, call a Python program to chew on it, then parse the results from another file. It's a little ugly and slower than keeping things in memory but it's entirely workable in many situations.

Solution 2:

I encountered a similar problem. It's not exactly the same problem, but it might be related.

I posted my question here: Propagating exceptions through dlsym cython. The part that is interesting for you is the 'public' keyword:

#hello.pyx
cdef public say_hello():
    print"Hello World!"

That will create a function like this

# (in the generated C file hello.c)
__PYX_EXTERN_C DL_IMPORT(...) say_hello(...);

Edit: I have added a working temp.cpp:

#include"Python.h"#include<iostream>#include"hello.h"usingnamespace std;

intmain(){
    Py_Initialize();
    inithello();
    say_hello();
    Py_Finalize();
    return1;
};

Compiling 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 in one step, complaining about undefined references.) This will successfully print 'Hello world' upon execution.

Note: The Py_Initialize() and inithello() are necessary, otherwise your code will crash. I haven't been able to get it to work without including "Python.h" and without the initialization parts (i.e. using only extern "C" { void sayhello(); } as you mention). It fails at linking. The solution can be to use dlsym and dynamically load your function, as I demonstrate in my question. But probably another solution exists, where you try to successfully export this method (in the hello.h header): __PYX_EXTERN_C DL_IMPORT(int) say_hello(void);

Solution 3:

If you have CMake I suggest to take a look to my project, where I use CMake to generate and link Cython based files

https://github.com/CarloNicolini/cymake

You probably have to edit some CMakeLists.txt to find the right cython installation

Post a Comment for "Embed Python Function In C++"