[英]C++ Compiling with Python.h Undefined Symbols
So, I've been trying to start using Python.h for a little project I want to work on that seems pretty /simple/. 所以,我一直在尝试开始使用Python.h来处理我想要处理的一个小项目,看起来非常/简单/。 But before I start I want to try to learn how to use Python.h.
但在开始之前,我想尝试学习如何使用Python.h。 So I found this little example online.
所以我在网上找到了这个小例子。
#include "Python/Python.h"
int main(int argc, char** argv)
{
Py_Initialize();
PyRun_SimpleString("print 'Test'");
PyRun_SimpleString("print str(3 + 5)");
Py_Exit(0);
}
Seems pretty straight forward. 似乎很直接。 When i first used
我第一次使用的时候
gcc test.cpp
to compile, i got some undefined symbols. 为了编译,我得到了一些未定义的符号。 I quickly found out I should use
我很快发现我应该使用
-lpython2.7
then I found out I could also use 然后我发现我也可以使用
-L/Library/Frameworks/Python.framework/Versions/2.7/lib/
that didn't work (I made sure that /Library/Frameworks/Python/Versions/2.7/lib/ existed) I'm stuck, what do I do? 这不起作用(我确保/Library/Frameworks/Python/Versions/2.7/lib/存在)我被卡住了,我该怎么办? I get
我明白了
Undefined symbols:
"_Py_Initialize", referenced from:
_main in ccoUOSlc.o
"_PyRun_SimpleStringFlags", referenced from:
_main in ccoUOSlc.o
_main in ccoUOSlc.o
"___gxx_personality_v0", referenced from:
_main in ccoUOSlc.o
CIE in ccoUOSlc.o
"_Py_Exit", referenced from:
_main in ccoUOSlc.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
EDIT: I just tried using the -Framework argument, and tried adding after the -L the -l python2.7 argument, and I now get 编辑:我刚尝试使用-Framework参数,并尝试在-L -l python2.7参数后添加,现在我得到了
Undefined symbols:
"___gxx_personality_v0", referenced from:
_main in ccfvtJ4j.o
CIE in ccfvtJ4j.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Now what? 怎么办?
If you are using an Python framework installation on OS X as it appears you are based on the paths, you can use the -framework
argument to the Apple compiler drivers: 如果您在OS X上使用Python框架安装,因为它看起来基于路径,您可以使用Apple编译器驱动程序的
-framework
参数:
cc test.cpp -framework Python
Alternatively, you can explicitly specify the directory path and library name: 或者,您可以显式指定目录路径和库名称:
cc test.cpp -L /Library/Frameworks/Python.framework/Versions/2.7/lib/ -l python2.7
Update: With the configuration you report in the comments ( Xcode 3.2.6
, gcc-4.2
), it appears you need to explicitly invoke the c++
variant of gcc
. 更新:使用您在注释中报告的配置(
Xcode 3.2.6
, gcc-4.2
),您似乎需要显式调用gcc
的c++
变体。 Either: 或者:
g++ test.cpp -framework Python
or 要么
c++ test.cpp -framework Python
should work. 应该管用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.