问题
I'm attempting to get my SWIG implementation to work on Windows XP (32bit) machine. My test.java is able to load the SWIG shared library dll (System.loadLibrary("TestJni")) but it throws the below exception when I attempt to call any C function that I've exposed using SWIG. I'm in search of some debugging tips. I created the shared library dll using 32 bit Linux using a Makefile. Maybe something is out of wack in the created of the SWIG shared library dll. I'm able to get this to work on Linux (with a few Linux specific changes to the Makefile). Any ideas?
Exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.test.jni.SampleJNI.setLogLevel(I)V
at com.test.jni.SampleJNI.setLogLevel(Native Method)
at com.test.jni.Sample.setLogLevel(Unknown Source)
at com.test.jni.Example.setLogLevel(Unknown Source)
at com.test.jni.Example.main(Unknown Source)
Makefile that creates the SWIG shared library dll:
CMODE=
SWIG = swig
PREFIX=/test/mingw/mingw32/bin/i386-mingw32-
CC = $(PREFIX)gcc
LD = $(CC)
OBJ_DIR = obj
AUTOGEN_DIR = ../src/java
PACKAGE_DIR = $(AUTOGEN_DIR)/com/test/jni
PACKAGE = com.test.jni
INCLUDES = -I$(HEADER_FILES_DIR) # env var that points to a dir with all the .h files
LIB_INCLUDES = -L$(C_API_DIR)/lib # env var that points to a dir with the C libraries (dlls)
LIBS = -lMainApi \ # MainApi.dll
-lm
DIRS = $(PACKAGE_DIR) $(DIST_DIR) $(OBJ_DIR) $(AUTOGEN_DIR) # DIST_DIR is passed in
CFLAGS = $(CMODE) -Wall -fpic $(INCLUDES) -O0 -g3
SFLAGS = -java $(INCLUDES) -package $(PACKAGE) -outdir $(PACKAGE_DIR)
LDFLAGS = -shared $(LIB_INCLUDES) $(LIBS) -leay32 -lws2_32 -lrpcrt4
OBJECTS = $(OBJ_DIR)/test_wrap.o
TARGET = $(LIB_DIR)/SampleJni.dll
all: $(DIRS) $(TARGET)
%_wrap.c: %.i
$(SWIG) $(SFLAGS) $<
$(OBJ_DIR)/%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
$(TARGET): $(OBJECTS)
$(LD) $(OBJECTS) $(LDFLAGS) -o $@
$(DIRS):
mkdir -p $@
clean:
rm -rf $(TARGET) $(PACKAGE_DIR)/* $(TARGET) $(AUTOGEN_DIR) $(OBJ_DIR)
Compilation Output:
[exec] /test/mingw/mingw32/bin/i386-mingw32-gcc obj/ewapi_wrap.o -shared -L/test/backup/jni/lib -lMainApi -lm -leay32 -lws2_32 -lrpcrt4 -o /test/backup/jni/lib/SampleJni.dll
[exec] i386-mingw32-gcc: --kill-at: linker input file unused because linking not done
[exec] rm ewapi_wrap.c
回答1:
It is likely that you have dependency to Mingw32 dll which you don't have it in PATH. Consult dependency walker and make sure you have those DLLs in your PATH.
You need to change compiler flag. I think you miss the following options:
-D_JNI_IMPLEMENTATION_ -Wl,--kill-at
回答2:
I had omitted the java directories that have the jdk's jni.h and jni_md.h header files from the CFLAGS (compile) include in the Makefile. Once I added those to the Makefile I was able to communicate from java to c via JNI method calls.
来源:https://stackoverflow.com/questions/8721310/unable-to-call-native-swig-method-using-dll-on-windows-xp