python 利用swig 调用c++的接口。

本文所用例子是 swig2.0-examples里Python的class目录里的代码。

具体的c++ 代码如下所示。

/* 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.

module 对应着具体so的名字。在生成的py文件里。会import _{module}.so。

/* 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 文件。

xxx@xxx-K43SJ:/usr/share/doc/swig2.0-examples/python/class$ sudo swig2.0 -c++ -python example.i 

调用如下命令生成example.o

sudo g++ -O2 -fPIC -c example.cxx

调用如下命令生成example_wrap.o -I 是用来链接example_wrap.cxx 里的Python.h的头文件。

如果没有包含这个头文件。则apt-get 下个python的dev包。

sudo g++ -O2 -fPIC -c example_wrap.cxx -I /usr/include/python2.7/ 

再调用如下命令生成c++ 的so文件。so文件的名字文件对应着.i文件里的module而且要加_

 sudo g++ -shared *.o -o _example.so

这时候,我们调用python

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'],
                           )

 

转载于:https://my.oschina.net/u/2461850/blog/846338

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值