python - 如何告诉 distutils 使用 gcc?

标签 python compiler-errors cython distutils

我想用 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)

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

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

这个标志无法识别,因为编译器是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

我尝试指定 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”环境变量。

关于python - 如何告诉 distutils 使用 gcc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16737260/

相关文章:

c++ - 使用来自 Cython 构造函数的参数初始化 C++ 对象

python - 如何同时在容器和数据中将 pandas 数据框注册为 parquet 或 csv 数据集?

python - 在python或c中从/dev/video0读取摄像头输入

python - python 6 的奇怪问题 : cannot import urllib

python - psycopg2 - 导入错误 : DLL load failed while importing _psycopg: The operating system cannot run %1

c++ - 为模板元编程打印静态变量

visual-studio-2010 - USBView编译错误

css - 为新颜色变量编译 materializeCSS 框架会出错

python - Cython 创建 C 函数别名

python - Cython MemoryView 转置 : Typeerror