我正在尝试将 python 嵌入到我的 C++ 项目中。我需要这样做才能使用一些在 C++ 中不可用的实现 Kolmogorov-Smirnov 测试的函数。
现在我只是想看看 Xcode 是否能够链接和编译一个嵌入 Python 的简单程序。我试图编译的代码如下:
#include<Python/Python.h>
int main(int argc, const char * argv[]) {
Py_Initialize();
PyObject* variable;
Py_Finalize();
return 0;
}
据我在此处阅读的说明所理解的:1. 在另一个应用程序中嵌入 Python - 1.6 在类 Unix 系统下编译和链接以及此处:Python/C API 参考手册 -为了编译我必须向编译器和链接器添加一些额外的标志。
为了找出应该添加哪些标志,我在终端中运行了以下两个命令(其中包含相应的输出):
$ python3.6-config --cflags
-I/Users/user/anaconda3/include/python3.6m -I/Users/user/anaconda3/include/python3.6m -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/user/anaconda3/include -arch x86_64 -I/Users/user/anaconda3/include -arch x86_64
$ python3.6-config --ldflags
-lpython3.6m -ldl -framework CoreFoundation -Wl,-stack_size,1000000 -framework CoreFoundation
我将用户文件夹的实际名称替换为user
.
现在,为了将这些标志添加到 Xcode 编译器和链接器,我进入了我的项目设置窗口,并在下面Build Settings -> Other C Flags
添加Build Settings -> Other Linker Flags
了我上面报告的标志。
但是当我编译我得到这个错误:
Apple Mach-O Linker (ld) Error Group
clang: error: linker command failed with exit code 1 (use -v to see invocation)
即使我注释了main
函数中除return 0
.
我不明白我做错了什么。
我正在使用Xcode 8.3.2
,我的 Python 发行版是:Python 3.6.1 |Anaconda 4.4.0