【问题标题】:How to embed particular version of python interpreter in c in OS X如何在 OS X 的 c 中嵌入特定版本的 python 解释器
【发布时间】:2014-07-19 10:16:54
【问题描述】:

我想在C中嵌入python。但是我发现嵌入在我的程序中的python解释器的版本是2.7(mac上的默认版本)。

当我在 mac os x 中编译 c 代码时,如何指定特定版本的 python 解释器。 os x 中的 gcc 肯定与 linux 中的不同。

我已经通过 HomeBrew 安装了 python3。

非常感谢。


更新: 我尝试使用python3.4-config --cflagspython3.4-config --ldflags 来找出所需的编译器和链接器标志。然后我在编译和链接时得到这些推荐的标志:

-I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m -I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m -Wno-unused-result -Werror=declaration-after-statement -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/sqlite/include

-L/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/config-3.4m -ldl -framework CoreFoundation -lpython3.4m

之后,我将这些标志与源文件一起组装到 gcc 中,并得到一个错误:

Undefined symbols for architecture x86_64:
  "_PyUnicodeUCS2_FromString", referenced from:
      _main in py2-5d8da5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我这里测试的C代码来自Python Documentation

【问题讨论】:

  • 您需要指定自定义链接器和编译器标志。例如:-L and -I 到您拥有的 Python3 安装。
  • 能给我举个例子吗?我是 python 新手。
  • 只有当你展示你现在如何编译你的源代码时。
  • 我使用gcc source.c -framework Python 命令。我试过gcc source.c -framework Python3 但失败了。
  • 除非您将 Python3 构建为框架,否则您将需要使用 gcc -L/path/to/python3/lib -I/path/to/python3/include -lpython source.c

标签: python c macos


【解决方案1】:

尝试在 OSX 上完成本教程时,我遇到了同样的错误。您不需要配置实用程序吐出的所有标志。如果你只是在做嵌入教程,你肯定不需要 corefoundation 框架。只需使用包含目录的标题:

-I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m -I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m

,以及要链接到的库:

-L/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/config-3.4m -lpython3.4m

所以这里是编译和链接的单行代码:

gcc -I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m -I/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/include/python3.4m -L/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/config-3.4m -lpython3.4m /path/to/main.c -o /path/to/output/executable

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2015-11-13
    • 2014-09-19
    • 2012-03-17
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多