2016-07-11 6 views
3

私はPythonで使用するためにC++拡張をコンパイルできるように、Windows 7でPython 2.7環境をセットアップしようとしています。私はこれに慣れていないので、簡単な例hereをダウンロードしてそのままそのまま使用しています。パスにnumpy.iファイルもあります。私はmingw(最新版)とswig(3.0.10版)でコンピュータをセットアップしました。私のPython版は2.7.9です。私はこの環境を使ってg ++を使って小さなC++プログラムを問題なくコンパイルしました。SWIG、mingw32、distutilsの問題

しかし、上記の「シンプルな」Python拡張機能をビルドしようとすると、次のような出力が表示されます(Windowsのcmd.exeウィンドウの最初の行にコマンドを含めました)。

python setup.py build -c=mingw32 
running build 
running build_ext 
building '_simple' extension 
swigging simple.i to simple_wrap.c 
C:\swigwin\swigwin-3.0.10\swig.exe -python -o simple_wrap.c simple.i 
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\lib\site-packages\numpy\core\include -I. -IC:\Python27\include -IC:\Python27\PC -c simple.cc -o build\temp.win32-2.7\Release\simple.o 
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\lib\sitepackages\numpy\core\include -I. -IC:\Python27\include -IC:\Python27\PC -c simple_wrap.c -o build\temp.win32-2.7\Release\simple_wrap.o 
writing build\temp.win32-2.7\Release\_simple.def 
C:\MinGW\bin\g++.exe -shared -s build\temp.win32-2.7\Release\simple.o build\temp.win32-2.7\Release\simple_wrap.o build\temp.win32-2.7\Release\_simple.def -LC:\Python27\libs -LC:\Python27\PCbuild -lpython27 -lmsvcr90 -o build\lib.win32-2.7\_simple.pyd 
build\temp.win32-2.7\Release\simple_wrap.o:simple_wrap.c:(.text+0xce5): undefined reference to `create_list' 
build\temp.win32-2.7\Release\simple_wrap.o:simple_wrap.c:(.text+0x170d): undefined reference to `dot' 
collect2.exe: error: ld returned 1 exit status 
error: command 'C:\\MinGW\\bin\\g++.exe' failed with exit status 1 

私は、私はここに非常に単純な何かが欠けてる恐ろしい気持ちを持っていますが、私は成功していない問題に別のCygwin環境でこれらの同じファイルをコンパイルするために管理してきました(はい、それは私のために望ましいですCygwin以外の環境でこれを行うことができます)。

私はあまりにも多くのコードでこの質問を抑制したくありませんが、参照のために私が使用しているファイルsimple.isetup.pyがここにあります。

simple.i: 
%module simple 
%{ 
    #define SWIG_FILE_WITH_INIT 
    #include "simple.h" 
%} 

%include "numpy.i" 
%init %{ 
import_array(); 
%} 

%apply (int DIM1, double* INPLACE_ARRAY1) {(int n0, double *a0)}; 
%apply (int DIM1, double* IN_ARRAY1) {(int n, double *a), (int m, double *b)}; 
%apply (int DIM1, double* ARGOUT_ARRAY1) {(int size, double *arr)}; 
%include "simple.h" 

setup.py: 
from distutils.core import setup, Extension 
import numpy 
import os 

os.environ['CC'] = 'g++'; 
setup(name='matt_simple_test', version='1.0', ext_modules =[Extension('_simple',['simple.cc', 'simple.i'], include_dirs = [numpy.get_include(),'.'])]) 

他のコードが必要な場合、私はそれらを投稿して幸せですが、再び、他のファイル(simple.ccsimple.h)はhereから逐語的に使用されています。

質問:私はこのエラーを修正するために私を導くことができますか?

+0

あなたはMinGWのとPythonの両方が32ビットであることを確認しましextern "C"を使用することですか? –

+0

私は標準的なmingwセットアッププログラムを使用しました。私の理解は32ビットでなければなりません。ちょっとした検索で、mingw-64がmingw32からフォークされたことがわかりました。私は確かにそのフォークをインストールしませんでした。 Pythonは確かに32ビットです。 – cabraut

答えて

1

このコンパイルステップでは、入力ファイルがC++コードとしてコンパイルされ、symbol.ccの関数にはCとの互換性のないシンボル(名前のマングリング)が与えられます。

C:\ MinGWの\ビン\ gcc.exe -mdll -O -Wall -IC:\ Python27 \ libには\サイト - パッケージ\ numpyのの\コア\は-Iが含まれます。 -IC:\ Python27 \ -IC含ま:\ Python27 \ PC -c simple.cc -oこのコンパイルでは

simple.o \ temp.win32-2.7 \リリース\を構築するには、入力ファイルをステップは次のようにコンパイルされていますCコードとsymbols.ccの関数のシンボルは異なると予想されます。

C:\ MinGWの\ビン\ gcc.exe -mdll -O -Wall -IC:\ Python27 \ libに\ sitepackages \ numpyの\コア\は-Iが含まれます。 -IC:\ Python27 \ PC -c simple_wrap.c -o問題を解決する1つの方法は、swig_optsを追加することです

simple_wrap.o \ temp.win32-2.7 \リリース\を構築:\ Python27 \は-ICには、

setup(name='matt_simple_test', version='1.0', 
     ext_modules=[Extension('_simple', ['simple.cc', 'simple.i'], 
        swig_opts=["-c++"], 
        include_dirs = [numpy.get_include(),'.'])]) 

別のオプションは、simple.ccに

extern "C" double dot(int n, double *a, int m, double *b) 
... 
extern "C" void create_list(int size, double *arr) 
... 
+0

はい、 'swig_opts'をsetupコマンドに追加すると、そのトリックが実行されました!どうもありがとう! – cabraut