swig与python

  当你觉得python慢的时候,当你的c/c++代码难以用在python上的时候,你可能会注意这篇文章。swig是一个可以把c/c++代码封装为python库的工具。(本文封装为python3的库)

文章结构

  • 整体看封装
  • 只使用python提供的c语言接口(Python.h)封装一个简单的c函数
  • 使用swig封装一个简单的c函数
  • 使用swig封装一个简单的c++类

整体看封装

c/c++实现功能 ==> c/c++封装c/c++函数 ==> 将前两者编译生成动态库 ==> python进一步封装;

手动封装c函数

我把实现和封装放在一个文件中(add.c)。

//add.c
#include <Python.h>

//定义一个C函数
int add(int a,int b){
    return a+b;
}

//包装c函数
static PyObject* _add_add(PyObject *self,PyObject *args){
    int a,b;
    PyArg_ParseTuple(args,"ii",&a,&b);            //把python参数转换为c函数
    return (PyObject*)Py_BuildValue("i",add(a,b));    //返回python对象的指针
}

//方法结构数组
static PyMethodDef _addMethods[]={        //
    { "add",_add_add,METH_VARARGS},
    {NULL,NULL}
};

//模块结构
static struct PyModuleDef _addModule={
    PyModuleDef_HEAD_INIT,        
    "_add",        //模块名
    "ADD",    //文档
    -1,            
    _addMethods        //PyMethodsDef实例 
};

//初始化函数
PyMODINIT_FUNC PyInit__add(){
    PyModule_Create(&_addModule);    //参数为PyModuleDef*
}

/*************************************************
 * (1)定义一个C函数,如add()。
 *
 * (2)包装c函数,如_add_add()。
 *
 * (3)方法结构数组,如_addMethods[]。
 *
 * (4)模块结构,如_addModule。
 *
 * (5)初始化函数PyInit_<module_name>(),如PyInit__add(),"_add"是模块名。
 *
 * 联系:
 * import ==> PyInit_<...>() ==> PyModule_Create() ==> PyModuleDef ==> PyMethodsDef ==> 包装函数 ==> c函数
 * ************************************************/

把add.c编译成动态库(_add.so)。

#Makefile
_add.so : add.c
    gcc -o _add.so add.c -fPIC -shared
clean :
    rm _add.so
_add.so已经是一个可用的python模块了,模块名为_add。
通过python进一步封装。这个库很简单,会感觉这一步行是多余的,但用swig都有这一步。
#add.py
from _add import *
自己创建add.c、Makefile和add.py,编译生成_add.so。

swig封装c函数

首先实现功能(add.c、add.h)。
//add.h
#ifndef ADD_H
#define ADD_H

int add(int,int);

#endif

//------------------------------------------------------------------------

//add.c
#include "add.h"
int add(int a,int b)
{ 
    return a+b;
}
swig需要一个输入文件(add.i)。
/* add.i */
%module add   /*模块名*/

%{
#include "add.h"
%}

int add(int,int);   /*add.h中的内容*/

又是Makefile。

#Makefile
_add.so : add.c add.h add_wrap.c
    gcc -shared -fPIC -o _add.so add.c add_wrap.c
add_wrap.c : add.i
    swig -python -py3 add.i
clean :
    rm _add.so add_wrap.c add.py
自己创建add.c、add.h、Makefile和add.i,编译生成add.py和_add.so。swig生成add.py和add_wrap.c,gcc将add.c和add_wrap.c编译成_add.so。在add_wrap.c搜索手动封装c函数的add.c文件中的相关结构便知其实质。

swig封装c++类

用c++实现一个向量类(Vector),两个文件——vector.hpp和vector.cpp。
//vector.hpp
#ifndef VECTOR_HPP
#define VECTOR_HPP

class Vector{
public:
    Vector(int,int);
    double abs();
    void display();
private:
    int x;
    int y;
};

#endif
//vector.cpp
#include "vector.hpp"
#include <iostream>
#include <cmath>
using namespace std;

Vector::Vector(int a,int b){  x=a;  y=b;  }
void Vector::display(){  cout << "(" << x << ',' << y << ')' << endl; }
double Vector::abs(){    return sqrt(x*x+y*y);  }

swig输入文件(vector.i)。

/* vector.i */
%module vector
%{
#include "vector.hpp"
%}

class Vector{
public:
    Vector(int,int);
    double abs();
    void display();
private:
    int x;
    int y;
};

还是Makefile。

#Makefile
_vector.so : vector.cpp vector.hpp vector_wrap.cxx
    g++ -shared -fPIC -I/usr/include/python3.4m -lpython3.4m -o _vector.so vector.cpp vector_wrap.cxx
vector_wrap.cxx : vector.i
    swig -c++ -python -py3 vector.i
clean :
    rm _vector.so vector_wrap.cxx vector.py

自己创建vector.cpp、vector.hpp、Makefile和vector.i,编译生成vector.py和_vector.so。

posted @ 2018-03-20 20:02  立秋了  阅读(20144)  评论(0编辑  收藏  举报