【发布时间】:2017-11-03 02:37:41
【问题描述】:
我正在尝试将 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. Embedding Python in Another Application - 1.6 Compiling and Linking under Unix-like systems 和此处:Python/C API Reference Manual - Introduction 为了编译,我必须向编译器和链接器添加一些额外的标志。
为了找出我应该添加哪些标志,我在终端中运行了以下两个命令(其中包含相应的输出):
$ 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
【问题讨论】:
标签: python c++ xcode cflags ldflags