1.5. SWIG wrap c++ into python

SWIG 是一个简单快捷地封装C/C++成更高级的语言的软件。它可以将 C/C++ 封装成包括Perl, PHP, Python, Tcl and Ruby在内的各种高级语言。

这里我整理一个linux下如何将C++封装成python的简单例子。

例子里有如下几个文件:

example.i
example.cxx
example.h
Makefile

example.i里面定义了封装后的python包里的函数和类。 example.h 和example.cxx是一个完整的C++程序包, 这里面的函数以及类不一定要在example.i里面出现。 也就是说example.i里面只要写上我们在python里面要用的函数和类就够了。

如果你想要封装的是一个复杂的C++ API,而使用的过程中又不需要整个API的所有功能,那么就可以自己写一个c++类调用你要用 的API,然后把自己的这个类封装成python包。这就体现了python的一个特性——“胶水语言”。

据说用SWIG封装可能会降低代码的执行效率,如果考虑效率的话那就只好用标准的更底层的封装方法了。那样的话需要大量的重写你的代码。

example.i

%module example
%{
#include "example.h"
%}

class caller{
public:
  caller();
  ~caller();
  int run();
};

example.cxx

#include "example.h"

caller::caller(){
}
caller::~caller(){
}

int caller::run(){
  cout<<"running"<<endl;
  return 0;
}

example.h

#include <iostream>
#include <iomanip>
#include <vector>
#include <list>
using namespace std;

class caller{
public:
  int i;
  caller();
  ~caller();
  int run();
};

Makefile


all: example

clean:
	rm -f example.py example.o  example_wrap.cxx example_wrap.o _example.so example.pyc\

example:example.cxx 
	swig -c++ -python example.i
	g++ -O2 -c -fPIC example.cxx -I../include
	g++ -O2 -c -fPIC  -I/usr/include/python2.7 example_wrap.cxx 
	g++ -shared example.o example_wrap.o -o _example.so