利用SIP将C++封装为Python
来源:互联网 发布:网络举报网站 编辑:程序博客网 时间:2024/05/16 01:25
SIP 是为Python生成C++接口代码的工具,它与 SWIG 类似,但使用不同的接口格式。其思想起源于SWIG,主要是为将QT封装为Python创造,它用作创建 PyQt和 PyKDE ,并支持 Qt signal/slot 系统。
本文主要介绍在Window平台下,SIP的编译、安装,以及将C++代码生成为Python。本文是在安装了Python的前提下介绍,Python的安装包,可以上其官网下载,这里不再累赘!
1.SIP编译及安装
在SIP官网http://www.riverbankcomputing.com/software/sip/download下载SIP,其最新版本是4.16.7,我下载的是最新版本。解压SIP,打开Windows的cmd命令行,进入SIP根目录,输入python configure.py --show-platform查看支持的平台。4.16.7支持的平台如下:
从上面截图可以看出,SIP目前还不支持VS2012,但打开SIP根目录下的specs查看所支持的平台时发现,可以通过修改其平台配置文件做到支持VS2012。
将win32-msvc2010复制一份,将复制一份名称修改为win32-msvc2012,用文本编辑器打开win32-msvc2012,修改一下内容:
将QMAKE_COMPILER_DEFINES += _MSC_VER=1600 WIN32修改为QMAKE_COMPILER_DEFINES += _MSC_VER=1700 WIN32
将QMAKE_CFLAGS = -nologo -Zm200 -Zc:wchar_t-修改为QMAKE_CFLAGS = -nologo -Zm200 -Zc:wchar_t
回到SIP根,用文本编辑器打开configure.py将下面红色标注的内容替换为win32-msvc2012:
现在SIP支持VS2012了!
打开VS2012 开发人员命令提示,进入SIP根目录,依次执行以下命令:
python configure.py --platform win32-msvc2012
nmake
nmake install
第一条语句主要是生成Makefile文件
2.C++->Python
1)利用VS创建一个静态库的工程,主要实现字符串逆序,其内容如下:
.h文件
#ifndef PYSIPTEST_INCLUDE_H#define PYSIPTEST_INCLUDE_H#include <string>using namespace std;class Word {private:const string the_word;public:Word(const char *w) : the_word(w) {};const char* Reverse() const;};#endifCPP文件
#include <string>#include <algorithm>#include "PySIPTest.h"const char* Word::Reverse() const{string strTemp = the_word;std::reverse(strTemp.begin(), strTemp.end());return strTemp.c_str();}
2)编写configure.py文件和*.sip文件
<span style="font-size:18px;">// word.sip%Module wordclass Word {%TypeHeaderCode#include <string>#include <word.h>%End public: Word(const char* w); const char* reverse() const;};# configure.pyimport os, sipconfig, sys# The name of the SIP build file generated by SIP and used by the build system.build_file = "word.sbf"# Get the SIP configuration information.# Documentation: http://pyqt.sourceforge.net/Docs/sip4/build_system.htmlconfig = sipconfig.Configuration()# Run SIP to generate the code.cmd = " ".join([config.sip_bin, "-c", ".", "-b", build_file, "word.sip"])print( "running command " + cmd )os.system(cmd)# Create the Makefile.makefile = sipconfig.SIPModuleMakefile(config, build_file)# Add the library we are wrapping.# The name doesn't include any platform specific prefixes or extensions.makefile.extra_libs = ["word"]# Generate the Makefile itself.makefile.generate()</span>
运行configure.py,会生成一个makefile文件(直接用IDLE打开configure.py,按F5运行;或者命令行用python configure.py运行都可以)
执行nmake命令生成,nmake install 安装。
这样C++就会封装为Python,如果没有设置目录,就会安装默认Python目录\Lib\site-packages下。
3.运行
打开Python 的命令行,进行测试:
Python 2.X系列测试代码如下:
<span style="font-size:18px;">import wordword.Word("SIPTest").reverse()</span>
Python 3.x系列测试代码如下:
<span style="font-size:18px;">import wordword.Word(b"SIPTest").reverse()</span>
SIP很是强大,本文只是肤浅的介绍了SIP的一点东西,欢迎大家指正。
同时欢迎大家转载,转载请注明出处!
- 利用SIP将C++封装为Python
- 利用Boost.Python将C++代码封装为Python模块
- 利用Boost.Python将C++代码封装为Python模块
- SWIG and Python(用SWIG将C/C++封装为Python扩展模块)
- 利用python将pdf输出为txt
- 利用反射技术将查询结果封装为对象
- 利用cython将python转为c代码
- Python:利用内建函数将字符串转化为整数
- 利用Python将excel表格转换为json文件
- 利用python将文件转换为utf8 无BOM
- 利用python将json数据转换为csv格式
- Python:利用内建函数将字符串转化为整数
- 利用XSLT及C#.net将SVG转换为XAML
- 利用xerces-c库将xml串转换为类
- 利用流将数字转换为字符串 (C++)
- 第三十章 利用结果集元数据将查询结果封装为map
- JDBC_利用Java反射技术将查询结果封装为对象
- java_jdbc_利用结果集元数据将查询结果封装为map_MetaData
- 【状压DP】 ZOJ 3777 Problem Arrangement
- 华为2016届实习生招聘(武汉)笔试
- 【js思考】用js实现将输入的数字的每一位平方,输出也是数字形式
- 【大话设计】初涉之装饰、代理、工厂模式
- zzuli - 第七届校赛
- 利用SIP将C++封装为Python
- LitePal vs GreenDAO vs SQLOpenHelper 性能比較
- Java第一天整理和第二天
- GDB 调试基本命令
- 并查集_HDU_1325
- 代码重构
- Android---28---Handler、Loop、MessageQueue的工作原理:
- zmalloc.h:50:31: 致命错误:jemalloc/jemalloc.h(redis)
- ORA-19808: recovery destination parameter mismatch