[英]python-dev: how to change library architecture
我正在尝试使用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-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页面中,我发现了这一点:
ld:警告:忽略文件/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config/libpython2.6.dylib,文件/Library/Frameworks/Python.framework/Versions/中缺少必需的架构x86_64 2.6 / lib / python2.6 / config / libpython2.6.dylib(2个切片)看起来您已经安装了32位版本的Python-可能是PowerPC / i386。 由于您拥有64位计算机,因此您还需要默认的Python才能具有64位组件。
但是他们从未解释过如何做到这一点。
如何使API正常工作?
更新 -使用cflags进行编译摆脱了被忽略的文件,但仍然给了我未定义的符号:
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仍然认为它在/Library/Frameworks
。 因此,现在真正的问题是:如何更改python-config的路径? 我的PYTHONPATH env变量是正确的,所以我不知道为什么未更改它。
您只传递了编译器标志。 也传递链接器标志。 否则,将不会链接库符号:
gcc -c test.c `python-config --cflags --ldflags`
为了解决未定义符号的问题,python-config可能使用了错误的解释器。 打开python-config(这是一个常规的python文件), which python-config
在which python-config
的第一行中检查类似#!/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6
如果没有)指向正确的解释器,将其更改为正确的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.