用途说明

  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

  1. /* File : example.c */
  2. #include <time.h>
  3. double My_variable = 3.0;
  4. int fact(int n) {
  5. if (n <= 1) return 1;
  6. else return n*fact(n-1);
  7. }
  8. int my_mod(int x, int y) {
  9. return (x%y);
  10. }
  11. char *get_time()
  12. {
  13. time_t ltime;
  14. time(&ltime);
  15. return ctime(&ltime);
  16. }

文件 example.i

  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. extern double My_variable;
  11. extern int fact(int n);
  12. extern int my_mod(int x, int y);
  13. extern char *get_time();

文件 main.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接口

我使用过的Linux命令之swig - 把C/C++的代码嵌入Java等语言的开发工具的更多相关文章

  1. 在 Linux 命令行中使用和执行 PHP 代码

    PHP是一个开源服务器端脚本语言,最初这三个字母代表的是“Personal Home Page”,而现在则代表的是“PHP:Hypertext Preprocessor”,它是个递归首字母缩写.它是一 ...

  2. 【改造Linux命令之rm - 删除文件或目录-】

    用途说明 rm命令是常用的命令,用来删除文件或目录(remove files or directories).它也是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比 ...

  3. 十年linux命令总结

    十年linux命令总结 本文链接: http://codingstandards.iteye.com/blog/786653 关于命令类型划分 本表中列出了我穷尽了我所有的记忆整理出来的Linux命令 ...

  4. 打造Linux三流娱乐环境,二流办公环境,一流Java开发环境

    写这篇文章的目的首先是为让自己以后再装linux环境时候,不用再通宵google+百度,做个备忘录,其次,给新入Linux环境的同学分享一点个人经验,再高尚点的动机也算是想做为开源技术的传播布道者.我 ...

  5. APICloud提供适用于命令行的开发工具,开发更具极客精神!

    APICloud近期大动作不断,上周刚刚支持Atom编辑器,并推出核心开发工具库.本周又为开发者提供了一款超轻便的新开发工具--CLI工具! 操作系统: Mac/Windows/Linux nodej ...

  6. Linux命令之dot - 绘制DOT语言脚本描述的图形

    本文链接:http://codingstandards.iteye.com/blog/840055 用途说明 Graphviz (Graph Visualization Software的缩写)是一个 ...

  7. Linux实战教学笔记04:Linux命令基础

    第四节:Linux命令基础 标签(空格分隔):Linux实战教学笔记 第1章 认识操作环境 root:当前登陆的用户名 @分隔符 chensiqi:主机名 -:当前路径位置 用户的提示符 1.1 Li ...

  8. 像黑客一样使用 Linux 命令行

    前言 之前在博客园看到一篇介绍 IntelliJ IDEA 配置的文章,它里面用的是 gif 动态图片进行展示,我觉得很不错.所以在我今天以及以后的博文中,我也会尽量使用 gif 动图进行展示.制作 ...

  9. 11 个很少人知道但很有用的 Linux 命令

    Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.Linux命令和它们的转换对于Linux用户.Shell脚本程序员和管理员来说是最有 ...

随机推荐

  1. LeetCode 5:Given an input string, reverse the string word by word.

    problem: Given an input string, reverse the string word by word. For example: Given s = "the sk ...

  2. IC卡的传输协议(3)【转】

    转自:http://bbs.ednchina.com/BLOG_ARTICLE_172027.HTM 3.终端传输层         本节描述了在终端和 IC 卡之间传输的命令和响应 APDU 的机制 ...

  3. Selenium IDE安装和检查获取的控件路径技巧

    来源:http://www.jianshu.com/p/0ea2dc83549f 从学习Selenium 开始,都是自己写脚本,后来得知有个插件Selenium IDE可以录制脚本,也懒得用了,觉得自 ...

  4. Python Requests 小技巧总结

    关于 Python Requests ,在使用中,总结了一些小技巧把,分享下. 1:保持请求之间的Cookies,我们可以这样做. import requests self.session = req ...

  5. ubuntu16.04下fcitx无法在QT Creator输入中文解决办法

    我的博客新地址:www.liuquanhao.com ------------------------------------------------------ Qt creator无法用fcitx ...

  6. JSTL c:url

    c:url 标签 jstl 实例代码和用法.     <c:url>标记格式化成一个字符串格式的URL,并将其存储到变量中.这个标签会在必要时自动执行URL重写. var属性指定的变量将包 ...

  7. Spring+Junit,测试事务时,一直回滚

    研究了好长时间,都不知道原因,也不能解决. 控制台又没有报异常,但是就是一直回滚.注释掉spring声明式配置的代码,就能正确的更新数据. 从网上看了,别人的文章 http://blog.csdn.n ...

  8. js的变量的有效域

    function test(o) { var i=0; if(typeof o=="object") { var j=1; for(var k=0;k<10;k++) { c ...

  9. poj 1696(极角排序)

    Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3924   Accepted: 2457 Descrip ...

  10. (十二)MySQL逻辑备份mysqldump

    (1)简介 语法 mysqldump -h服务器 -u用户名 -p密码 [-P端口号] [参数] 数据库名 >备份文件.sql 关于数据库: -A,--all-databases 所有库,会生成 ...