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. ETL testing

    https://www.tutorialspoint.com/etl_testing/index.htm querysurge-installer-6.0.5-linux-x64  测试ETL的工具.

  2. css3 加载动画效果

    Loading 动画效果一           HTML 代码: <div class="spinner"> <div class="rect1&quo ...

  3. mysql使用模板解决旧数据处理,默认初始化数据的通用方法!

    一 业务介绍 先来看看我这得大致业务需求,这次业务比较简单: 即从现在开始,每次new一个爷爷都需要默认初始化给这个爷爷三个儿子(子表,爷爷id去关联),并在初始化每个儿子的同时再给每个儿子初始化若干 ...

  4. HDU 2825 Wireless Password

    题目链接:HDU-2825 题意:给出m个单词,要构造出满足包含其中大于等于k个单词的字符串,字符只包括小写字母,问长度为n的这样的串有多少个. 思路:令dp[i][j][k]表示当前已经构造了i个字 ...

  5. docker stack 部署 filebeat

    =============================================== 2018/7/21_第3次修改                       ccb_warlock 更新 ...

  6. vue引入elementUI 报错

    在main.js里面引入import 'element-ui/lib/theme-default/index.css'中报错,无法启动项目,这是把package.json里面的webpack改成 1 ...

  7. 数据库-mysql数据连接

    一:Mysql 连接的使用 在前几章节中,我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据. 本章节我们将向大家介绍如何使用 MySQL 的 JO ...

  8. PV操作2011

  9. 牛客红包OI赛 B 小可爱序列

    Description 链接:https://ac.nowcoder.com/acm/contest/224/B 来源:牛客网 "我愿意舍弃一切,以想念你,终此一生." " ...

  10. wpf 在Popup内的TextBox 输入法 不能切换

    切换输入法 输入不了中文 [DllImport("User32.dll")] public static extern IntPtr SetFocus(IntPtr hWnd); ...