【发布时间】:2014-11-27 08:09:12
【问题描述】:
我正在尝试使用 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:警告:忽略文件 /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config/libpython2.6.dylib, 文件中缺少所需的架构 x86_64 /Library/Frameworks/Python.framework/Versions/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)
更新 - 问题是我的机器上的/usr/local/Cellar/Frameworks 中安装了python,但由于某种原因python-config 仍然认为它在/Library/Frameworks 中。
所以现在真正的问题是:如何更改 python-config 的路径?
我的 PYTHONPATH 环境变量是正确的,所以我不知道为什么它没有被改变。
【问题讨论】:
标签: python c python-2.7