在 C 中嵌入 Python图书馆
我正在努力将 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 工作。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要链接 libpython。 UNIX 程序员在链接命令中使用“-lpython”(即在“g++”命令的末尾)来执行此操作。在 Mac 上,我认为它会是“-framework Python”。
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".