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接口很少 要么自己需要 ...
随机推荐
- sqlite 用法整理
转载:http://blog.csdn.net/zhaoweixing1989/article/details/19080593 先纪录到这,以后慢慢整理. 1. 在Android下通过adb she ...
- 基于SAP Kyma的订单编排增强介绍
尽管有一万个舍不得,2018年还是无可挽回地离我们远去了. 唯有SAP成都研究院的同事和我去年在网络上留下的这些痕迹,能证明2018年我们曾经很认真地去度过每一天: SAP成都研究院2018年总共87 ...
- 简单封装的ajax请求
简单封装了一个ajax请求,做一下统一处理,少写重复代码,只是一个初步的代码,没有经过优化. $.extend({ myAjax: function (option, rollBack) { var ...
- ThreadLocal介绍
作者:知乎用户链接:https://www.zhihu.com/question/23089780/answer/62097840来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- Netty问题小结
1.问题:高并发时连接不够:java.net.SocketException: No buffer space available (maximum connections reached?): co ...
- Type Systems
This section deals with more theoretical aspects of types. A type system is a set of rules used by a ...
- ecshop 中如何禁用右键和F12
找到 网站根目录/themes/js/common.js,在最后加入如下代码: //禁用右键和F12 //方法一 document.oncontextmenu = function () { retu ...
- 搭建一个redis高可用系统
一.单个实例 当系统中只有一台redis运行时,一旦该redis挂了,会导致整个系统无法运行. 单个实例 二.备份 由于单台redis出现单点故障,就会导致整个系统不可用,所以想到的办法自然就是备份( ...
- 使用命令导入、导出mysql数据
1.导出全部数据库 利用mysqldump的—all-databases参数可以一口气把你数据库root用户下的所有数据库一口气导出到一个sql文件里.然后,重装系统后使用source命令可以再一口气 ...
- 安全过滤javascript,html,防止跨脚本攻击
本文改自: http://blog.51yip.com/php/1031.html 用户输入的东西是不可信认的,例如,用户注册,用户评论等,这样的数据,你不光要做好防sql的注入,还要防止JS的注入, ...