SWIG Tutorial.docx
《SWIG Tutorial.docx》由会员分享,可在线阅读,更多相关《SWIG Tutorial.docx(10页珍藏版)》请在冰豆网上搜索。
SWIGTutorial
想要快一点的方法吗,使用swig吧。
假设你有一些c你想再加入Tcl,Perl,Python,JavaandC#.。
举例来说有这么一个文件example.c
/*File:
example.c*/
#include
doubleMy_variable=3.0;
intfact(intn){
if(n<=1)return1;
elsereturnn*fact(n-1);
}
intmy_mod(intx,inty){
return(x%y);
}
char*get_time()
{
time_tltime;
time(<ime);
returnctime(<ime);
}
接口文件
现在,为了增加这些文件到你喜欢的语言中,你需要写一个接口文件(interfacefile)投入到swig中。
这些Cfunctions的接口文件可能如下所示:
/*example.i*/
%moduleexample
%{
/*Putheaderfileshereorfunctiondeclarationslikebelow*/
externdoubleMy_variable;
externintfact(intn);
externintmy_mod(intx,inty);
externchar*get_time();
%}
externdoubleMy_variable;
externintfact(intn);
externintmy_mod(intx,inty);
externchar*get_time();
建立Tcl模块
在UNIX系统提示,键入以下信息(LINUX系统请见SWIGWIKI共享库页面其他操作系统帮助):
unix%swig-tclexample.i
unix%gcc-fpic-cexample.cexample_wrap.c\
-I/usr/local/include
unix%gcc-sharedexample.oexample_wrap.o-oexample.so
unix%tclsh
%load./example.soexample
%puts$My_variable
3.0
%fact5
120
%my_mod73
1
%get_time
SunFeb1123:
01:
071996
%
该SWIG命令创建了一个文件example_wrap.c,编辑并且和其余的程序联接。
在这情况下,我们必须创建一个动态可装载的链接。
能够装载进入TCL使用LOAD命令。
建立Python模块
转换编码C成Python模块很简单,只需要按如下做即可(请见其他操作系统的SWIG共享库帮助手册):
unix%swig-pythonexample.i
unix%gcc-cexample.cexample_wrap.c\
-I/usr/local/include/python2.1
unix%ld-sharedexample.oexample_wrap.o-o_example.so
我们现在可以使用如下Python模块:
>>>importexample
>>>example.fact(5)
120
>>>example.my_mod(7,3)
1
>>>example.get_time()
'SunFeb1123:
01:
071996'
>>>
建立Perl模块
你可以建立如下的Perl模块,如Solaris(请见其他操作系统的SWIG共享库帮助手册):
unix%swig-perl5example.i
unix%gcc-cexample.cexample_wrap.c\
`perl-MExtUtils:
:
Embed-eccopts`
unix%ld-Gexample.oexample_wrap.o-oexample.so
unix%perl
useexample;
print$example:
:
My_variable,"\n";
printexample:
:
fact(5),"\n";
printexample:
:
get_time(),"\n";
3.0
120
SunFeb1123:
01:
071996
unix%
建立Java模块
SWIG也会产生JNI代码以便Jave代码进入C/CC++。
以下是建立一个Jave模块的事例(cygwin,见其他操作系统的swig维基共享库页帮助):
$swig-javaexample.i
$gcc-cexample.cexample_wrap.c-I/c/jdk1.3.1/include-I/c/jdk1.3.1/include/win32
$gcc-sharedexample.oexample_wrap.o-mno-cygwin-Wl,--add-stdcall-alias-oexample.dll
$catmain.java
publicclassmain{
publicstaticvoidmain(Stringargv[]){
System.loadLibrary("example");
System.out.println(example.getMy_variable());
System.out.println(example.fact(5));
System.out.println(example.get_time());
}
}
$javacmain.java
$javamain
3.0
120
MonMar418:
20:
312002
$
建立C#模块
SWIG也会产生代码以便C#使用Pinvoke进入C/CC++。
以下是如何建立C#模块事例。
cygwin,见其他操作系统的swig维基共享库页帮助。
使用了开源DotGNUPortable.NET能够在大多数Unix系统上运行,和其他C#compilers一样方便使用:
$swig-csharpexample.i
$gcc-c-fpicexample.cexample_wrap.c
$gcc-sharedexample.oexample_wrap.o-olibexample.so
$cscc-orunme*.cs
$catrunme.cs
usingSystem;
publicclassrunme{
staticvoidMain(){
Console.WriteLine(example.My_variable);
Console.WriteLine(example.fact(5));
Console.WriteLine(example.get_time());
}
}
$ilrunrunme
3
120
TueMay1310:
45:
452003
$
SWIG懒人方法
如上所见,并非总是需要写一个专门的接口文件。
如果你有一个头文件,你可以直接在其中包含SWIG接口,如例:
%moduleexample
%{
/*Includestheheaderinthewrappercode*/
#include"header.h"
%}
/*Parsetheheaderfiletogeneratewrappers*/
%include"header.h"
另外,有些人可能只包括SWIG条件指令在头文件中。
例如:
#ifdefSWIG
%moduleexample
%{
#include"header.h"
%}
#endif
externintfact(intn);
...
MicrosoftWindows下运行SWIG
SWIG能够运行在所有已知的32位版本的WINDOWS下95/98/NT/2000/XP。
SWIG通常使用命令提示符因此能够使用NMAKE。
模块通常由DLL编译,可动态加载入Tcl,Python,或者任何你使用的语言。
只需要小小加工,SWIG就能够在MS下发挥巨大作用。
That'sit(well,moreorless)
在开始前,你需要知道的事情。
这里是简短的清单:
∙明确模块名称
∙使用ANSIC/C++
∙理解如何编写一个共享模块/动态连接库(可能需要阅读一些所使用的编译器的帮助文件)
∙放松
Surelythere'smoretoit...
上述例子都很简单,但是大体思路已经延伸到复杂的C/C++。
事实上,重要的是明白SWIG一个完整的C++支持下几乎能包含所有语言的特征。
这些包括预处理,指针,类,甚至C++模块。
SWIG能够在特定语言打包结构和类变成PROXY。
为了说明这一点,假设你想要封装以下C++数据结构:
//pair.h.ApairliketheSTL
namespacestd{
templatestructpair{
T1first;
T2second;
pair():
first(T1()),second(T2()){};
pair(constT1&f,constT2&s):
first(f),second(s){}
};
}
为了封装SWIG你需要如下接口:
//pair.i-SWIGinterface
%modulepair
%{
#include"pair.h"
%}
//Ignorethedefaultconstructor
%ignorestd:
:
pair:
:
pair();
//Parsetheoriginalheaderfile
%include"pair.h"
//Instantiatesometemplates
%template(pairii)std:
:
pair;
%template(pairdi)std:
:
pair;
接下去是编译(Python):
$swig-python-c++pair.i
$c++-cpair_wrap.c-I/usr/local/include/python2.1
$c++-sharedpair_wrap.o-o_pair.so
$python
Python2.1(#3,Aug202001,15:
41:
42)
[GCC2.95.219991024(release)]onsunos5
Type"copyright","credits"or"license"formoreinformation.
>>>importpair
>>>a=pair.pairii(3,4)
>>>a.first
3
>>>a.second
4
>>>a.second=16
>>>a.second
16
>>>b=pair.pairdi(3.5,8)
>>>b.first
3.5
>>>b.second
8