首先,感謝你們在過去幾年中在這個論壇上給予我的所有答案,但是今天我找不到明確的答案,所以我儘管現在是時候發佈了。在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