swig - Simplified Wrapper and Interface Generator

swig可以支持python,go,php,lua,ruby,c#等多种语言的包裹

本文主要记录如何使用swig创建一个可供python中调用静态库接口

首先手上有一个头文件(.h)以及一个静态库文件(.a),这是常见的api分发方式

libcode.a code.h

看一下code.h中的内容:

int encode(const char* salt, int version, const char* from, string& to);

int decode(const char* salt, int version, const char* from, string& to);

可以知道包含了一个加密和一个解密的函数,我们这里只是用解密来举例

为了使用swig进行包裹,我们先创建一个自己的头文件和实现文件coding.cpp coding.h

看一下内容:

coding.cpp

#include "code.h"

using std::string;
const int version = 1; string decode(int version, string salt, string from)
{
string to;
int ret = decode(salt.c_str(), version, from.c_str(), to);
if(ret != 0)
{
return "";
}
return to;
}

coding.h

#include <string>

std::string decode(int version, std::string salt, std::string from);

接下来我们定义我们的swig入口文件coding.i

%module coding
%include "std_string.i" %{
#define SWIG_FILE_WITH_INIT
#include "coding.h"
%} std::string decode(int version, std::string salt, std::string from);

注意这里由于使用了std::string,所以必须%include "std_string.i"

然后,我们使用swig来获取到包裹文件

swig -c++ -python coding.i

执行后会得到coding_wrap.cxx文件以及coding.py文件

接下来我们编译出几个目标文件:

g++ -O2 -fPIC -c coding.cpp
g++ -O2 -fPIC -c coding_wrap.cxx -I/usr/include/python2.7

得到coding.ocoding_wrap.o

然后链接得到动态库

g++ -lssl -shared coding.o coding_wrap.o libcode.a -o _coding.so

注意这边链接的时候使用了-lssl选项是因为加解密的静态库的实现中使用openssl

然后得到了_coding.so这个动态库

至此,我们所需要的两个文件_coding.socoding.py就都已经拿到了,下面的python代码演示了如何使用

import coding
coding.decode(1, "xxxxx-salt-xxxxx", "xxxxxxxxxx-encoded-text-xxxxxxxxxxxx")

参考:

http://www.swig.org/Doc1.3/Python.html#Python_nn22

http://www.swig.org/Doc1.3/Library.html#Library_nn14

http://www.deep-nlp.com/?p=31

python通过swig调用静态库的更多相关文章

  1. # 2017-2018-2 20155228 《信息安全系统设计原理》 使用VirtualStudio2008创建和调用静态库和使用VirtualC++6.0创建和调用动态库

    使用virtual c++ 6.0创建和调用动态库 不得不说一下关于环境的问题 只要我打一个响指,一半的安装在win7上的VC6.0都会因为兼容性问题直接崩掉 懒得研究怎么解决兼容性的问题了,直接开一 ...

  2. VC调用静态库、动态库

    静态库 // 相对路径 或者 绝对路径 #include "yourlib.h" //相对路径 或者 绝对路径 #pragma comment(lib, "yourlib ...

  3. Delphi XE10在 Android下调用静态库a文件

    Delphi Seatle can link Delphi project with Static library files(*.a): 1.at Delphi IDE, Add the " ...

  4. 16位汇编 多文件 intel汇编 编译器masm5.0 调用子程序库即静态库的自定义函数 WINDOWS

    ;以下是16位汇编 创建静态库,并调用静态库中的函数 ;多文件汇编格式 ;编译方法(此处用的是masm 5.0,如果是其他的编译器,有可能不能编译) ;第一种,编译方法 ;1.masm main.as ...

  5. C、C++的Makefile的编写以及动、静态库的制作调用(包括MAC地址的获取及MD5加密)

    一.C代码 静态库 四个.h.c文件 add.h #ifndef ADD_H #define ADD_H int add(int a,int b); #endif add.c #include < ...

  6. windows 创建和调用 动态库,静态库

    windows创建和调用静态库 // MathFuncsLib.h namespace MathFuncs { class MyMathFuncs { public: // Returns a + b ...

  7. Qt生成和调用动态库dll,和静态库.a(windows和linux通用)

    系统1:ThinkPad T570.Windows10.QT5.12.2(Qt Creater 4.8.2)一.动态库.dll的创建和调用1.在qtcreater中按如下步骤创建动态库,动态库名为my ...

  8. iOS开发中静态库制作 之.a静态库制作及使用篇

    iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...

  9. 静态库介绍与简单演练及同名资源冲突解决(.a格式的静态库)

    1.静态库和动态库都是闭源库,不公开源代码. 静态库:.a和.framework 动态库:.dylib和.framework(iOS9取消了.dylib,使用.tbd替代) 2.静态库和动态库在使用上 ...

随机推荐

  1. thinkphp 带条件分页查询

    thinkphp 带条件分页查询:form表单传值时候,method='get'. 用 get 传值

  2. php Only variables should be passed by reference 报错问题

    这个错误是变量引用引起的非致命错误,可修改php.ini文件的error_reporting = E_ALL & E_NOTICE 使其屏蔽此错误

  3. UNIX环境高级编程 第1章 UNIX基础知识

    所有操作系统都为运行在它之上的程序提供各种服务,典型的服务包括:执行新程序.打开文件.读写文件.分配存储空间.提供时间等. UNIX体系结构 严格来说,操作系统是一种软件,它控制计算机硬件资源,提供程 ...

  4. Css中实现一个盒子固定宽度,另一个盒子宽度自适应的方法

    Css中实现一个盒子固定宽度,另一个盒子宽度自适应的方法 网上方法很多,个人认为以下两种思想是最为常用的. 一种是让第一个盒子脱离文档流,第二个盒子离左边有一定距离. 第二种方法是使用flex布局,不 ...

  5. imperva 更改web界面的密码

    通过SSH作为用户根登录到MX(或通过另一个用户并提升) 运行命令“su oracle” //首先切换到oracle用户 sqlplus secure/(密码)   /用此命令登录到数据库     s ...

  6. c++ 类的构造顺序

    在单继承的情况下,父类构造先于子类,子类析构先于父类,例: class A { public: A() { cout << "A" << endl; } ~ ...

  7. vue总结 01基础特性

    最近有时间来总结一下vue的知识: 一.vue.js 被定义成一个开发web界面的前端库,是一个非常轻量的工具.vue.js本身具有响应式和组件化的特点. 我们不需要在维护视图和数据的统一上花费大量的 ...

  8. JMeter接口&性能测试

    JMeter接口测试 目前最新版本发展到5.0版本,需要Java7以上版本环境,下载解压目录后,进入\apache-jmeter-5.0\bin\,双击ApacheJMeter.jar文件启动JMem ...

  9. 2017 MoveIt!更新 ros indigo

    First MoveIt! Update in 2017. Using it on NEXTAGE pt.1 2017 MoveIt! update pt.2; Stopping motion on ...

  10. 《精通Python设计模式》学习结构型之装饰器模式

    这只是实现了Python的装饰器模式. 其实,python的原生的装饰器的应用比这个要强,要广的. ''' known = {0:0, 1:1} def fibonacci(n): assert(n ...