安装完Swig后,需要编辑两个文件,即example.i和example.cpp
其中example.i为:
%module example_vector
%include "std_string.i"
%include "std_vector.i"
%{
using namespace std;
vector vector_int2str(vector input);
%}
namespace std {
%template(StringVector) vector;
%template(IntVector) vector;
}
using namespace std;
vector vector_int2str(vector input);
其中example.cpp为:
#include#include using namespace std; vector vector_int2str(vector input) { vector result; for (vector ::const_iterator it = input.begin(); it != input.end(); ++it) { result.push_back(to_string(*it)); } return result; }
生成后使用conda进入python2环境,以此执行下面的编译命令
swig -c++ -python example.i g++ -fPIC -c example.cpp example_wrap.cxx -I/home/l/miniconda3/envs/python2/include/python2.7 g++ -shared example.o example_wrap.o -o _example_vector.so
其中-I/home/l/miniconda3/envs/python2/include/python2.7是python的安装目录,可以进入conda环境后使用下面的命令查找
python-config --cflags



