我有一个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++')
(完整上下文请见 这里)
然而,在 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
$
try: send_flags_for_clang() except ThatException: send_flags_for_gcc()
。 - Two-Bit Alchemist