在 OSX 10.10.5 中使用 gcc 时出现编译错误

Compilation error when using gcc in OSX 10.10.5

我正在尝试使用 python setup.py install 安装 python 软件包,但在安装过程的某个时刻出现错误:

gcc: error: x86_64: No such file or directory
gcc: error: unrecognized option ‘-arch’
error: command 'gcc' failed with exit status 1

之前,我为 Xcode 7 安装了 Xcode 7.0 及其各自的命令行工具。编译器似乎在 which gcc 本地 /usr/local/bin/gcc。但是,当我尝试 gcc -v 时,我得到了 Segmentation fault: 11。此外,当我尝试 /usr/bin/gcc -v 时,我得到了

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

然后,安装的编译器似乎在不同的位置。 Command line tool installed, but gcc/g++ compiler not working 中提出了类似的问题,但没有明确的解决方案。你知道我该如何修复它吗(link 到实际安装的编译器以继续安装 Python 包)?提前致谢。

扩展 @Droppy 的评论:xcode 没有在 /usr/local/bin 中安装 gcc(OP 没有说明它的来源)。 MacPorts 会把它放在 /opt/local/bin/gcc 中,但忽略 select 当前端口。所以 CC=clang 是最简单的回答方式。但是,OP 询问如何在 setup.py.

中覆盖它

已在这些问题中讨论过:

  • Choosing GCC version when building ( setup.py )
  • How may I override the compiler (gcc) flags that setup.py uses by default?
  • How to tell distutils to use gcc?

first最中肯,导致了这个建议:

CC=/usr/bin/clang CFLAGS="-O" python setup.py build

(clang 会忽略大部分 gcc 选项,甚至不会发出警告,但设置 CFLAGS 可以帮助说服 python 脚本不要尝试 clang 不太可能支持的选项)。