0

I want to add python functions in C++ code.

I made a GUI in gtk (on the Raspberry PI) and now I want to work with a camera module which is easy to handle in python. (I want to start a video directly when I push a button.)

So I included the file Python.h

#include <python3.4m/Python.h>
#include <python3.4m/pythonrun.h>

then I thought it should work, but when I try to compile Py_Initialize()

I get the error:

undefined reference to Py_Initialize.

I think this is strange because, when I type in, there came the selection for Py_Initialize.

Cœur
  • 37,241
  • 25
  • 195
  • 267
hallochg
  • 1
  • 1
  • 1
  • 3
    "I think this is strange cause, when I type in, there came the selection for Py_Initialize." Your editor/IDE is *not* the same thing as the compiler that's actually building an executable. Don't mistake one for the other. – Jesper Juhl May 22 '17 at 22:03
  • Ahh ohh yes. Thank u! – hallochg May 23 '17 at 07:11

3 Answers3

0

In terms of headers you should be fine, since it compiled but failed at linking.

Now you need to link against the Python libraries. The way this is done largely depends on what toolchain you are using.

arboreal84
  • 2,086
  • 18
  • 21
  • I use codeblocks, (Project GTK) GCC compiler. But i don't know how to link the Python libraries? I tried it with pkg-config --cflag python and pkg-config --libs python – hallochg May 23 '17 at 07:06
  • 1
    Then add `-lpython3.4m` as your gcc parameter. – arboreal84 May 23 '17 at 07:07
  • How can i do this, sorrry Im a bit confused :// – hallochg May 23 '17 at 07:09
  • In "Project build options", "Compiler Settings" -> "Other Options". If not there try "Linker Settings" – arboreal84 May 23 '17 at 09:24
  • hmmm this doesnt work :/ I typed in "other Options" -lpython3.4m – hallochg May 23 '17 at 12:21
  • I'm building from command line using the following command: g++ -I/usr/include/python3.8/ *.cpp -o output -lpython3.8 Without -lpython3.8 I'm getting same error as original writer of this question. Can someone explain me why adding that works? I mean how should I have known that I need to type exactly that? – kivikall Nov 10 '20 at 20:08
  • @kivikall, see this: https://stackoverflow.com/questions/26562335/what-is-the-difference-between-include-and-link-when-linking-to-a-library – arboreal84 Dec 02 '20 at 04:21
0

Maybe you can see my answer in another question:

if with python 3.x installed, maybe this command can work:

g++ hw.cpp `/usr/bin/python3-config --cflags` `/usr/python3-config --ldflags`

By the way, you should check you gcc and python version.

As I know, if gcc version is 5.4 and python version is 3.7, it doesn't work.(python 3.5 >is work)

When you run /usr/bin/python3-config --cflags, in fact, it is the compile option.

LittleSec
  • 11
  • 2
0

Set the python include folder and it static lib on gcc command line and put the python dynamic lib on LD_LIBRARY_PATH. Before Py_Initialize(), do not forget to set python home with Py_SetPythonHome(). These steps must be sufficient for your code compile and run.

João Paulo
  • 6,300
  • 4
  • 51
  • 80