我正在尝试使用 python-dev api,但遇到了一些问题。
这是我要编译的代码
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <Python.h>
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
与
gcc -c test.c `python-config --cflags`
首先,我的 (OSX 10.9) 计算机上安装了 python 2.7.8。
当我运行 python-config --cflags
时,我得到了这个输出:
-I/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -I/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -g -O2 -DNDEBUG -g -O3
所以出于某种原因,即使我有 python 2.7.8,python-dev api 也是 python 2.6。我尝试重新安装 python,但这对我没有好处。
其次,出于某种原因,python 试图将 powerpc 和 i386 架构与 -arch ppc -arc i386
一起使用,但我用 python-config --cflags | 修复了这个问题sed -e 's/-arch\.*\//g
.
但是即使我成功地编译了我的代码,链接器仍然从 python-dev 获取奇怪的架构代码:
gcc test.o -o test `python-config --ldlags`
ld: warning: ignoring file /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config/libpython2.6.dylib, missing required architecture x86_64 in file /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config/libpython2.6.dylib (2 slices)
Undefined symbols for architecture x86_64:
"_PyRun_SimpleStringFlags", referenced from:
_main in test.o
"_Py_Finalize", referenced from:
_main in test.o
"_Py_Initialize", referenced from:
_main in test.o
"_Py_SetProgramName", referenced from:
_main in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
在Homebrew github page我发现了这个:
ld: warning: ignoring file /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config/libpython2.6.dylib, missing required architecture x86_64 in file /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config/libpython2.6.dylib (2 slices) It looks like you have a 32-bit version of Python installed - probably PowerPC/i386. Since you have a 64-bit computer, you need your default Python to have a 64-bit component too.
但他们从未解释过如何做到这一点。
如何让 API 正常工作?
更新 - 使用 cflags 编译去掉了被忽略的文件,但仍然给我 undefined symbol :
gcc test.o -o test `python-config --cflags --ldflags | sed -e 's/-arch\ .*\ //g' | sed -e 's/Versions\/2\.6/Versions\/Current/g'`
Undefined symbols for architecture x86_64:
"_PyRun_SimpleStringFlags", referenced from:
_main in test.o
"_Py_Finalize", referenced from:
_main in test.o
"_Py_Initialize", referenced from:
_main in test.o
"_Py_SetProgramName", referenced from:
_main in test.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
更新 - 问题是 python 安装在我机器的 /usr/local/Cellar/Frameworks
中,但出于某种原因 python-config 仍然认为它在 /库/框架
。
所以现在真正的问题是:如何更改 python-config 的路径?
我的 PYTHONPATH env 变量是正确的,所以我不知道为什么它没有改变。
最佳答案
您只传递了编译器标志。也传递链接器标志。否则,将不会链接库符号:
gcc -c test.c `python-config --cflags --ldflags`
为了解决 undefined symbol 问题,python-config 可能使用了错误的解释器。在 which python-config
打开 python-config(它是一个常规的 python 文件)并检查第一行是否有类似这样的内容 #!/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
如果它没有指向正确的解释器,请将其更改为正确的路径。
关于 python 开发: how to change library architecture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27165877/