我想在C中嵌入
python但是我发现嵌入在我的程序中的python解释器版本是2.7(mac上的默认版本).
当我在mac os x中编译c代码时,如何指定特定版本的python解释器. os x中的gcc与linux中的gcc完全不同.
我已经通过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
解决方法
尝试在OSX上执行本教程时遇到了同样的错误.您不需要config实用程序吐出的所有标志.如果您只是在进行嵌入教程,那么您绝对不需要corefoundation框架.只需使用头文件的include目录:
-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