Insert python function in C ++

I am experimenting with Cython to generate c-code from python, but there seem to be some problems with the name change. First I generated a code conversion from python code to c, and then compiled the code using gcc to .so. The reason I want to use cython instead of the C / python API is because I will use this later on more complex classes that I would like to use as a library for speed, etc. Later (I have a lot of problems finding people who go from python to C ++, since this is usually the opposite). Below is all the code that I should try to execute the code (but it fails). Any input would be appreciated. Thanks!

#hello.pyx def say_hello(): print "Hello World!" #generate the c code cython -a hello.pyx #creates the shared library gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.6 -o libhello.so hello.c //temp.cpp #include <iostream> extern "C" { void say_hello(); }; using namespace std; int main(){ say_hello(); return 1; }; #attempt to compile (this is where it fails) g++ -I/usr/include/python2.6/ -lpython2.6 -L./ -lhello temp.cpp -o temp 

Here is the error message:

 /tmp/ccpKHOMl.o: In function main: temp.cpp:(.text+0x5): undefined reference to say_hello' /tmp/ccpKHOMl.o: In function __static_initialization_and_destruction_0(int, int): temp.cpp:(.text+0x33): undefined reference to std::ios_base::Init::Init() temp.cpp:(.text+0x38): undefined reference to std::ios_base::Init::~Init() collect2: ld returned 1 exit status 
+4
source share
3 answers

You cannot get the interaction you want in this way. If you open and check hello.c, you will not find "static int say_hello" anywhere. Cython is designed to allow Python to use C libraries, while not allowing C libraries to use python.

You can look here in the documentation, but unfortunately, this support still exists for the python interpreter, which is the "responsible", and what you are looking for is the other way around.

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

There is also a 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, and then parse the results from another file. This is a little ugly and slower than storing things in memory, but it is fully functional in many situations.

+3
source

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

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

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);

+1
source

If you have CMake, I suggest taking a look at my project, where I use CMake to create and link to Cython-based files.

https://github.com/CarloNicolini/cymake

You may have to edit the CMakeLists.txt file to find the correct cython installation

0
source

All Articles