我正在尝试创建一个可从Java调用的C动态库。我已经在Cygwin下编译了一个DLL,并使用SWIG生成JNI,以下是使用的makefile:
这是SWIG接口文件utilities.i的内容:
我已验证从DLL正确导出方法,并将utilities.dll放置在以下两个位置:
库成功加载,但调用库失败并显示以下内容:
CC= gcc -mno-cygwin
SWIG= /cygdrive/c/Documents\ and\ Settings/student/Desktop/swigwin-2.0.4/swig.exe -java
INCLUDE1= -I/cygdrive/c/Program\ Files/Java/jdk1.6.0_25/include
INCLUDE2= -I/cygdrive/c/Program\ Files/Java/jdk1.6.0_25/include/win32
utilities:
${SWIG} utilities.i
${CC} -c utilities.c utilities_wrap.c ${INCLUDE1} ${INCLUDE2}
${CC} -shared utilities.o utilities_wrap.o -Wl,--add-stdcall-alias -o utilities.dll
这是SWIG接口文件utilities.i的内容:
/* utilities.i */
%module utilities
%{
#include "driver.h"
%}
extern int get_3711a_fd(char * device);
/* Other prototypes omitted for brevity */
我已验证从DLL正确导出方法,并将utilities.dll放置在以下两个位置:
- C:\Program Files\Java\jdk1.6.0_25\bin
- C:\Program Files\Java\jdk1.6.0_25\jre\bin
System.load(libraryPath)
从路径1.中加载,其中包含库文件名,并在调用时捕获任何SecurityException
或UnsatisfiedLinkError
。库成功加载,但调用库失败并显示以下内容:
Exception in thread "main" java.lang.UnsatisfiedLinkError:
invokeoncomport.utilitiesJNI.get_3711a_fd(Ljava/lang/String;)I
at invokeoncomport.utilitiesJNI.get_3711a_fd(Native Method)
at invokeoncomport.utilities.get_3711a_fd(utilities.java:14)
at invokeoncomport.Main.main(Main.java:41)
SWIGEXPORT jint JNICALL Java_utilitiesJNI_get_13711a_1fd(...)
。 - Rob