在setup.py中编译C ++代码时,如何删除-arch i386标志?

时间:2019-05-23 15:22:26

标签: python c++11 setup.py

我想为Python编译C ++扩展。问题是C ++程序仅在x86_64体系结构中工作,但是默认的编译标志包括-arch i386。

setup.py如下:

from distutils.core import setup, Extension

integral_ext = Extension('mod._mod', 
            sources = ['wrap.cpp'],
            extra_compile_args = ['-mmacosx-version-min=10.7', '-std=c++11', '-stdlib=libc++'])

setup(
    ext_modules = [integral_ext],
)

在编译过程中,终端显示命令为:

/usr/bin/clang -fno-strict-aliasing -Wsign-compare -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -g -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -c wrap.cpp -o build/temp.macosx-10.6-intel-3.6/wrap.o -mmacosx-version-min=10.7 -std=c++11 -stdlib=libc++

由于C ++中需要一些x86_64的static_assert代码,因此此操作将失败。 删除-arch i386标志后,我可以通过命令行成功编译它。但是,我正在编写一个库供其他人使用,并且我想简化它,以便用户可以通过运行python3 setup.py install的标准方法来安装它。

最重要的是,我无法更改C ++文件,并且我想在setup.py中做一些事情使其编译。谢谢!

1 个答案:

答案 0 :(得分:0)

当我在setup.py的开头包含以下几行时,错误消失了:

import os

os.environ['CFLAGS'] = '-fno-strict-aliasing -dynamic -DNDEBUG -g -fwrapv -O3 -c'
os.environ['LDFLAGS'] = '-arch x86_64 '

运行python3 setup.py install时的编译输出为:

/usr/bin/clang -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fno-strict-aliasing -dynamic -DNDEBUG -g -fwrapv -O3 -c -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -c qtwinclient_wrap.cpp -o build/temp.macosx-10.6-intel-3.6/qtwinclient_wrap.o -mmacosx-version-min=10.7 -std=c++11 -stdlib=libc++
/usr/bin/clang++ -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -g -arch x86_64 -fno-strict-aliasing -dynamic -DNDEBUG -g -fwrapv -O3 -c build/temp.macosx-10.6-intel-3.6/qtwinclient_wrap.o -o build/lib.macosx-10.6-intel-3.6/qtwinclient/_qtwinclient.cpython-36m-darwin.so -lc++ -mmacosx-version-min=10.7 -Lqtwinclient -L/Library/Frameworks/Python.framework/Versions/3.6/lib/ -lqtwinclient_c -lpython3.6
clang: warning: build/temp.macosx-10.6-intel-3.6/qtwinclient_wrap.o: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lc++: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lqtwinclient_c: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lpython3.6: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-bundle' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-undefined dynamic_lookup' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-g' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-fno-strict-aliasing' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-D NDEBUG' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-g' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-fwrapv' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-O3' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-mmacosx-version-min=10.7' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-Lqtwinclient' [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-L/Library/Frameworks/Python.framework/Versions/3.6/lib/' [-Wunused-command-line-argument]

编译成功,但是clang: warning: argument unused during compilation的许多警告看起来仍然很丑陋。如果有人可以提出建议,那么将不胜感激。