python封装c++_使用SIP对C++类进行Python封装

本人翻译,欢迎转载,赠人玫瑰,手留余香。

Using SIP

Bindings are generated by the SIP code generator from a number of specification files, typically with a .sip extension.  Specification files look very similar to C and C++ header files, but often with additional information (in the form of a directive or an annotation) and code so that the bindings generated can be finely tuned.

SIP可以从多个参数文件创建C/C++的Python绑定代码,参数文件以*.sip命名,类似于C/C++的.H头文件,原则上与C/C++的模块或头文件对应。

A Simple C++ Example

We start with a simple example.  Let’s say you have a (fictional) C++ library that implements a single class called Word.  The class has one constructor that takes a \0 terminated character string as its single argument.  The class has one method called reverse() which takes no arguments and returns a \0 terminated character string.  The interface to the class is defined in a header file called word.h which might look something like this:

开始一个C++的类,非常简单,实现字符的顺序翻转(接受一个字符串指针,包含的字符串必须以'\0'结尾),如下所示:

// Define the interface to the word library.

class Word {

const char *the_word;

public:

Word(const char *w);

char *reverse() const;

};

The corresponding SIP specification file would then look something like this:

对应的SIP文件如下:

// Define the SIP wrapper to the word library.

%Module word

class Word {

%TypeHeaderCode

#include 

%End

public:

Word(const char *w);

char *reverse() const;

};

Obviously a SIP specification file looks very much like a C++ (or C) header file, but SIP does not include a full C++ parser.  Let’s look at the differences between the two files.

SIP文件与C/C++头文件非常类似,但是不包括完整的C++语法解析支持。另外,还有一些特殊的指示符。包括:

The %Module directive has been added [1].  This is used to name the Python module that is being created, word in this example.

%Module 指示Python模块的名称,该名称在Python中以import name的方式使用。

The %TypeHeaderCode directive has been added.  The text between this and the following %End directive is included literally in the code that SIP generates.  Normally it is used, as in this case, to #include the corresponding C++ (or C) header file [2].

%TypeHeaderCode和%End之间是SIP创建时加入的包含文件指示。

The declaration of the private variable this_word has been removed. SIP does not support access to either private or protected instance variables.

该C++类在Python中的接口定义,已经移除了私有变量和保护变量等定义。

If we want to we can now generate the C++ code in the current directory by running the following command:

现在可以使用sip 创建出接口文件,如下:

sip -c . word.sip

However, that still leaves us with the task of compiling the generated code and linking it against all the necessary libraries.  It’s much easier to use theSIP build system to do the whole thing.

下一步,我们还需要对创建的代码进行编译。可以使用SIP的Build System来同时完成创建接口文件和库构建的过程。

Using the SIP build system is simply a matter of writing a small Python script. In this simple example we will assume that the word library we are wrapping and it’s header file are installed in standard system locations and will be found by the compiler and linker without having to specify any additional flags.  In a more realistic example your Python script may take command line options, or search a set of directories to deal with different configurations and installations.

创建SIP的构建系统主要是编写一个Python的脚本,非常简单。我们假设所有的库已经安装缺省位置安装(在更复杂的例子中,再解释如何处理不同的目录问题。)

This is the simplest script (conventionally called configure.py):

import os

import sipconfig

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

config = sipconfig.Configuration()

# Run SIP to generate the code.

os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "word.sip"]))

# 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 (e.g. the "lib" prefix on UNIX, or the

# ".dll" extension on Windows).

makefile.extra_libs = ["word"]

# Generate the Makefile itself.

makefile.generate()

Hopefully this script is self-documenting.  The key parts are theConfiguration and SIPModuleMakefile classes.  The build system contains other Makefile classes, for example to build programs or to call other Makefiles in sub-directories.

After running the script (using the Python interpreter the extension module is being created for) the generated C++ code and Makefile will be in the current directory.

To compile and install the extension module, just run the following commands [3]:

make

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值