Search code examples
c++pythonlinkerembedding

Embedding Python in C++ libraries


I'm working on embedding Python in some C++ code, but I'm getting stuck compiling it.

For a header file, I have

#include <Python.h>

I would initial try,

$g++ EmbeddedPython.cpp

but would end up getting

EmbeddedPython.cpp:1:20: error: Python.h: No such file or directory
EmbeddedPython.cpp: In function ‘int main(int, char**)’:
EmbeddedPython.cpp:6: error: ‘Py_Initialize’ was not declared in this scope
....

I then tried

g++ EmbeddedPython.cpp -I/System/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5

and that got rid of the first two errors, but I still ended up with

 Undefined symbols:
  "_Py_Initialize", referenced from:
  _main in ccxJAUAB.o

I'm a bit of new to this, but I think I'm learning fast. I believe I need to 'Link' a library, right? But which one and how? Do I need a dynamic or a static one?

I am working on a MacBook Pro.


Solution

  • You need to link against libpython. UNIX programmers do this with "-lpython" in the link command (ie at the end of that "g++" command). On a Mac, I think it would be "-framework Python".