【问题标题】:Set Xcode flags in order to embed python in my C++ project设置 Xcode 标志以便在我的 C++ 项目中嵌入 python
【发布时间】: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 -&gt; Other C FlagsBuild Settings -&gt; 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


    【解决方案1】:

    好的,我想我找到了解决方案。不确定它是否真的是正确的(因为我不是专家,我也不太明白现在它起作用的原因),但我还是要发布它。

    问题是,如果您查看此页面中的说明:1. Embedding Python in Another Application - 1.6. Compiling and Linking under Unix-like systems,您会看到在执行命令 python3.6-config --ldflags 时,输出包含一个标志 -L/opt/lib/python3.4/config-3.4m,而在我的情况下它没有。

    所以在弄清楚给我链接错误的标志是-lpython3.6m 之后,我想可能是因为链接器找不到我的 python 分发所在的目录或类似的东西.

    因此,我在硬盘驱动器中查找了一个文件夹的路径,该文件夹的名称与网页上显示的名称相似,我发现/Users/user/anaconda3/lib/python3.6/config-3.6m-darwin 是一个名称与我非常相似的文件夹的路径正在寻找,除了文件夹名称末尾的“-darwin”可能存在,因为我使用的是 macOS 发行版(也许这就是命令 python3.6-config --ldflags 没有找到它的原因?我不知道)。

    在此之后,我刚刚在 Xcode 的“Other Liker Flags”的开头添加了-L/Users/user/anaconda3/lib/python3.6/config-3.6m-darwin,一切(几乎)都编译得很好。

    我说“几乎”是因为我还必须将代码开头的 include#include&lt;Python/Python.h&gt; 重命名为 #include&lt;Python.h&gt;(可能是因为通过使用第一个,我包含了系统默认的 Python 发行版,而第二个我包括 Anaconda 一个?再说一次,我不知道)。

    无论如何,现在一切都编译得很好,所以我想分享一下我是如何做到的,以防其他人处于同样的情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 2017-03-10
      相关资源
      最近更新 更多