【发布时间】:2015-04-22 20:46:29
【问题描述】:
我有一个 Python C++ 扩展,在 OS X 上使用 Clang 编译时需要以下编译标志:
CPPFLAGS='-std=c++11 -stdlib=libc++ -mmacosx-version-min=10.8'
LDFLAGS='-lc++'
在我的setup.py 中检测 OS X 非常简单。我可以这样做:
if sys.prefix == 'darwin':
compile_args.append(['-mmacosx-version-min=10.8', '-stdlib=libc++'])
link_args.append('-lc++')
(请参阅here 了解完整上下文)
但是,在 GCC 上,这个编译标志是无效的。因此,如果我以这种方式编写 setup.py,如果有人尝试在 OS X 上使用 GCC,编译将失败。
GCC 和 Clang 支持不同的编译器标志。因此,我需要知道将调用哪个编译器,以便发送不同的标志。在setup.py中检测编译器的正确方法是什么?
编辑 1:
请注意,编译错误不会引发 Python 异常:
$ python setup.py build_ext --inplace
running build_ext
building 'spacy.strings' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -c spacy/strings.cpp -o build/temp.linux-x86_64-2.7/spacy/strings.o -O3 -mmacosx-version-min=10.8 -stdlib=libc++
gcc: error: unrecognized command line option ‘-mmacosx-version-min=10.8’
gcc: error: unrecognized command line option ‘-stdlib=libc++’
error: command 'gcc' failed with exit status 1
$
【问题讨论】:
-
当您向 GCC 传递一个错误标志时,会发生什么异常(在您的 Python 脚本中)?正确的方法是
try: send_flags_for_clang() except ThatException: send_flags_for_gcc()。 -
查看编辑 --- 未引发异常。
标签: python setuptools setup.py distutils compiler-flags