【发布时间】: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 --cflags 和python3.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