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", //文档
-,
_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.py
from _add import *
swig封装c函数
//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
swig封装c++类
//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。
swig与python的更多相关文章
- SWIG 和 Python——c/c++与脚本交互
C 和 C++ 被公认为(理当如此)创建高性能代码的首选平台.对开发人员的一个常见要求是向脚本语言接口公开 C/C++ 代码,这正是 Simplified Wrapper and Interface ...
- 使用swig在python中调用C++
1.安装swig 下载链接: http://www.swig.org/survey.html tar -xvf swig-.tar.gz ./configure --prefix=/usr/local ...
- SWIG 扩展Opencv python调用C++
osx:10.12 g++ 7.1 swig 3.0.12 opencv 3.2.0 SWIG是Simplified Wrapper and Interface Generator的缩写.是Pytho ...
- 如何实现Python调用C代码--python与C之间如何通信(swig)
转载: https://www.zhihu.com/question/23003213 1. C代码如何调用Python 1.1 test #include <Python.h> int ...
- use Swig to create C/C++ extension for Python
SWIG is a software development tool that simplifies the task of interfacing different languages to C ...
- python通过swig调用静态库
swig - Simplified Wrapper and Interface Generator swig可以支持python,go,php,lua,ruby,c#等多种语言的包裹 本文主要记录如何 ...
- 初识代码封装工具SWIG(回调Python函数)
这不是我最早使用swig了,之前在写Kynetix的时候就使用了swig为python封装了C语言写的扩展模块.但是当时我对C++还不是很了解,对其中的一些概念也只是拿来直接用,没有理解到底是什么,为 ...
- 学习笔记:安装swig+用SWIG封装C++为Python模块+SWIG使用说明
这段时间一直在摸索swing,用它来封装C++代码来生成python脚步语言.并总结了swing从安装到配置再到代码封装编译生成动态库的整个过程,下面这篇文章都是我在实际的运用中的一些经验总结,分享给 ...
- c++ python 交互之 swig
c++ python 交互之 swig 工作中准备用python 作为脚本语言来实现一些工作于是就研究 可以和c++ 交互的脚本语言 本来一开始用的lua 但是 lua本身API接口很少 要么自己需要 ...
随机推荐
- 手工执行sql tuning advisor和sql access advisor
sql tuning advisor:创建任务DECLARE my_task_name VARCHAR2(30); my_sqltext CLOB; BEGIN my_sqltext := 'SELE ...
- Windows文件批量重命名
选择要命名的文件 按F2,编辑名字 然后按回车就行了 电视剧命名,我认为这样足够了
- SOA架构
基于服务的SOA架构_后续篇 今天是元宵节,首先祝各位广大博友在接下来的光阴中技术更上一层,事事如意!(没能在元宵节发布,今天就补上吧) 昨天简单介绍了一下本人在近期开发过的一个电商购物平台的架构 ...
- c3p0-0.9.1.2.jar
C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展.目前使用它的开源项目有Hibernate,Spring等
- 关于notify() 和notifyAll() 一个需要注意的地方
notify() 和 notifyAll()都是唤醒其他正在等待同一个对象锁的线程. 下面是我遇到的一个问题,记下来,免得忘了. 直接上代码,有错误的代码: 代码描述:有一个Caculate类,类中又 ...
- 【剑指offer】字符串的组合
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/mmc_maodun/article/details/26405471 转载请注明出处:http:// ...
- git branch 相关操作总结 新建分支 删除分支 切换分支 查看分支
查看分支 (1) 查看本地分支 git branch 列出本地已经存在的分支,并且在当前分支的前面加*号标记,例如:localhost:website admin$ git branch* bran ...
- 小BAT解决大麻烦_某卡教室控制软件
@echo off mode con cols= lines= if "%1" == "h" goto begin mshta vbscript:)(windo ...
- Eclipse安装Sonarlint插件
这里安装的是Sonarlint3.6.插件安装非常简单.插件比Sonar更为简单快捷. 一.首先通过点击Eclipse上方Help菜单会出现一个下拉列表,点击其中的Eclipse MarketPlac ...
- 使用@AspectJ注解开发Spring AOP
一.实体类: Role public class Role { private int id; private String roleName; private String note; @Overr ...