Python在OS X上使用错误版本的GCC构建
我正在尝试构建一个叫做pycrypto的python包。我的Mac上安装的是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的情况下会出现那个错误?
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
修正你的路径,你就能走向更高的境界了。
顺便说一下,这是一个distutils
的问题,Python本身并不会编译任何东西。
正如你发现的,你可以用CC
这个环境变量来替换编译器。你也可以用CXX
这个环境变量来替换链接器。我也想知道为什么distutils
会这样,但事实就是如此。
根据你提供的路径(/Library/Frameworks/Python.framework/Versions/2.6
),看起来你安装的是一个只能在32位系统上运行的Python 2.6,可能是通过python.org的安装程序安装的。当你构建一个包含C扩展模块的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
另一个选择是安装来自python.org的64位/32位 Python 2.7安装程序。这个版本是用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