(1) 运行SWIG 安装SWIG成功后,使用以下格式的命令运行: $ swig [ options ] filename 选项包括: -chicken Generate CHICKEN wrappers -csharp Generate C# wrappers -guile Generate Guile wrappers -java Generate Java wrappers -mzscheme Generate Mzscheme wrappers -ocaml Generate Ocaml wrappers -perl Generate Perl wrappers -php Generate PHP wrappers -pike Generate Pike wrappers -python Generate Python wrappers -ruby Generate Ruby wrappers -sexp Generate Lisp S-Expressions wrappers -tcl Generate Tcl wrappers -xml Generate XML wrappers -c++ Enable C++ parsing -Dsymbol Define a preprocessor symbol -Fstandard Display error/warning messages in commonly used format -Fmicrosoft Display error/warning messages in Microsoft format -help Display all options -Idir Add a directory to the file include path -lfile Include a SWIG library file. -module name Set the name of the SWIG module -o outfile Name of output file -outdir dir Set language specific files output directory -swiglib Show location of SWIG library -version Show SWIG version number 这只是命令行选项的一个子集。对每种目标语言都有各自附加的选项。可以使用命令"swig -help or swig -lang -help"查看全部。
filename是用户编写的SWIG标记脚本文件。
(2) SWIG的输入 输入为编写的脚本文件,通常后缀为.i或.swg。 通常该脚本文件的格式如下: %module mymodule %{ #include "myheader.h" %} // Now list ANSI C/C++ declarations int foo; int bar(int x); ... 模块名使用"%module"(或-module命令行选项)进行标记。这个标记必须在文件的开始出现,用于命名目标扩展模块。如果选择在命令行提供,则不需要"%module"标记。 在"%{ ... %}"中进行头文件和其它特殊的声明(如% rename、% ignore等)。它将被逐字的复制到SWIG创建的wrapper文件中。
2. SIP(A Tool for Generating Python Bindings for C and C++ Libraries) Python-SIP是一个用于为Python生成C++接口的工具。它类似于SWIG,但使用了一个不同的接口格式。它用于建造PyQt 和PyKDE,支持Qtsignal/slot机制。 SIP是一个为C/C++库自动生成Python绑定的工具。SIP最初于1998年为了PyQt(Python绑定到Qt GUI工具集)而开发的,但也适合于生成C/C++库的绑定。 SIP的命名是因为它最初是作为一个小的SWIG出现的。与SWIG不同,SIP实现是为了尽可能最小化的实现Python与C/C++的整合。
(2) 运行: SIP命令行语法如下: $ sip [options] [specification] 其中,specification是模块规范文件(通常后缀为sip)的文件名。若被省略则默认为stdin。 命令行选项如下: -h Display a help message.
-V Display the SIP version number.
-a file The name of the Scintilla API file to generate. This file contains a description of the module API in a form that the Scintilla editor component can use for auto-completion and call tips. By default the file is not generated.
-b file The name of the build file to generate. This file contains the information about the module needed by the SIP build system to generate a platform and compiler specific Makefile for the module. By default the file is not generated.
-c dir The name of the directory (which must exist) into which all of the generated C or C++ code is placed. By default no code is generated.
-d file The name of the documentation file to generate. Documentation is included in specification files using the %Doc and %ExportedDoc directives. By default the file is not generated.
-e Support for C++ exceptions is enabled. The causes all calls to C++ code to be enclosed in try/catch blocks and C++ exceptions to be converted to Python exceptions. By default exception support is disabled.
-I dir The directory is added to the list of directories searched when looking for a specification file given in an %Include or %Import directive. This option may be given any number of times.
-j number The generated code is split into the given number of files. This make it easier to use the parallel build facility of most modern implementations of make. By default 1 file is generated for each C structure or C++ class.
-r Debugging statements that trace the execution of the bindings are automatically generated. By default the statements are not generated.
-s suffix The suffix to use for generated C or C++ source files. By default .c is used for C and .cpp for C++.
-t tag The SIP version tag (declared using a %Timeline directive) or the SIP platform tag (declared using the %Platforms directive) to generate code for. This option may be given any number of times so long as the tags do not conflict.
-w The display of warning messages is enabled. By default warning messages are disabled.
-x feature The feature (declared using the %Feature directive) is disabled.
-z file The name of a file containing more command line options.
(3) 输入: 输入为规范文件。 我们通过一个简单的规范文件示例来说明规范文件语法。假定有一个C++库实现了Word类。类有一个构造器,构造器以一个\0结束的字符串作为唯一参数。类有一个叫做reverse()的无参方法,它返回一个\0结束的字符串。 类的接口在头文件word.h中定义,如下所示: // Define the interface to the word library. class Word { const char *the_word; public: Word(const char *w); char *reverse() const; };
相应的SIP规范文件如下所示: // Define the SIP wrapper to the word library. %Module word 0 class Word { %TypeHeaderCode #include "word.h" %End public: Word(const char *); char *reverse() const; };