2013-10-22 42 views
2

首先,感谢你们在过去几年中在这个论坛上给予我的所有答案,但是今天我找不到明确的答案,所以我尽管现在是时候发布了。在Windows中编译用于python的SWIG包装器

我设法编译并在我的Debian DISTRIB运行C++代码test5.cpp,其包装(也许是不正确的字)为Python模块Amod.py与SWIG(从而需要一个“翻译”文件test5.i。我使用和进口成功将模块数据分析到其他更复杂的Python代码(基本上操纵numpy数组,matplotlib等)。

现在我想在Windows计算机上运行相同的代码,但随后python无法再导入模块,librairy文件_Amod.so是一个.so文件,而不是windows的预期的.pyd,但是我找不到一个快速简单的方法来在Windows上重新编译它,我挖掘了CodeBlocks文档,但是它的代码有缺陷,我迷路了(http://wiki.codeblocks.org/index.php?title=Adding_support_for_non_C/C%2B%2B_files_to_the_build_system

基本上我想运行Windows相当于下面的工作代码(至极,我希望能帮助初学者痛饮):

编译:

swig -c++ -python test5.i 
g++ -fPIC -c test5.cpp 
g++ -fPIC -c test5_wrap.cxx -I/usr/include/python2.7 
g++ -shared test5.o test5_wrap.o -o _Amod.so 

至极软件和编译器应该我使用和如何,而不会浪费我的时间? (我已经获得了CodeBlocks)非常感谢。

因为,如果需要的信息,所述test5.i是以下,封装C++中numpy的阵列排列,加入一些内联(替换功能),用于检查目的(全部是挖成与血液和泪液痛饮帮助):

/* %module module_name is used in compilation as: g++ -shared main.o main_wrap.o -o _module_name.so (with the underscore)*/ 
%module Amod 

%{ 
/* Put header files here or function declarations like below. The #define SWIG_FILE_WITH_INIT line inserts a macro that specifies that the resulting C file should be built as a python extension, inserting the module init code. check http://www.swig.org/Doc1.3/Python.html#Python_nn3 */ 
#define SWIG_FILE_WITH_INIT 
#include "test5.h" 
%} 

/* numpy.i and import_array() allow SWIG to manipulate C++ pointer (like double* ivec) like numpy array, because SWIG doesn't know a priori, that the pointer refer to an array. check http://docs.scipy.org/doc/numpy/reference/swig.interface-file.html */ 

%include "numpy.i" 

%init %{ 
import_array(); 
%} 


/* C++ function arg must fits the typemap directives available in numpy.i. */ 

%apply (int DIM1, double* INPLACE_ARRAY1) {(int len1, double* ivec),(int len2, double* ovec),(int len3, double* gauss)}; 
%rename (abel) abel_swig; 
%exception abel_swig { 
    $action 
    if (PyErr_Occurred()) SWIG_fail; 
} 
%inline %{ 
void abel_swig(int len1, double* ivec, int len2, double* ovec, int algo, double alpha) { 
    if (len1 != len2) { 
     PyErr_Format(PyExc_ValueError,"Arrays of lengths (%d,%d) given",len1, len2); 
     return; 
    } 
    memset(ovec, 0, len1*sizeof(double)); 
    return abel(len1, ivec, len2, ovec, algo, alpha); 
} 
%} 


%rename (convol_gauss) convol_gauss_swig; 
%exception convol_gauss_swig { 
    $action 
    if (PyErr_Occurred()) SWIG_fail; 
} 
%inline %{ 
void convol_gauss_swig(int len1, double* ivec, int len2, double* ovec, int len3, double* gauss) { 
    if ((len1 != len2)||(len1 != len3)) { 
     PyErr_Format(PyExc_ValueError,"Arrays of lengths (%d,%d,%d) given, they must be the same",len1, len2, len3); 
     return; 
    } 
    memset(ovec, 0, len1*sizeof(double)); 
    return convol_gauss(len1, ivec, len2, ovec, len3, gauss); 
} 
%} 


/* Put header files here or function declarations like below */ 

%include "test5.h" 

和头test5.h

#ifndef TEST5_H_INCLUDED 
#define TEST5_H_INCLUDED 
#include <cstring> 
#include <iostream> 
#include <cmath> 
void convol_gauss(int size, double* ivec, int size2, double* ovec, int size3, double* gauss); 
void abel(int len1, double* ivec, int len2, double* ovec, int algo); 

#endif // TEST5_H_INCLUDED 

回答

1

好吧,我设法做到这一点。

1-当心你。我的文件编码为UTF-8,因为它防止痛饮如果Python来编译为不同的编码。(#error Must use Python with unicode enabled

2-下载痛饮用于Windows的www.swig .ORG和安装MinGW的

3-类型在CMD或Powershell的

swig -c++ -python test5.i 
g++ -c -Wall test5.cpp 
g++ -c -Wall test5_wrap.cxx -I C:\Python27\include -I C:\Python27\Lib\site-packages\numpy\core\include\ 
g++ -Wall -shared -I C:\Python27\include -I C:\Python27\Lib\site-packages\numpy\core\include\ test5.o test5_wrap.o -o _Amod.pyd -L C:/Python27/libs/ -lpython27 

-I C:\Python27\Lib\site-packages\numpy\core\include\解决错误: fatal error : numpy\arrayobject.h no such file or directory

-L C:/Python27/libs/ -lpython27解决错误: undefined reference to _imp__Py...

相反的是Linux的编译,程序库C:/Python27/libs/python27.lib必须小心链接,以及标题的directores(C:\ Python27 \ include和numpy C:\ Python27 \ Lib \ site-packages \ numpy \ core \ include) 希望它可以帮助,但我没有感觉这是一个非常干净的工作。考虑到我遇到的错误,请参阅this previous post