2016-07-11 48 views
3

我一直在試圖建立一個Python 2.7環境在Windows 7下,這樣我可以編譯Python中使用C++的擴展。由於我是新手,我已經下載了一個簡單的例子here並逐字使用了這些文件。我在路徑中也有一個numpy.i文件。我用mingw(最新版本)和swig(v。3.0.10)設置了我的電腦,我的Python版本是2.7.9。我甚至用這個環境編譯了一個使用g ++的小型C++程序,沒有任何問題。與痛飲,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和蟒蛇都是32位? –

+0

我使用了標準的mingw安裝程序,我的理解是它應該是32位。有一點google搜索表明mingw-64是從mingw32分支出來的,我當然沒有安裝這個分支。 Python絕對是32位。 – cabraut

回答

1

在該編譯步驟輸入的文件被編譯爲C++代碼,並在symbol.cc功能將給出與C(名稱重整)不相容的符號。

C:\ MinGW的\ BIN \ gcc.exe -mdll -O -Wall -IC:\ Python27 \ LIB \站點包\ numpy的\核心\包括-I。 -IC:\ Python27 \ include -IC:\ Python27 \ PC -c simple.cc -o build \ temp.win32-2.7 \ Release \ simple.o

在此編譯步驟中,輸入文件被編譯爲C代碼和symbols.cc中函數的符號預計會有所不同。

C:\ MinGW的\ BIN \ gcc.exe -mdll -O -Wall -IC:\ Python27 \ lib中\ sitepackages \ numpy的\芯\包括-I。 -IC:\ Python27 \包括-IC:\ Python27 \ PC -c simple_wrap.c -o建立\ temp.win32-2.7 \發佈\ simple_wrap.o來解決這個問題

一種方法是添加swig_opts

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

另一種選擇是使用extern "C"在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