用途说明

  SWIG是Simplified Wrapper and Interface Generator的缩写,其官方站点是http://www.swig.org/。SWIG是个帮助使用C或者C++编写的软件能与其它各种高级编程语言进行嵌入联接的开发工具。SWIG能应用于各种不同类型的语言包括常用脚本编译语言例如Perl, PHP, Python, Tcl, Ruby and PHP。支持语言列表中也包括非脚本编译语言,例如C#, Common Lisp (CLISP, Allegro CL, CFFI, UFFI), Java, Modula-3, OCAML以及R,甚至是编译器或者汇编的计划应用(Guile, MzScheme, Chicken)。SWIG普遍应用于创建高级语言解析或汇编程序环境,用户接口,作为一种用来测试C/C++或进行原型设计的工具。SWIG还能够导出 XML或Lisp s-expressions格式的解析树。SWIG可以被*使用,发布,修改用于商业或非商业中。

 

常用参数

swig -version 看SWIG开发工具的版本。

swig -java 生成java代码。见示例三所展示的。

swig -help 看帮助。用 man swig 和 info swig 没有看到相应的手册。

最佳的学习swig的方法就是到官方网站去看相关文档。

 

使用示例

示例一 查看swig的基本信息

注:有些Linux安装之后不一定有swig命令,可以先确认一下是否安装了swig,以及是哪种安装方式(rpm方式,make方式)。RHEL5.5下默认安装的swig版本是1.3.29,当前最新版本是Version: 2.0.1 (4 October 2010)。可以先把Linux默认安装的版本删除,然后再下载源代码编译安装。

 

[root@new55 swig]# rpm -q swig
swig-1.3.29-2.el5

[root@new55 swig]# swig -version

SWIG Version 1.3.29

Compiled with g++ [i686-redhat-linux-gnu]
Please see http://www.swig.org for reporting bugs and further information
[root@new55 swig]# whereis swig
swig: /usr/bin/swig /usr/share/swig
[root@new55 swig]# which swig
/usr/bin/swig
[root@new55 swig]# rpm -qif $(which swig) 
Name        : swig                         Relocations: (not relocatable)
Version     : 1.3.29                            Vendor: Red Hat, Inc.
Release     : 2.el5                         Build Date: 2006年11月07日 星期二 16时15分56秒
Install Date: 2010年08月13日 星期五 00时19分22秒      Build Host: hs20-bc2-4.build.redhat.com
Group       : Development/Tools             Source RPM: swig-1.3.29-2.el5.src.rpm
Size        : 8572427                          License: BSD
Signature   : DSA/SHA1, 2007年01月18日 星期四 01时53分06秒, Key ID 5326810137017186
Packager    : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>
URL         : http://swig.sourceforge.net/
Summary     : 把 C/C++/Objective C 连接到某些高级别的编程语言。
Description :
Simplified Wrapper and Interface Generator (SWIG) is a software
development tool for connecting C, C++ and Objective C programs with a
variety of high-level programming languages.  SWIG is primarily used
with Perl, Python and Tcl/TK, but it has also been extended to Java,
Eiffel and Guile.  SWIG is normally used to create high-level
interpreted programming environments, systems integration, and as a
tool for building user interfaces.
[root@new55 swig]#

 

示例二 下载、编译和安装swig开发工具

下载最新的swig包

[root@new55 setup]# wget "http://sourceforge.net/projects/swig/files/swig/swig-2.0.1/swig-2.0.1.tar.gz/download"
--2010-12-01 11:17:44--  http://sourceforge.net/projects/swig/files/swig/swig-2.0.1/swig-2.0.1.tar.gz/download
正在解析主机 sourceforge.net... 216.34.181.60
Connecting to sourceforge.net|216.34.181.60|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:http://downloads.sourceforge.net/project/swig/swig/swig-2.0.1/swig-2.0.1.tar.gz?r=&ts=1291173453&use_mirror=ncu [跟随至新的 URL]
--2010-12-01 11:17:45--  http://downloads.sourceforge.net/project/swig/swig/swig-2.0.1/swig-2.0.1.tar.gz?r=&ts=1291173453&use_mirror=ncu
正在解析主机 downloads.sourceforge.net... 216.34.181.59
Connecting to downloads.sourceforge.net|216.34.181.59|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:http://ncu.dl.sourceforge.net/project/swig/swig/swig-2.0.1/swig-2.0.1.tar.gz [跟随至新的 URL]
--2010-12-01 11:17:46--  http://ncu.dl.sourceforge.net/project/swig/swig/swig-2.0.1/swig-2.0.1.tar.gz
正在解析主机 ncu.dl.sourceforge.net... 140.115.17.45
Connecting to ncu.dl.sourceforge.net|140.115.17.45|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:4922992 (4.7M) [application/x-gzip]
Saving to: `swig-2.0.1.tar.gz'

100%[==========================================================================================>] 4,922,992   10.4K/s   in 9m 10s 

2010-12-01 11:26:57 (8.73 KB/s) - `swig-2.0.1.tar.gz' saved [4922992/4922992]

[root@new55 setup]# ls -l swig-2.0.1.tar.gz
-rw-r--r-- 1 root root 4922992 10-05 04:48 swig-2.0.1.tar.gz
[root@new55 setup]#

将系统原有的swig软件卸载掉。

[root@new55 setup]# rpm -q swig
swig-1.3.29-2.el5
[root@new55 setup]# rpm -e swig-1.3.29-2.el5
[root@new55 setup]#

解压,编译,安装。
[root@new55 setup]# tar zxf swig-2.0.1.tar.gz
[root@new55 setup]# cd swig-2.0.1/
[root@new55 swig-2.0.1]# ls
aclocal.m4  CCache           configure     Doc       Lib          LICENSE-UNIVERSITIES  README        swig.spec.in  vms
ANNOUNCE    CHANGES          configure.in  Examples  LICENSE      Makefile.in           RELEASENOTES  TODO          Win
autogen.sh  CHANGES.current  COPYRIGHT     INSTALL   LICENSE-GPL  preinst-swig.in       Source        Tools
[root@new55 swig-2.0.1]# ./configure --prefix=/usr
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
省略输出
checking whether to enable PCRE support... yes
checking whether to use local PCRE... no
checking for a sed that does not truncate output... /bin/sed
checking for pcre-config... no
configure: error:
        Can't find pcre-config script from PCRE (Perl Compatible Regular Expressions)
        library package. This dependency is needed for configure to complete,
        Either:
        - Install the PCRE developer package on your system (preferred approach).
        - Download the PCRE source tarball, build and install on your system
          as you would for any package built from source distribution.
        - Use the Tools/pcre-build.sh script to build PCRE just for SWIG to statically
          link against. Run 'Tools/pcre-build.sh --help' for instructions.
          (quite easy and does not require privileges to install PCRE on your system)
        - Use configure --without-pcre to disable regular expressions support in SWIG
          (not recommended).
See `config.log' for more details.
[root@new55 swig-2.0.1]#

configure报错了,缺少pcre开发包。(PCRE = Perl Compatible Regular Expressions)

到Linux安装盘中找pcre开发包,我的机器上安装盘保存到镜像文件rhel55.iso了。

[root@new55 os]# mkdir /mnt/rhel55
[root@new55 os]# mount rhel55.iso /mnt/rhel55
mount: rhel55.iso is not a block device (maybe try `-o loop'?)
[root@new55 os]# mount -o loop rhel55.iso /mnt/rhel55   
[root@new55 os]# cd /mnt/rhel55/
[root@new55 rhel55]# find . -name "pcre*rpm"
./Server/pcre-6.6-2.el5_1.7.i386.rpm
./Server/pcre-devel-6.6-2.el5_1.7.i386.rpm
[root@new55 rhel55]# rpm -ivh ./Server/pcre-devel-6.6-2.el5_1.7.i386.rpm
warning: ./Server/pcre-devel-6.6-2.el5_1.7.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing...                ########################################### [100%]
   1:pcre-devel             ########################################### [100%]
[root@new55 rhel55]#

重新configure。

[root@new55 swig-2.0.1]# ./configure --prefix=/usr
checking build system type... i686-pc-linux-gnu
省略输出
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
[root@new55 swig-2.0.1]#

编译,安装。

[root@new55 swig-2.0.1]# make
make[1]: Entering directory `/root/setup/swig-2.0.1/Source'
make  all-am
make[2]: Entering directory `/root/setup/swig-2.0.1/Source'

make[1]: Leaving directory `/root/setup/swig-2.0.1/CCache'
[root@new55 swig-2.0.1]# make install
Installing SWIG executable
Installing /usr/bin/swig
Installing the SWIG library

省略输出

Installing ccache-swig
Installing /usr/bin/ccache-swig
Installing /usr/share/man/man1/ccache-swig.1
Installation complete
[root@new55 swig-2.0.1]#

确认是否安装成功,查看一下版本号。

[root@new55 swig-2.0.1]# swig -version

SWIG Version 2.0.1

Compiled with g++ [i686-pc-linux-gnu]

Configured options: +pcre

Please see http://www.swig.org for reporting bugs and further information
[root@new55 swig-2.0.1]#

 

示例三 把官方站点的提供的例子在Linux下用Java尝试一下

文件 example.c

C代码  我使用过的Linux命令之swig - 把C/C++的代码嵌入Java等语言的开发工具
  1. /* File : example.c */  
  2.   
  3. #include <time.h>  
  4. double My_variable = 3.0;  
  5.   
  6. int fact(int n) {  
  7.     if (n <= 1) return 1;  
  8.     else return n*fact(n-1);  
  9. }  
  10.   
  11. int my_mod(int x, int y) {  
  12.     return (x%y);  
  13. }  
  14.   
  15. char *get_time()  
  16. {  
  17.     time_t ltime;  
  18.     time(&ltime);  
  19.     return ctime(&ltime);  
  20. }  

 

文件 example.i

C代码  我使用过的Linux命令之swig - 把C/C++的代码嵌入Java等语言的开发工具
  1. /* example.i */  
  2. %module example  
  3. %{  
  4. /* Put header files here or function declarations like below */  
  5. extern double My_variable;  
  6. extern int fact(int n);  
  7. extern int my_mod(int x, int y);  
  8. extern char *get_time();  
  9. %}  
  10.   
  11. extern double My_variable;  
  12. extern int fact(int n);  
  13. extern int my_mod(int x, int y);  
  14. extern char *get_time();  

 

文件 main.java

Java代码  我使用过的Linux命令之swig - 把C/C++的代码嵌入Java等语言的开发工具
  1. public class main {  
  2.   public static void main(String argv[]) {  
  3.     System.loadLibrary("example");  
  4.     System.out.println(example.getMy_variable());  
  5.     System.out.println(example.fact(5));  
  6.     System.out.println(example.get_time());  
  7.   }  
  8. }  

 

先在Linux下将上面三个文件保存好。

[root@new55 swig]# swig -java example.i

[root@new55 swig]# ls
example.c  example.i  example.java  exampleJNI.java  example_wrap.c  main.java

查找一下jni所需头文件的位置。(JNI = Java Native Interface)

[root@new55 swig]# locate jni.h
/usr/include/xulrunner-sdk-1.9/java/jni.h
/usr/include/xulrunner-sdk-1.9/stable/jni.h
/usr/include/xulrunner-sdk-1.9/system_wrappers/jni.h
/usr/include/xulrunner-sdk-1.9/system_wrappers/JavaVM/jni.h
/usr/java/jdk1.6.0_21/include / jni.h

[root@new55 swig]# locate jni_md.h
/usr/include/xulrunner-sdk-1.9/java/jni_md.h
/usr/include/xulrunner-sdk-1.9/stable/jni_md.h
/usr/java/jdk1.6.0_21/include/linux/ jni_md.h

把c代码编译成动态链接库(把前面找出来的jni头文件路径替换掉下面的参数相应部分)。

注意:动态链接库的文件名称格式为 lib*.so。

[root@new55 swig]# gcc -fpic -c example.c example_wrap.c -I/usr/java/jdk1.6.0_21/include/ -I/usr/java/jdk1.6.0_21/include/linux/

[root@new55 swig]# gcc -shared example.o  example_wrap.o -o libexample.so

编译调用C函数的java代码为class。

[root@new55 swig]# javac main.java

执行java程序。

[root@new55 swig]# java main

Exception in thread "main" java.lang.UnsatisfiedLinkError: no example in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
        at java.lang.Runtime.loadLibrary0(Runtime.java:823)
        at java.lang.System.loadLibrary(System.java:1028)
        at main.main(main.java:3)

上面的报错有两种解决方法。

第一种方法:java命令参数指定。

[root@new55 swig]# java -Djava.library.path=. main
3.0
120
Wed Dec  1 11:04:03 2010

[root@new55 swig]# java main
3.0
120
Tue Nov 30 19:41:37 2010

 

第二种方法:导出环境变量LD_LIBRARY_PATH。
[root@new55 swig]# export LD_LIBRARY_PATH=.
[root@new55 swig]# java main               
3.0
120
Wed Dec  1 11:04:44 2010

[root@new55 swig]#

到此,在java中使用C代码的尝试就成功了。

 

问题思考

相关资料

【1】官方站点                    http://www.swig.org/

【2】SWIG是什么                       http://www.swig.org/translations/chinese/exec.html
【3】Swig Tutorial                  http://swig.minidx.com/tutorial.html
【4】SWIG and JAVA                   http://www.swig.org/Doc1.3/Java.html

【5】SwigFaqLinuxSharedLibraries    http://www.dabeaz.com/cgi-bin/wiki.pl?SwigFaqLinuxSharedLibraries

【6】百度百科  SWIG        
【7】花钱的年华    SWIG,C/C++类库与Java,Python等泛高阶语言的万能粘合剂    

【8】masrpsj    利用SWIG转换C++接口到Java接口

分类:

技术点:

相关文章: