Reputation: 405
I'm trying to wrap some c++ code with python using swig and I need to send NumPy arrays into the c++ vector class for some processing.
My problem is that I don't seem to be able to access "numpy.i" in my swig .i file.
How can I import/include numpy.i?
add_vector.i
%module add_vector
%{
#define SWIG_FILE_WITH_INIT
#include "add_vector.h"
%}
%include "numpy.i"
%init %{
import_array();
%}
%include std_vector.i
%template(vecInt) std::vector<int>;
%include "add_vector.h"
Makefile
all:
rm -f *.so *.o *_wrap.* *.pyc *.gch add_vector.py
swig -c++ -python add_vector.i
g++ -O0 -g3 -fpic -c add_vector_wrap.cxx add_vector.h add_vector.cpp -I/home/tools/anaconda3/pkgs/python-3.7.3-h0371630_0/include/python3.7m/
g++ -O0 -g3 -shared add_vector_wrap.o add_vector.o -o _add_vector.so
tester.py
import add_vector as vec
import numpy as np
a = np.array([1,2,3])
b = np.array([4,5,6])
c = np.empty(len(a))
vec.add(c,a,b)
print('c:', c)
Output:
rm -f *.so *.o *_wrap.* *.pyc *.gch add_vector.py
swig -c++ -python add_vector.i
add_vector.i:7: Error: Unable to find 'numpy.i'
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1
I'm using debian, in case that matters.
Thanks!
Upvotes: 1
Views: 1446
Reputation: 734
Copy numpy.i
into the same folder as add_vector.i
.
Or use the command line option -lifile
and give it the path to your numpy.i file.
swig -l/path/to/numpy.i ...
For a list of SWIG command line options, see http://www.swig.org/Doc3.0/SWIGDocumentation.html#SWIG_nn2
Upvotes: 2