python - SWIG2 with NUMPY -
i trying build swig2.0 interface c++ class. requirement need able pass numpy array python c++ class.
i have simple c++ class :
// // file : matmanip.h // #ifndef __mat_manip_h #define __mat_manip_h #include <iostream> class matmanip { public: matmanip(); // matrix of length d void set_matrix( float * m, int d ); }; #endif
the .cpp file :
#include <iostream> #include "matmanip.h" // file : matmanip.cpp matmanip::matmanip() { std::cout << "constructor\n"; } void matmanip::set_matrix( float * m, int d ) { for( int i=0 ; i<d ; i++ ) std::cout << "m[" << << "] = " << m[i] << std::endl; }
my swig interface file (matmanip.i). obtained numpy.i file https://github.com/numpy/numpy/blob/master/tools/swig/numpy.i :
%module matmanip %{ #define swig_file_with_init #include "matmanip.h" %} %include "numpy.i" %init %{ import_array(); %} %apply( float * in_array1, int dim1) {(double * seq, int n) }; %include "matmanip.h"
here compile:
$ swig -c++ -python matmanip.i $ g++ -fpic -c matmanip.cpp $ g++ -fpic -c matmanip_wrap.cxx -i /usr/include/python2.7 $ g++ -shared matmanip.o matmanip_wrap.o -o _matmanip.so $ ls matmanip.cpp matmanip.o _matmanip.so numpy.i matmanip.h matmanip.py matmanip_wrap.cxx matmanip.i matmanip.pyc matmanip_wrap.o
until point work! can see _matmanip.so.
however, try import matmanip in python here error
$ python >>> import numpy np >>> matmanip import matmanip >>> m = matmanip() constructor >>> m.set_matrix( np.array( [2.0, 3.4, 5.8] ) ) traceback (most recent call last): file "<stdin>", line 1, in <module> file "matmanip.py", line 82, in set_matrix def set_matrix(self, *args): return _matmanip.matmanip_set_matrix(self, *args) typeerror: matmanip_set_matrix() takes 3 arguments (2 given) >>>
i puzzled! swig version 2.0.12. numpy 1.12.1. python version 2.7.12. g++ version 5.4.0. on ubuntu 16.04.
Comments
Post a Comment