当gcc在OSX中的PATH上时编译cython模块 [英] compiling cython modules when gcc is on the PATH in OSX

查看:258
本文介绍了当gcc在OSX中的PATH上时编译cython模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用distutils通过Enthought Canopy版本的python来编译cython模块;但是很明显,gcc和clang之间存在混淆。 Distutils尝试使用 gcc 和clang选项 -arch x86_64 编译模块。问题是我从Macports安装了gcc,所以gcc不仅仅是clang的链接。我可以使用 CC ='clang'./setup.py build_ext 来编译该模块,但是在分发该模块方面有点不客气。我可以在setup.py中添加一些内容,以使其在这种设置下更加强大吗?如果在编译器标志中使用-arch,我正在考虑使用clang的方法,但是我似乎无法找到distutils确切地从何处获得了编译器标志,或者如何告诉它使用特定的编译器。

I'm trying to use distutils to compile a cython module using Enthought Canopy's version of python; however it's clear there is a mixup between gcc and clang. Distutils is trying to compile the module using gcc and the clang option -arch x86_64. The problem is that I have gcc installed from macports, so gcc isn't just a link to clang. I can get the module to compile using CC='clang' ./setup.py build_ext, but this feels a little hacky in terms of distributing the module. Is there something I can put in setup.py to make it more robust with this sort of setup? I'm thinking something along the lines of using clang if -arch is in the compiler flags, but I can't seem to find where exactly distutils gets the compiler flags, or how to tell it to use a specific compiler.

推荐答案


Distutils尝试使用gcc和clang选项-arch x86_64编译模块。

Distutils is trying to compile the module using gcc and the clang option -arch x86_64.

那不是c选项;


问题是我已经从macports安装了gcc,所以gcc不仅仅是链接到clang。

The problem is that I have gcc installed from macports, so gcc isn't just a link to clang.

gcc 从不只是指向 clang 的链接,除非您确实在Mac上做过一些奇怪的事情。特别是,如果您安装了包含MacPorts所需的Xcode命令行工具,则将有一个 / usr / bin / gcc ,该链接是 gcc-4.2 或 llvm-gcc-4.2 (或 gcc-4.0 或更旧)。很快,有一天,苹果将放弃对 llvm-gcc 的支持,而根本没有任何 gcc 。* *

gcc is never just a link to clang, unless you've really done something weird on your Mac. In particular, if you've install the Xcode Command Line Tools necessary to include MacPorts, you will have a /usr/bin/gcc that's a link to gcc-4.2 or llvm-gcc-4.2 (or gcc-4.0 or older). One day soon, Apple will drop support for llvm-gcc, and there will be nothing called gcc at all.**


我可以使用CC ='clang'./setup.py build_ext获取要编译的模块,但是这有点儿麻烦分配模块的条件

I can get the module to compile using CC='clang' ./setup.py build_ext, but this feels a little hacky in terms of distributing the module

该模块显然想使用 gcc 进行构建,意思是苹果的/ usr / bin / gcc。

The module apparently wants to build with gcc, by which it means Apple's /usr/bin/gcc.

依靠 gcc 成为任何特定的编译器几乎总是一个坏主意,因为 MacPorts文档解释。

Relying on gcc to be any specific compiler is almost always a bad idea, as the MacPorts documentation explains.

但是,在这种情况下, Python做到这一点很有意义。 Python会记住用于构建它的编译器以及相同的编译器设置,依此类推,因此可以确保distutils将构建它可以实际使用的模块。因此,如果它是使用/ usr / bin / gcc和 -arch x86_64 构建的,那就是它将用来构建模块的方法。您可以运行 python-config 工具(每个Python安装都有其自己的工具,因此请确保您运行的是正确的安装工具),以确切了解其需求。

However, in this case, it makes sense for Python to do this. Python remembers the compiler used to build it, and the same compiler settings, and so on, so it can be sure that distutils will build modules it can actually use. So, if it was built with /usr/bin/gcc, with -arch x86_64, that's what it's going to try to use to build modules. You can run the python-config tool (each of your Python installations will have its own, so make sure you run the right one) to see exactly what it wants.

用Apple的clang代替Apple的gcc-4.2几乎总是可以逃脱。但是要替换一些甚至不支持相同选项的随机编译器吗?

You can almost always get away with substituting Apple's clang for Apple's gcc-4.2. But substituting some other random compiler that doesn't even support the same options? That's not going to work.

如果您想使用MacPorts构建Python模块,则几乎可以肯定要构建Python本身。对于MacPorts,不要使用某些预编译的二进制安装程序。最重要的是,您可能想对具有端口的任何Python模块使用端口,而不是手动构建端口,因为其中许多都需要使用MacPorts的解决方法。

If you want to build Python modules with MacPorts, you almost certainly want to build Python itself with MacPorts, not use some precompiled binary installer. On top of that, you probably want to use ports for any Python modules that have them, instead of building them manually, because many of them require workarounds to work with MacPorts.

另一方面,如果您打算构建非MacPorts软件(Python模块或其他模块),则最好 not 将MacPorts gcc 作为PATH的最重要内容。几乎每个configure / setup.py / etc。曾经撰写过的文章都会检测到您使用的是Mac,希望 gcc 是Apple gcc ,并通过 -arch 标志,依此类推。 (实际上,您会注意到,甚至许多端口也明确要求使用apple-gcc-4.2,llvm-gcc-4.2或clang,因为即使MacPorts团队也无法使它们使用其他编译器。您确定要这样做吗?尝试?)

On the other hand, if you plan to build non-MacPorts software—Python modules or otherwise—you're really better off not putting the MacPorts gcc as the top thing on the PATH. Almost every configure/setup.py/etc. ever written will detect that you're on a Mac, expect gcc to be Apple gcc, and pass -arch flags, and so on. (In fact, you'll notice that even many ports explicitly require apple-gcc-4.2, llvm-gcc-4.2, or clang, because even the MacPorts team couldn't get them working with a different compiler. Are you sure you want to try?)

*……这将是例如的链接。 /llvm-gcc-4.2/bin/llvm-gcc-4.2 ,它本身将通过 i686-apple-darwin11-llvm-gcc-4.2的链接或存根包装本身…但这并不重要。

* … and that will be a link to, say, ../llvm-gcc-4.2/bin/llvm-gcc-4.2, which will itself by a link to or stub wrapper around i686-apple-darwin11-llvm-gcc-4.2… but that's not important.

**至少在Xcode 5.0 Beta中这种说法是正确的。

** Or at least that was true off and on in the Xcode 5.0 betas.

这篇关于当gcc在OSX中的PATH上时编译cython模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆