简体   繁体   English

Python distutils没有使用正确版本的gcc

[英]Python distutils not using correct version of gcc

I am trying to compile a package on Mac OSX 10.6.5. 我想在Mac OSX 10.6.5上编译一个包。 The package's install script relies on distutils. 包的安装脚本依赖于distutils。 The problem is that the computer's default gcc is version 4.2 (I determined this by just running gcc --version in a terminal window) but when I run 'python setup.py build', I see from the output that the distutils is choosing gcc-4.0 instead of 4.2 This is a big problem because the code I am using require gcc >= 4.2. 问题是计算机的默认gcc是版本4.2(我通过在终端窗口中运行gcc --version来确定这一点)但是当我运行'python setup.py build'时,我从输出中看到distutils正在选择gcc -4.0而不是4.2这是一个很大的问题,因为我使用的代码需要gcc> = 4.2。 I do not have admin rights on this machine, so as a workaroud, I created some symlinks that send gcc-4.0 to gcc-4.2. 我没有这台机器的管理员权限,所以作为一个workaroud,我创建了一些将gcc-4.0发送到gcc-4.2的符号链接。 The result is the code compiles, but the generated .so files do not work (when I try to import them in python, I get errors complaining about a missing init function in the shared object). 结果是代码编译,但生成的.so文件不起作用(当我尝试在python中导入它们时,我得到错误抱怨共享对象中缺少init函数)。

I have tried compiling this code on a different mac (10.6.6) and it works like a charm: distutils chooses 4.2 without being forced to do so and I can import the generated shared object without a problem. 我尝试在不同的mac(10.6.6)上编译这个代码,它就像一个魅力:distutils选择4.2而不被强制这样做,我可以毫无问题地导入生成的共享对象。 So, what I would like to do is to compile the code on my computer without having to do this symlink trickery...I just want distutils to choose 4.2 automatically as it should. 所以,我想要做的是在我的计算机上编译代码,而不必做这个符号链接技巧...我只是想让distutils自动选择4.2。 I have tried taking the .so files that compile properly and transferring them to my computer, but that fails for a number of reasons (they are linked against libraries that are not present on my machine/are a different version that those installed). 我已经尝试将正确编译的.so文件并将它们传输到我的计算机,但由于多种原因(它们与我的机器上不存在的库链接/与安装的版本不同)失败。

Does anyone have any advice here? 有没有人在这里有任何建议?

Thanks, Josh 谢谢,乔希

To force distutils to use a separate compiler, you can redefine a few variables via the environment. 要强制distutils使用单独的编译器,您可以通过环境重新定义一些变量。 First, find out what distutils is using as defaults: 首先,找出distutils使用的默认值:

>>> from distutils import sysconfig
>>> sysconfig.get_config_var('LDSHARED')
'gcc-4.0 -Wl,-F. -bundle -undefined dynamic_lookup'
>>> sysconfig.get_config_var('CC')
'gcc-4.0'

Next you need to redefine those, substituting in the version of gcc you'd like to use: 接下来你需要重新定义那些,替换你想要使用的gcc版本:

% LDSHARED="gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup" CC=gcc-4.2 \
    /usr/bin/python setup.py build_ext

Keep in mind that the sysconfig defaults are pulled from the Makefile which was originally used to compile python, so fudging with them may produce unintended results: 请记住,sysconfig默认值是从最初用于编译python的Makefile中提取的,因此用它们捏造可能会产生意想不到的结果:

>>> path = sysconfig.get_python_lib(plat_specific=1, standard_lib=1)
>>> os.path.join(path, 'config', 'Makefile')
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/config/Makefile'

您是否尝试过设置自定义CC环境变量?

CC=gcc-4.2 python setup.py build

You don't say which versions of Python you are using but chances are that the default Python on the machine you are using is not the Apple-supplied Python 2.6.1, more likely one installed from python.org. 你没有说你正在使用哪个版本的Python,但是你使用的机器上的默认Python不是Apple提供的Python 2.6.1,更可能是从python.org安装的。 Try running the build script with /usr/bin/python2.6 which should be the Apple-supplied Python 2.6; 尝试使用/usr/bin/python2.6运行构建脚本,该脚本应该是Apple提供的Python 2.6; that does use gcc-4.2 . 确实使用gcc-4.2

When you use Python's Distutils (typically by running a setup.py script or using easy_install ), Distutils tries to make sure that any C extension modules in the package you are installing will be compiled with the same compiler version and with compatible compilation options (CPU archs, ABI, etc) as was used to build Python itself. 当您使用Python的Distutils(通常通过运行setup.py脚本或使用easy_install )时,Distutils会尝试确保您正在安装的软件包中的任何C扩展模块将使用相同的编译器版本和兼容的编译选项进行编译(CPU archs,ABI等)用于构建Python本身。 Traditionally, most python.org installers for OS X provide a universal Python that works on multiple versions of OS X and multiple CPU archs. 传统上,OS X的大多数python.org安装程序都提供了一个通用的Python,可以在多个版本的OS X和多个CPU arch上运行。 That's why they are built with gcc-4.0. 这就是为什么它们是用gcc-4.0构建的。 If you need to use gcc-4.2 for some other library, then the safest thing is to use a Python that was built with gcc-4.2. 如果你需要将gcc-4.2用于其他库,那么最安全的是使用用gcc-4.2构建的Python。 The Apple-supplied system Pythons on OS X 10.6 are so built. 苹果提供的系统Pythons on OS X 10.6是如此构建的。 Also, for the most recent releases of Python (2.7.1 and 3.2), python.org provides a second OS X installer variant for OS X 10.6 that is also built with gcc-4.2. 此外,对于最新版本的Python(2.7.1和3.2),python.org为OS X 10.6提供了第二个OS X安装程序变体,它也是使用gcc-4.2构建的。 But if you do not have admin access to your machine, that's not an option anyway. 但是,如果您没有管理员访问您的计算机,那么无论如何这都不是一个选项。

You can see which python is being used by default by: 您可以通过以下方式查看默认使用哪个python:

which python

您可以调整distutils.cfg(请参阅此处 ),也可以将命令行参数传递为--compiler = gcc42(不确定最后一个)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM