利用SIP将C++封装为Python

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;
};

#endif
CPP文件

#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 word

class Word {

%TypeHeaderCode
#include <string>
#include <word.h>
%End

 public:
    Word(const char* w);
    const char* reverse() const;
};

# configure.py

import 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.html
config = 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 word
word.Word("SIPTest").reverse()</span>

Python 3.x系列测试代码如下:

<span style="font-size:18px;">import word
word.Word(b"SIPTest").reverse()</span>

SIP很是强大,本文只是肤浅的介绍了SIP的一点东西,欢迎大家指正。


同时欢迎大家转载,转载请注明出处!









郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。