Swig工具在win10上使用

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

        SWIG 是一种软件开发工具它将 C 和 C++ 编写的程序与各种高级编程语言连接起来。这里我们用它来将 C/C++ 转换成 Java。

一、Swig安装

1、下载

官网SWIG官网下载

源码链接 GitHubhttps://github.com/swig/swig.git

        这两个地址可能会出现无法打开或者无法下载的情况直接在网上搜索“Swig下载”在第三方平台下载一个即可。

2、安装

        将下载的 swigwin-4.1.1.zip 压缩包解压最好是解压到盘符根目录下例如D:\swigwin-4.1.1。可以看到有 swig.exe 在文件夹内。

3、设置系统变量

        将 swig.exe 所在路径添加至系统变量 Path 中。

4、验证

win+r 打开cmd输入swig -version 查看版本信息

swig -version

看到版本信息就说明安装成功了。

        请忽略打印版本信息与上面版本信息不一致问题以为官网下载异常在网上随便找了一个版本下载。

二、Swig使用

        首先个人需求是 Android 项目调用 so 库中方法。这里我们可以拿到 .h 文件再通过 .h 文件编写 .i 文件然后使用 Swig 工具通过 .i 文件生成 .java 文件。

1、so 库文件

        例如so库中使用 c 写的 sotest.c 和 sotest.h。

/* File : sotest.c */
 #include <time.h>
 double My_variable = 3.0;
 
 int fact(int n) {
     if (n <= 1) return 1;
     else return n*fact(n-1);
 }
 
 int my_mod(int x, int y) {
     return (x%y);
 }
 	
 char *get_time()
 {
     time_t ltime;
     time(&ltime);
     return ctime(&ltime);
 }
/* File : sotest.h */
double My_variable = 3.0;

int fact(int n);
int my_mod(int x, int y);
char *get_time();

         正常情况下我们是看不到上面的 so 库中的代码但是会给我我们提供上面的 .h 文件这样我们就知道要调用的方法名称。

2、准备 .i 文件

        下面是我们要做的根据上面的 sotest.h 文件自己写一个 sotest.i 文件。

/* example.i */
 %module example
 %{
 /*将头文件或函数声明放在这里如下所示 */
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();
 %}
 
 extern double My_variable;
 extern int fact(int n);
 extern int my_mod(int x, int y);
 extern char *get_time();

3、生成 java 文件

        执行以下命令Swig 会根据 sotest.i 接口定义脚本生成出若干个文件

swig -java sotest.i

        注意上面的 sotest.i 需要携带文件路径例如我的文件放在 D 盘 SoTest 文件夹下则命令为swig -java D:\SoTest\sotest.i。执行完成文件夹下新增 example.java、exampleJNI.java 和 sotest_wrap.c 三个文件。

1example.java

/* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 3.0.6
 *
 * Do not make changes to this file unless you know what you are doing--modify
 * the SWIG interface file instead.
 * ----------------------------------------------------------------------------- */


public class example {
  public static void setMy_variable(double value) {
    exampleJNI.My_variable_set(value);
  }

  public static double getMy_variable() {
    return exampleJNI.My_variable_get();
  }

  public static int fact(int n) {
    return exampleJNI.fact(n);
  }

  public static int my_mod(int x, int y) {
    return exampleJNI.my_mod(x, y);
  }

  public static String get_time() {
    return exampleJNI.get_time();
  }

}

2exampleJNI.java

/* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 3.0.6
 *
 * Do not make changes to this file unless you know what you are doing--modify
 * the SWIG interface file instead.
 * ----------------------------------------------------------------------------- */


public class exampleJNI {
  public final static native void My_variable_set(double jarg1);
  public final static native double My_variable_get();
  public final static native int fact(int jarg1);
  public final static native int my_mod(int jarg1, int jarg2);
  public final static native String get_time();
}

        从 Java 的角度讲接口的封装已经完成但 native 方法调用相应的动态库还需要打包。

4、打包

        通过以下命令来打包。

gcc -c -fPIC sotest.c sotest_wrap.c -I F:\openjdk-19.0.2\jdk-19.0.2\include -I F:\openjdk-19.0.2\jdk-19.0.2\include\win32

        sotest.c 和 sotest_wrap.c 分别为原始的 c 文件和生成的 c 文件后面的路径为自己安装 jdk 的路径记得修改。jdk 请自行安装环境变量配置参考 《JDK环境变量配置》。执行命令又发现 gcc "不是内部或外部命令也不是可运行的程序"所以还需要配置 C 语言环境安装 MinGw 并配置环境变量参考《MinGw 配置》。由于公司网络的原因导致最后安装失败mingw-get: *** ERROR *** setup: unable to continue后面的两步没有得到验证。

        全部完成后在执行上面命令这时会生成 sotest.o 和 sotest_wrap.o 两个文件。接着执行下面命令

gcc -shared -o sotest.so sotest.o sotest_wrap.o

        这时就生成了我们想要的 sotest.so 文件。

5、验证

        通过前面的方法调用生成的 so 库。

System.load("加载绝对路径文件");
System.loadLibrary("载系统库的默认路径")

参考SWIG初体验

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6