在C++库中嵌入Python

Embedding Python in C++ libraries

本文关键字:Python C++      更新时间:2023-10-16

我正在将Python嵌入一些C++代码中,但我在编译它时遇到了麻烦。

对于头文件,我有

#include <Python.h>

我会先试试,

$g++ EmbeddedPython.cpp

但最终会得到

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
....

然后我尝试了

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

这消除了前两个错误,但我最终还是使用了

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

我对此有点陌生,但我觉得我学得很快。我想我需要"链接"一个图书馆,对吧?但是是哪一个,如何?我需要一个动态的还是静态的?

我正在开发MacBook Pro。

您需要针对libpython进行链接。UNIX程序员使用链接命令中的"-lpyson"(即在"g++"命令的末尾(来完成此操作。在Mac上,我认为它应该是"-framework Python"。