Python distutils 未使用正确版本的 gcc

发布于 2024-11-07 02:16:33 字数 609 浏览 4 评论 0原文

我正在尝试在 Mac OSX 10.6.5 上编译一个包。该软件包的安装脚本依赖于 distutils。问题是计算机的默认 gcc 是版本 4.2(我通过在终端窗口中运行 gcc --version 来确定这一点),但是当我运行“python setup.py build”时,我从输出中看到 distutils 正在选择 gcc -4.0 而不是 4.2 这是一个大问题,因为我使用的代码要求 gcc >= 4.2。我在这台机器上没有管理员权限,因此作为解决方法,我创建了一些将 gcc-4.0 发送到 gcc-4.2 的符号链接。结果是代码编译了,但生成的 .so 文件不起作用(当我尝试在 python 中导入它们时,我收到错误,抱怨共享对象中缺少 init 函数)。

我尝试在不同的 mac (10.6.6) 上编译此代码,它的工作方式就像一个魅力:distutils 选择 4.2,而不是被迫这样做,我可以毫无问题地导入生成的共享对象。所以,我想做的是在我的计算机上编译代码,而不必执行此符号链接技巧...我只是希望 distutils 自动选择 4.2,因为它应该。我尝试获取正确编译的 .so 文件并将其传输到我的计算机,但由于多种原因而失败(它们与我的计算机上不存在的库链接/与安装的版本不同)。

这里有人有什么建议吗?

谢谢, 乔什

I am trying to compile a package on Mac OSX 10.6.5. The package's install script relies on 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. 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. 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).

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. 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. 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).

Does anyone have any advice here?

Thanks,
Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请帮我爱他 2024-11-14 02:16:33

要强制 distutils 使用单独的编译器,您可以通过环境重新定义一些变量。首先,找出 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'

接下来,您需要重新定义它们,替换为您想要使用的 gcc 版本:

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

请记住,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'

To force distutils to use a separate compiler, you can redefine a few variables via the environment. First, find out what distutils is using as defaults:

>>> 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:

% 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:

>>> 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'
智商已欠费 2024-11-14 02:16:33

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

CC=gcc-4.2 python setup.py build

Have you tried setting a custom CC environment variable?

CC=gcc-4.2 python setup.py build
霓裳挽歌倾城醉 2024-11-14 02:16:33

您没有说明您正在使用哪个版本的 Python,但您使用的计算机上的默认 Python 很可能不是 Apple 提供的 Python 2.6.1,更可能是从 python.org 安装的 Python。尝试使用 /usr/bin/python2.6 运行构建脚本,它应该是 Apple 提供的 Python 2.6;它确实使用了 gcc-4.2。

当您使用 Python 的 Distutils 时(通常通过运行 setup.py 脚本或使用 easy_install),Distutils 会尝试确保您正在安装的包中的任何 C 扩展模块使用与构建 Python 本身相同的编译器版本和兼容的编译选项(CPU 架构、ABI 等)进行编译。传统上,大多数适用于 OS X 的 python.org 安装程序都提供可在多个版本的 OS X 和多个 CPU 架构上运行的通用 Python。这就是为什么它们是用 gcc-4.0 构建的。如果您需要将 gcc-4.2 用于其他库,那么最安全的方法是使用使用 gcc-4.2 构建的 Python。 Apple 提供的 OS X 10.6 上的系统 Python 就是如此构建的。此外,对于最新版本的 Python(2.7.1 和 3.2),python.org 为 OS X 10.6 提供了第二个 OS X 安装程序变体,该变体也是使用 gcc-4.2 构建的。但如果您没有计算机的管理员访问权限,那么无论如何这都不是一个选择。

您可以通过以下方式查看默认使用哪个Python:

which python

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. Try running the build script with /usr/bin/python2.6 which should be the Apple-supplied Python 2.6; that does use 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. Traditionally, most python.org installers for OS X provide a universal Python that works on multiple versions of OS X and multiple CPU archs. That's why they are built with 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. The Apple-supplied system Pythons on OS X 10.6 are so built. 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. 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:

which python
乱世争霸 2024-11-14 02:16:33

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

You can either adjust your distutils.cfg (see here) or you can pass command line arguments as --compiler=gcc42 (not sure about the last one).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文