59

C++ および OpenMP コードを含むテスト プロジェクトを Cython でラップし、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フラグは、OpenMP に対してコンパイルおよびリンクするために gcc で使用されます。ただし、呼び出すだけの場合

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 を使用するように指示するにはどうすればよいですか?

4

6 に答える 6

57

os.environ を使用して setup.py 内から「CC」環境変数を設定してみてください。

于 2013-05-24T14:42:56.073 に答える
22

他の人が Windows で同じ問題に直面している場合に備えて (CC 環境変数は何の効果もありません):

  • ファイル「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))

-mno-cygwin非推奨のオプションが削除された最新バージョンの gcc を使用している場合、2 番目のポイントが必要になることがあります。

OPの実際のニーズに直接関係していなくても、これが役立つことを願っています(ただし、質問のタイトルにはまだ関連しています...)

于 2013-05-24T17:02:00.360 に答える
21

distutilsソースを調べたところ、--compilerオプションは「unix」、「msvc」、「cygwin」、「mingw32」、「bcpp」、または「emx」を想定しています。CC環境変数をチェックして、必要なコンパイラ名をチェックします。次のように build を呼び出してみてください:

CC=gcc python setup.py build

を設定する必要はありCXXません。それはチェックされません。

于 2013-05-24T14:43:58.270 に答える