因此,我一直在尝试将Python.h用于一个我想从事的小项目,看起来很/ simple /。但是在我开始之前,我想尝试学习如何使用Python.h。
因此,我在网上找到了这个小例子。

#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);
}

似乎很简单。当我第一次使用
gcc test.cpp

进行编译,我得到了一些 undefined symbol 。我很快发现我应该使用
-lpython2.7

然后我发现我也可以使用
-L/Library/Frameworks/Python.framework/Versions/2.7/lib/

那行不通(我确保存在/Library/Frameworks/Python/Versions/2.7/lib/)
我被卡住了,我该怎么办?
我懂了
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

编辑:
我只是尝试使用-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

怎么办?

最佳答案

如果您正在OS X上使用Python框架安装(看起来是基于路径),则可以对Apple编译器驱动程序使用-framework参数:

cc test.cpp -framework Python

或者,您可以显式指定目录路径和库名称:
cc test.cpp -L /Library/Frameworks/Python.framework/Versions/2.7/lib/ -l python2.7

更新:使用您在注释中报告的配置(Xcode 3.2.6gcc-4.2),您似乎需要显式调用c++gcc变体。要么:
g++ test.cpp -framework Python

要么
c++ test.cpp -framework Python

应该管用。

关于c++ - 使用Python.h进行C++编译的 undefined symbol ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13115988/

10-14 18:47
查看更多