首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何告诉distutils使用gcc?

如何告诉distutils使用gcc?
EN

Stack Overflow用户
提问于 2013-05-24 22:23:36
回答 6查看 52.7K关注 0票数 59

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

代码语言:javascript
复制
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)

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

代码语言:javascript
复制
cls ~/workspace/CythonOpenMP/src $ python3 setup.py build

无法识别此标志,因为编译器为clang:

代码语言:javascript
复制
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,但没有成功:

代码语言:javascript
复制
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?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-05-24 22:42:56

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

票数 57
EN

Stack Overflow用户

发布于 2013-05-25 01:02:00

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

  • 创建文件"C:\Python27\Lib\distutils\distutils.cfg“并将其写入:

代码:

代码语言:javascript
复制
[build]
compiler = mingw32

从文件"C:\Python27\Lib\distutils\cygwinccompiler.py“:中删除"-mno-cygwin”“C:\Python27\Lib\distutils\cygwinccompiler.py”选项的所有实例

这一点:

代码语言:javascript
复制
    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))

变成这样:

代码语言:javascript
复制
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的实际需求没有直接关系(但仍然与问题的标题有关……)

票数 22
EN

Stack Overflow用户

发布于 2013-05-24 22:43:58

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

代码语言:javascript
复制
CC=gcc python setup.py build

您不需要设置CXX,它不会对此进行检查。

票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16737260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档