本文所用例子是 swig2.0-examples里Python的class目录里的代码。python
具体的c++ 代码以下所示。linux
/* File : example.h */ class Shape { public: Shape() { nshapes++; } virtual ~Shape() { nshapes--; }; double x, y; void move(double dx, double dy); virtual double area(void) = 0; virtual double perimeter(void) = 0; static int nshapes; }; class Circle : public Shape { private: double radius; public: Circle(double r) : radius(r) { }; virtual double area(void); virtual double perimeter(void); }; class Square : public Shape { private: double width; public: Square(double w) : width(w) { }; virtual double area(void); virtual double perimeter(void); }; /* 文件名是example.cxx*/ /* File : example.c */ #include "example.h" #define M_PI 3.14159265358979323846 /* Move the shape to a new location */ void Shape::move(double dx, double dy) { x += dx; y += dy; } int Shape::nshapes = 0; double Circle::area(void) { return M_PI*radius*radius; } double Circle::perimeter(void) { return 2*M_PI*radius; } double Square::area(void) { return width*width; } double Square::perimeter(void) { return 4*width; }
python接口的文档以下所示,后缀一般都是python.i.c++
module 对应着具体so的名字。在生成的py文件里。会import _{module}.so。bash
/* File : example.i */ %module example %{ #include "example.h" %} /* Let's just grab the original header file here */ %include "example.h"
调用 下行命令生成 example_wrap.cxx, 和example.py 文件。python2.7
xxx@xxx-K43SJ:/usr/share/doc/swig2.0-examples/python/class$ sudo swig2.0 -c++ -python example.i
调用以下命令生成example.ossh
sudo g++ -O2 -fPIC -c example.cxx
调用以下命令生成example_wrap.o -I 是用来连接example_wrap.cxx 里的Python.h的头文件。ui
若是没有包含这个头文件。则apt-get 下个python的dev包。this
sudo g++ -O2 -fPIC -c example_wrap.cxx -I /usr/include/python2.7/
再调用以下命令生成c++ 的so文件。so文件的名字文件对应着.i文件里的module并且要加_code
sudo g++ -shared *.o -o _example.so
这时候,咱们调用pythonorm
xxx@xxx-K43SJ:/usr/share/doc/swig2.0-examples/python/class$ python Python 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from example import * >>> dir() ['Circle', 'Circle_swigregister', 'Shape', 'Shape_swigregister', 'Square', 'Square_swigregister', '__builtins__', '__doc__', '__name__', '__package__', 'cvar'] >>> c = Circle(10) >>> dir(c) ['__class__', '__del__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__swig_destroy__', '__swig_getmethods__', '__swig_setmethods__', '__weakref__', '_s', 'area', 'move', 'nshapes', 'perimeter', 'this', 'x', 'y'] >>> c.area() 314.1592653589793 >>> c.perimeter() 62.83185307179586 >>> c.nshapes() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not callable >>> c.nshapes 1 >>>
还有另一种编译的方式。用distutils来编译。写脚本以下,setup.py
#!/usr/bin/env python """ setup.py file for SWIG example """ from distutils.core import setup, Extension example_module = Extension('_example', sources=['example_wrap.cxx', 'example.cxx'], ) setup (name = 'example', version = '0.1', author = "SWIG Docs", description = """Simple swig example from docs""", ext_modules = [example_module], py_modules = ["example"], )
调用以下命令编译获得py文件和so文件。
sudo python setup.py build_ext --inplace
接下来讲说编译多个.cxx或.cpp文件
用g++ 文件的方式编译连接so时在这个命令里把其余.cxx文件也一块儿编译成.o,也能够将*_wrap.cxx一块儿编译成.o ,这时候也要添加Python.h的连接路径。接下来把他们连接成so。就能够了。
sudo g++ -O2 -fPIC -c *.cxx
第二种方法。
在soureces里添加其余要添加的cxx。Extension还能够添加其余要连接的so。
example_module = Extension('_example', sources=['example_wrap.cxx', 'example.cxx', 'hello.cxx'], )