Python 如何告诉 distutils 使用 gcc?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16737260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:29:32  来源:igfitidea点击:

How to tell distutils to use gcc?

pythoncompiler-errorscythondistutils

提问by clstaudt

I want to wrap a test project containing C++ and OpenMP code with Cython, and build it with distutils via a setup.pyfile. The content of my file looks like this:

我想用 Cython 包装一个包含 C++ 和 OpenMP 代码的测试项目,并通过setup.py文件用 distutils 构建它。我的文件内容如下所示:

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)

The -fopenmpflag is used with gcc to compile and link against OpenMP. However, if I just invoke

-fopenmp标志与 gcc 一起用于编译和链接 OpenMP。但是,如果我只是调用

cls ~/workspace/CythonOpenMP/src $ python3 setup.py build

this flag is not recognized, because the compiler is clang:

无法识别此标志,因为编译器是叮当声的:

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

I've unsucessfully tried to specify gcc:

我尝试指定 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

How can I tell distutils to use gcc?

如何告诉 distutils 使用 gcc?

采纳答案by timotimo

Try setting the "CC" environment variable from inside the setup.py with os.environ.

尝试使用 os.environ 从 setup.py 内部设置“CC”环境变量。

回答by Andrew W

Try this: http://mail.python.org/pipermail/distutils-sig/2002-August/002944.html

试试这个:http: //mail.python.org/pipermail/distutils-sig/2002-August/002944.html

In short, it appears that you should try: python setup.py build --compiler=g++ first.

简而言之,您似乎应该先尝试: python setup.py build --compiler=g++ 。

回答by SethMMorton

I just took a look at the distutilssource, and the --compileroption expects "unix", "msvc", "cygwin", "mingw32", "bcpp", or "emx". It checks the compiler name you want by checking the CCenvironment variable. Try calling build like this:

我只是看了一下distutils源代码,该--compiler选项需要“unix”、“msvc”、“cygwin”、“mingw32”、“bcpp”或“emx”。它通过检查CC环境变量来检查您想要的编译器名称。尝试像这样调用构建:

CC=gcc python setup.py build

You don't need to set CXX, it doesn't check for that.

你不需要设置CXX,它不会检查。

回答by Gauthier Boaglio

Just in case some others are facing the same problem under Windows (where CC environment variable wouldn't have any effect) :

以防万一其他人在 Windows 下面临同样的问题(其中 CC 环境变量不会产生任何影响):

  • Create file "C:\Python27\Lib\distutils\distutils.cfg" and write this inside :
  • 创建文件“C:\Python27\Lib\distutils\distutils.cfg”并将其写入其中:

Code :

代码 :

[build]
compiler = mingw32
  • Remove all instances of "-mno-cygwin" gcc option from file "C:\Python27\Lib\distutils\cygwinccompiler.py" :
  • 从文件 "C:\Python27\Lib\distutils\cygwinccompiler.py" 中删除 "-mno-cygwin" gcc 选项的所有实例:

This :

这个 :

    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))

Becomes this :

变成这样:

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))

The second point can be necessary in case you are using a recent version of gcc, where the deprecated option -mno-cygwinhas been removed.

如果您使用的是最新版本的 gcc,其中不推荐使用的选项-mno-cygwin已被删除,则第二点可能是必要的。

Hope this will help even if it is not directly related to the OP real needs (but still related to the question's title...)

希望这会有所帮助,即使它与 OP 的实际需求没有直接关系(但仍然与问题的标题有关......)

回答by MarSoft

According to this wiki, Python versions after 3.4 do not support MinGW anymore. CPython 3.7 for Windows is compiled with MSC v.1916. When I try to use above-mentioned method with distutils.cfg, I then get an error from distutils: Unknown MS Compiler Version 1916. Looks like it has a hardcoded table of msvcr libraries in its cygwincompiler.pyfile (which is also responsible for MinGW), and last version known to that file is 1600from VS2010 / MSVC 10.0.

根据这个 wiki,3.4 之后的 Python 版本不再支持 MinGW。适用于 Windows 的 CPython 3.7 使用MSC v.1916. 当我尝试将上述方法与 一起使用时distutils.cfg,我从 distutils: 收到错误消息Unknown MS Compiler Version 1916。看起来它的文件中有一个硬编码的 msvcr 库表cygwincompiler.py(也负责 MinGW),该文件已知的最新版本1600来自 VS2010 / MSVC 10.0。