如何告诉distutils使用gcc?
我想用Cython包装一个包含C ++和OpenMP代码的testing项目,并通过setup.py
文件使用distutils构build它。 我的文件的内容如下所示:
from distutils.core import setup from distutils.extension import Extension from Cython.Build import cythonize from Cython.Distutils import build_ext modules = [Extension("Interface", ["Interface.pyx", "Parallel.cpp"], language = "c++", extra_compile_args=["-fopenmp"], extra_link_args=["-fopenmp"])] for e in modules: e.cython_directives = {"embedsignature" : True} setup(name="Interface", cmdclass={"build_ext": build_ext}, ext_modules=modules)
-fopenmp
标志与gcc一起使用来编译和链接到OpenMP。 但是,如果我只是援引
cls ~/workspace/CythonOpenMP/src $ python3 setup.py build
这个标志不被识别,因为编译器是叮当声的:
running build running build_ext skipping 'Interface.cpp' Cython extension (up-to-date) building 'Interface' extension cc -Wno-unused-result -fno-common -dynamic -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -c Interface.cpp -o build/temp.macosx-10.8-x86_64-3.3/Interface.o -fopenmp clang: warning: argument unused during compilation: '-fopenmp' cc -Wno-unused-result -fno-common -dynamic -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -c Parallel.cpp -o build/temp.macosx-10.8-x86_64-3.3/Parallel.o -fopenmp clang: warning: argument unused during compilation: '-fopenmp' Parallel.cpp:24:10: warning: unknown pragma ignored [-Wunknown-pragmas] #pragma omp parallel for ^ 1 warning generated. c++ -bundle -undefined dynamic_lookup -L/usr/local/lib -L/usr/local/opt/sqlite/lib build/temp.macosx-10.8-x86_64-3.3/Interface.o build/temp.macosx-10.8-x86_64-3.3/Parallel.o -o build/lib.macosx-10.8-x86_64-3.3/Interface.so -fopenmp ld: library not found for -lgomp clang: error: linker command failed with exit code 1 (use -v to see invocation) error: command 'c++' failed with exit status 1
我没有尝试指定gcc:
cls ~/workspace/CythonOpenMP/src $ python3 setup.py build --compiler=g++-4.7 running build running build_ext error: don't know how to compile C/C++ code on platform 'posix' with 'g++-4.7' compiler
我怎么能告诉distutils使用gcc?
尝试使用os.environ从setup.py中设置“CC”环境variables。
以防万一其他人在Windows下面临相同的问题(其中CC环境variables不会有任何影响):
- 创build文件“C:\ Python27 \ Lib \ distutils \ distutils.cfg”并写在里面:
代码:
[build] compiler = mingw32
- 从文件“C:\ Python27 \ Lib \ distutils \ cygwinccompiler.py”中删除所有“-mno-cygwin”gcc选项的实例:
这个 :
self.set_executables(compiler='gcc -mno-cygwin -O -Wall', compiler_so='gcc -mno-cygwin -mdll -O -Wall', compiler_cxx='g++ -mno-cygwin -O -Wall', linker_exe='gcc -mno-cygwin', linker_so='%s -mno-cygwin %s %s' % (self.linker_dll, shared_option, entry_point))
变成这样:
self.set_executables(compiler='gcc -O -Wall', compiler_so='gcc -mdll -O -Wall', compiler_cxx='g++ -O -Wall', linker_exe='gcc', linker_so='%s %s %s' % (self.linker_dll, shared_option, entry_point))
第二点可能是必要的,如果您使用的是最新版本的gcc,其中已弃用的选项-mno-cygwin
已被删除。
希望这将有助于即使它不是直接关系到OP的实际需求(但仍然涉及到问题的标题…)
我只看了一下distutils
源代码,而--compiler
选项需要“unix”,“msvc”,“cygwin”,“mingw32”,“bcpp”或者“emx”。 它通过检查CC
环境variables来检查所需的编译器名称。 尝试调用像这样的构build:
CC=gcc python setup.py build
你不需要设置CXX
,它不检查。
试试这个: http : //mail.python.org/pipermail/distutils-sig/2002-August/002944.html
总之,看来你应该试试:python setup.py build –compiler = g ++ first。
你可以尝试安装anaconda env:
conda create -n py35a python=3.5 numpy scipy matplotlib pandas gcc anaconda
搭配:
zsh -c ". activate py35a && gcc --version && python setup.py build_ext --inplace"