GCC编译dll,Java调用dll

来源:互联网 发布:iframe跨域调用js方法 编辑:程序博客网 时间:2024/04/29 21:34

我们通过实例来学习使用gcc编译出dll文件的方法,看下面的例子说明这个过程,共有三个文件:hello.c、dll.h和dll.c. hello.c 文件内容如下#include <stdio.h> #include /"dll.h/" int main()

 { hello();return 0;}其中,hello()函数是动态连接库提供的函数。

dll.h 文件内容如下#ifdef BUILD_DLL /* DLL export */ #define EXPORT __declspec(dllexport)

#else /* EXE import */ #define EXPORT __declspec(dllimport)

#endif EXPORT void hello(void);

dll.c 文件内容如下#include /"dll.h/" EXPORT void hello(void)

{ printf (/"Hello//n/");}

   
    三个文件的内容都很简单,无须解释。

    编译连接程序

    1、编译hello.c gcc -c hello.c 。

    2、编译dll.c gcc -c -DBUILD_DLL dll.c注意要使用要使用-DBUILD_DLL来设置宏BUILD_DLL 。

    3、创建dll gcc -shared -o message.dll dll.o -Wl,——out-implib,libmessage.a这一步要详细说明一下-shared参数用来创建共享库,在windows中为dll -Wl 等待下一条信息进行连接——out-implib是给连接程序ld使用的,用于创建要连接dll需要的import library 。

    4、创建可执行文件gcc -o hello.exe hello.o -L./ -lmessage -L 指定连接库路径-lmessage (or -l message) 指定dll的import library好了,编译连接完成,运行程序C://>hello Hello!

 

 

 

 

 

 

 

下面是用java调用dll的例子

 

最近用到了java调用dll,网上找到了JNative这个开源的东西。
怎么用呢?
想自己编写个dll实验下。
于是找C/C++的编译器,人家推荐用MinGW
先写个头文件吧:
test.h:

 

C代码 复制代码
  1. #ifndef MINGW_DLL_H__   
  2. #define MINGW_DLL_H__   
  3. int add(int a,int b);   
  4. #endif  

 

 

接着写C文件:
test.c:

C代码 复制代码
  1. #include <stdio.h>   
  2. #include "test.h"   
  3. int add(int a,int b){   
  4.   printf("/n");   
  5.   printf("dll function add() called/n");   
  6.   return a+b;   
  7. }  

 

 

MinGW将C编译成dll,命令如下:

C代码 复制代码
  1. gcc -Wall -shared test.c -o test.dll  

 

 

把test.dll放到System32下面,
最后,该是写我们的java代码的时候了:
Test.java:

Java代码 复制代码
  1. import org.xvolks.jnative.JNative;   
  2. import org.xvolks.jnative.Type;   
  3. import org.xvolks.jnative.exceptions.NativeException;   
  4.   
  5. public class Test{   
  6.     public static  int testJNative(int a,int b) throws NativeException, IllegalAccessException{   
  7.         JNative n = null;   
  8.         try{   
  9.             n = new JNative("zzz.dll""add");   
  10.             n.setRetVal(Type.INT);        
  11.             n.setParameter(0 , a ) ;     
  12.             n.setParameter(1, b);   
  13.             n.invoke();         
  14.             System.out.println(  "返回:"  + n.getRetVal());         
  15.             return Integer.parseInt(n.getRetVal());   
  16.         }finally{   
  17.             if (n != null)   
  18.                 n.dispose();   
  19.         }   
  20.     }   
  21.     public static void main(String[] args) throws NativeException, IllegalAccessException{   
  22.        testJNative(14);   
  23.     }   
  24. }  

 

 

OK了!
运行下试试,结果如下:
返回:5。
dll function add() called

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>----------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

这个并不麻烦,可是,就MinGW,倒是不好搞,但是毕竟已经搞定,下面是我的方法:
先到  http://sourceforge.net/project/showfiles.php?group_id=2435
找到第一个:Automated MinGW Installer,下载下来,然后安装,安装时,选择全部安装就可以了,
他会自动的下载所有需要的文件。
刚开始的时候按照网上介绍的方法,一个一个文件下载的,没注意这个东西,结果,总是报少文件,
后来才发现的他,把原来的全删掉,OK了!

JNative的下载地址是:http://sourceforge.net/project/showfiles.php?group_id=156421,这个东西倒是不难,
把它带的dll放到system32下面,就OK了!

原创粉丝点击