【问题标题】:Python build using wrong version of GCC on OS X在 OS X 上使用错误版本的 GCC 构建 Python
【发布时间】:2011-08-22 02:26:11
【问题描述】:

我正在尝试构建 python 包 pycrypto。 OS X 安装了 gcc-4.2 而不是 gcc-4.0,但 python 继续尝试使用 gcc-4.0。我怎样才能让它使用 gcc-4.2?或者我应该以不同的方式解决这个问题。

我收到以下错误:

bash-3.2$ 
bash-3.2$ sudo python setup.py build
running build
running build_py
running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c src/MD2.c -o build/temp.macosx-10.3-fat-2.6/src/MD2.o
unable to execute gcc-4.0: No such file or directory
error: command 'gcc-4.0' failed with exit status 1
bash-3.2$ 
bash-3.2$ 
bash-3.2$ 

我正在使用 Mac OS X 10.6.7 和 python 2.6.6 并安装了 XCode。

编辑:如果我添加 CC=gcc-4.2,那么我仍然会收到错误:

bash-3.2$ 
bash-3.2$ export CC=gcc-4.2
bash-3.2$ python setup.py build
running build
running build_py
running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -std=c99 -O3 -fomit-frame-pointer -arch i386 -arch x86_64 -Isrc/ -I/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c src/MD2.c -o build/temp.macosx-10.3-intel-2.6/src/MD2.o
gcc-4.0 -g -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 build/temp.macosx-10.3-intel-2.6/src/MD2.o -o build/lib.macosx-10.3-intel-2.6/Crypto/Hash/MD2.so
unable to execute gcc-4.0: No such file or directory
error: command 'gcc-4.0' failed with exit status 1
bash-3.2$ 

编辑:似乎在这里使用 sudo 会有所不同。

我尝试按照 Adam 的建议同时使用 CC 和 CXX,但没有 sudo 时出现以下错误:

bash-3.2$ python setup.py build
running build
running build_py
creating build/lib.macosx-10.3-fat-2.6
creating build/lib.macosx-10.3-fat-2.6/Crypto
copying lib/Crypto/__init__.py -> build/lib.macosx-10.3-fat-2.6/Crypto
copying lib/Crypto/pct_warnings.py -> build/lib.macosx-10.3-fat-2.6/Crypto
...
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
creating build/temp.macosx-10.3-fat-2.6
creating build/temp.macosx-10.3-fat-2.6/src
gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c src/MD2.c -o build/temp.macosx-10.3-fat-2.6/src/MD2.o
/usr/libexec/gcc/powerpc-apple-darwin10/4.2.1/as: assembler (/usr/bin/../libexec/gcc/darwin/ppc/as or /usr/bin/../local/libexec/gcc/darwin/ppc/as) for architecture ppc not installed
Installed assemblers are:
/usr/bin/../libexec/gcc/darwin/x86_64/as for architecture x86_64
/usr/bin/../libexec/gcc/darwin/i386/as for architecture i386
lipo: can't open input file: /var/tmp//ccxan625.out (No such file or directory)
error: command 'gcc-4.2' failed with exit status 1

如果我使用 sudo,我会在尝试使用 4.0 时收到以下错误:

bash-3.2$ sudo python setup.py build
Password:
running build
running build_py
running build_ext
warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
building 'Crypto.Hash.MD2' extension
gcc-4.0 -fno-strict-aliasing -fno-common -dynamic -arch ppc -arch i386 -std=c99 -O3 -fomit-frame-pointer -Isrc/ -I/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c src/MD2.c -o build/temp.macosx-10.3-fat-2.6/src/MD2.o
unable to execute gcc-4.0: No such file or directory
error: command 'gcc-4.0' failed with exit status 1
bash-3.2$ 
bash-3.2$ 

这些额外的信息是否让正在发生的事情以及如何解决它变得更加明显?知道为什么没有 sudo 的调用会出现其他错误吗?

【问题讨论】:

    标签: python macos gcc


    【解决方案1】:

    根据显示的路径 (/Library/Frameworks/Python.framework/Versions/2.6),您似乎已经安装了仅 32 位的 Python 2.6,可能使用了 python.org 安装程序。当您构建包含 C 扩展模块的 Python 包时,该 Python 实例中包含的 Python Distutils 将尝试使用与构建 Python 时相同的 gcc 版本和相同的 CPU 架构。显然您已经安装了新的尖端 Xcode 4,它不再包含 gcc-4.0 或 ppc 支持。您使用的 Python 版本是使用 Mac OS X 10.6 附带的 Xcode 3 工具构建的。您也许可以通过以下方式覆盖编译器选择来解决它:

    export CC=gcc-4.2
    python setup.py build
    sudo python setup.py install
    

    编辑:

    看起来这不适用于pycrypto;它的构建太复杂了。如果您不介意在 OS X 10.6 中使用 Apple 提供的 Python 2.6,这应该可以工作:

    export ARCHFLAGS='-arch i386 -arch x86_64'
    /usr/bin/python2.6 setup.py build
    

    另一种选择是安装64-bit/32-bit Python 2.7 installer from python.org。它是使用 gcc-4.2 构建的,并且仅适用于 Intel,因此在 Xcode 4 中使用它时应该没有任何问题。

    更新:

    以下是我在 Xcode 3 中使用的确切步骤。它们应该在安装了 Xcode 4 时也能正常工作:

    $ mkdir p
    $ cd p
    $ curl -O http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.3.tar.gz
    $ tar xf pycrypto-2.3.tar.gz 
    $ cd pycrypto-2.3/
    $ export ARCHFLAGS='-arch i386 -arch x86_64'
    $ /usr/bin/python2.6 setup.py build
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.6-universal-2.6
    creating build/lib.macosx-10.6-universal-2.6/Crypto
    copying lib/Crypto/__init__.py -> build/lib.macosx-10.6-universal-2.6/Crypto
    [...]
    running build_ext
    warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
    building 'Crypto.Hash.MD2' extension
    creating build/temp.macosx-10.6-universal-2.6
    creating build/temp.macosx-10.6-universal-2.6/src
    gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -fwrapv -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -std=c99 -O3 -fomit-frame-pointer -arch i386 -arch x86_64 -Isrc/ -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c src/MD2.c -o build/temp.macosx-10.6-universal-2.6/src/MD2.o
    gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 build/temp.macosx-10.6-universal-2.6/src/MD2.o -o build/lib.macosx-10.6-universal-2.6/Crypto/Hash/MD2.so
    [...]
    building 'Crypto.Util._counter' extension
    gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -fwrapv -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -std=c99 -O3 -fomit-frame-pointer -arch i386 -arch x86_64 -Isrc/ -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c src/_counter.c -o build/temp.macosx-10.6-universal-2.6/src/_counter.o
    gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 build/temp.macosx-10.6-universal-2.6/src/_counter.o -o build/lib.macosx-10.6-universal-2.6/Crypto/Util/_counter.so
    $ /usr/bin/python2.6 setup.py install
    running install
    running build
    running build_py
    running build_ext
    warning: GMP library not found; Not building Crypto.PublicKey._fastmath.
    running install_lib
    creating /Library/Python/2.6/site-packages/Crypto
    [...]
    byte-compiling /Library/Python/2.6/site-packages/Crypto/pct_warnings.py to pct_warnings.pyc
    running install_egg_info
    Writing /Library/Python/2.6/site-packages/pycrypto-2.3-py2.6.egg-info
    $ /usr/bin/python2.6 setup.py test
    running test
    ............................................................................................[...]
    ----------------------------------------------------------------------
    Ran 902 tests in 42.612s
    
    OK
    $ cd 
    $ /usr/bin/python2.6
    Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
    [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from Crypto.Hash import MD5
    >>> m = MD5.new()
    >>> m.update('abc')
    >>> m.digest()
    '\x90\x01P\x98<\xd2O\xb0\xd6\x96?}(\xe1\x7fr'
    >>> m.hexdigest()
    '900150983cd24fb0d6963f7d28e17f72'
    >>> ^D
    

    【讨论】:

    • 如果我添加 CC=gcc-4.2,那么我仍然会得到错误。请参阅上述问题中添加的“编辑”。还有其他想法吗?提前致谢。
    • 是的,这个简单的解决方案不适用于 pycrypto。查看其他选项的更新答案。
    • ARCHFLAGS 解决方案对我不起作用。另外,由于我要使用的库之一,我需要使用 python 2.6。我使用此处找到的 Mac 安装程序磁盘映像安装了 python:python.org/download/releases/2.6.6
    • 似乎使用 CXX 而不使用 sudo 会有所不同。查看我最近的编辑。任何想法这个其他错误来自什么?谢谢!
    • Mark:我解释了sudo 在我对您其他类似问题的回答中的不同之处。通常出于安全原因,sudo 进程不会从其父进程继承大多数环境变量,o 如果您在 sudo 步骤之前定义 CCCXX,它们将不会在 @987654333 中看到@ed 命令。这就是为什么我向您展示了如何通过 sudo bash 等来解决这个问题。
    【解决方案2】:

    顺便说一句,这是一个distutils 问题,Python 本身不会编译任何东西。

    正如您所发现的,您可以使用 CC 环境变量覆盖编译器。您可以覆盖与CXX 环境变量一起使用的链接器。我也想知道为什么 distutils 会这样做,但确实如此。

    【讨论】:

    • Distutils 试图确保任何 C 扩展模块的编译和链接方式与 Python 本身中的 C 模块的编译和链接方式兼容。平台之间对于什么是必要的存在差异。在 OS X 上,Distutils 尝试确保使用一组兼容的 CPU 架构,使用相同的 OS X ABI (MACOSX_DEPLOYMENT_TARGET),并使用相同的 GCC 版本(例如,OS 中的 gcc-4.2 X 10.6 不支持 32 位 python.org 安装程序仍支持的 ppc G3 架构)等。
    • 我明白这一点,但覆盖默认行为非常不稳定。请注意,这里使用了两个环境变量,我看不出有什么好的理由。我也不明白为什么要使用环境变量,而不是提供一个普通的旧命令行开关。 (--compiler 开关只会改变编译器的风格,可以这么说,即“unix”/vc/borland/etc。它不允许您指定命令)。正如我在尝试使用 mpicc 作为编译器时了解到的那样,所有这些的文档都非常不完整。
    • Distutils 文档令人沮丧地不完整,没有任何争论。您可能知道,有一个接近完成的Distutils2 项目旨在替换 Python 3.3 标准库中的 Distutils,并且也可以安装在现有的 Python(2.4 -> 3.2)中。 packages.python.org/Distutils2 将是建议或贡献代码和/或文档改进的地方。
    • 实际上,这对我来说是个新闻。希望他们允许将 distutils2 包与源包一起分发,因为我的库是为在集群上运行而设计的,它们上面有可怕的旧版本软件。
    • 似乎使用 CXX 而不使用 sudo 会有所不同。查看我最近的编辑。任何想法这个其他错误来自什么?谢谢!
    【解决方案3】:

    关注这个错误:

    unable to execute gcc-4.0: No such file or directory
    error: command 'gcc-4.0' failed with exit status 1
    

    编译器会准确地告诉您问题所在。 gcc-4.0(和gcc-4.2)不在您的PATH 中。提示:which gcc-4.2,例如:

    % which gcc-4.2
    /usr/bin/gcc-4.2
    

    假设你的位置在同一个位置,我不确定为什么 /usr/bin 不在你的 PATH 中,但是你有它!

    找出你的PATH实际上是什么:

    % echo $PATH
    /opt/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/bin/X11:/sw/bin:/sw/sbin
    

    修好你的道路,你就会走上启蒙之路。

    【讨论】:

    • 这里的问题是Apple最近发布的新Xcode 4不再包含gcc-4.0,也不再包含旧的ppc支持。
    • 那个位置在我的路径中。我安装了 4.2,但我的机器上没有 gcc-4.0。我不知道它为什么要寻找 4.0,而且让它寻找 4.2 的解决方案都没有奏效(例如设置 CC 和 CXX 环境变量)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 2015-02-09
    • 2011-01-02
    • 1970-01-01
    • 2013-12-02
    相关资源
    最近更新 更多