Trying to understand the binding procedure for writing a Python / C ++ hybrid

I want to start learning more about using SWIG and other methods for the Python and C ++ interface. To get started, I wanted to compile this simple program mentioned in another post :

#include <Python.h> int main() { Py_Initialize(); PyRun_SimpleString ("import sys; sys.path.insert(0, '/home/ely/Desktop/Python/C-Python/')"); PyObject* pModule = NULL; PyObject* pFunc = NULL; pModule = PyImport_ImportModule("hello"); if(pModule == NULL){ printf("Error importing module."); exit(-1); } pFunc = PyObject_GetAttrString(pModule, "Hello"); PyEval_CallObject(pFunc, NULL); Py_Finalize(); return 0; } 

where the file "hello.py" has only the contents:

  def Hello(): print "Hello world!" 

Note. I already have the python2.7-dev and python-dev and libboost-python-dev files installed. But when I am going to compile the code, I get errors that, in my opinion, are due to incorrect binding to Python libraries.

 ely@AMDESK :~/Desktop/Python/C-Python$ gcc -I/usr/include/python2.7 test.cpp /tmp/ccVnzwDp.o: In function `main': test.cpp:(.text+0x9): undefined reference to `Py_Initialize' test.cpp:(.text+0x23): undefined reference to `PyImport_ImportModule' test.cpp:(.text+0x58): undefined reference to `PyObject_GetAttrString' test.cpp:(.text+0x72): undefined reference to `PyEval_CallObjectWithKeywords' test.cpp:(.text+0x77): undefined reference to `Py_Finalize' /tmp/ccVnzwDp.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status 

I searched for examples of this online and I found the following syntax that forces the code to compile into an object file, but then I cannot actually execute the file.

 ely@AMDESK :~/Desktop/Python/C-Python$ gcc -c -g -I/usr/include/python2.7 test.cpp ely@AMDESK :~/Desktop/Python/C-Python$ ./test.o bash: ./test.o: Permission denied ely@AMDESK :~/Desktop/Python/C-Python$ chmod ug=rx ./test.o ely@AMDESK :~/Desktop/Python/C-Python$ ./test.o bash: ./test.o: cannot execute binary file ely@AMDESK :~/Desktop/Python/C-Python$ sudo chmod ug=rx ./test.o ely@AMDESK :~/Desktop/Python/C-Python$ ./test.o bash: ./test.o: cannot execute binary file 

The same behavior as above is still visible if I use g++ instead of gcc .

Helping to understand my linking error would be great and even better for any explanation that would help me understand the β€œlogic” behind what I need to do so that I can better remember which things I can forget next time. Thanks!

+6
source share
3 answers

What you see are linker errors. To fix this, you need to link the python2.7 library.

Try the following line:

 gcc -I/usr/include/python2.7 test.c -lpython2.7 

he should work.

+8
source

Do you compile C or C ++ code first?

Use gcc for the former and g++ for the latter. C ++ code requires additional binding.

Secondly: you need to link your program with libpython2.7.so in order to insert an interpreter into it. To do this, add -lpython2.7 to the gcc command line.

+1
source

test.o not your executable file, so you cannot execute it.

By default, a.out specified for your program, try running this. You can specify a name for your program using the -o option.

+1
source

Source: https://habr.com/ru/post/911428/


All Articles