【问题标题】:python-dev: how to change library architecturepython-dev:如何更改库架构
【发布时间】: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


    【解决方案1】:

    您只是传递编译器标志。也传递链接器标志。否则,库符号将不会被链接:

    gcc -c test.c `python-config --cflags --ldflags`
    

    为了解决未定义符号问题,python-config 可能使用了错误的解释器。在which python-config 打开python-config(它是一个常规的python 文件)并检查第一行是否有类似#!/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 的内容 如果它没有指向正确的解释器,请将其更改为正确的路径。

    【讨论】:

    • 没有更多被忽略的文件,但仍然出现丢失符号错误
    • 我不知道python-dev是否安装正确。您不需要进行 sed 替换。如果python-config 给出了错误的架构,那么它主要是一个安装问题。可以安装与python相同版本的python-dev吗?
    • 发现问题。 python-config 设置为/Library/....,而不是实际安装python 的/usr/local/Cellar/....。我该如何改变呢?
    猜你喜欢
    • 2015-08-06
    • 1970-01-01
    • 2014-11-03
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2017-02-21
    相关资源
    最近更新 更多