无法在 Windows XP 上使用 DLL 调用本机 SWIG 方法 [英] Unable to Call Native SWIG Method using DLL on Windows XP

查看:34
本文介绍了无法在 Windows XP 上使用 DLL 调用本机 SWIG 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的 SWIG 实现在 Windows XP(32 位)机器上运行.我的 test.java 能够加载 SWIG 共享库 dll (System.loadLibrary("TestJni")),但是当我尝试调用我使用 SWIG 公开的任何 C 函数时,它会引发以下异常.我正在寻找一些调试技巧.我使用 Makefile 使用 32 位 Linux 创建了共享库 dll.也许在创建 SWIG 共享库 dll 时有些东西不正常.我可以让它在 Linux 上工作(对 Makefile 进行一些 Linux 特定的更改).有什么想法吗?

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 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)  

创建 SWIG 共享库 dll 的 Makefile:

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)

编译输出:

[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

推荐答案

我在 Makefile 中省略了包含 CFLAGS(编译)中 jdk 的 jni.h 和 jni_md.h 头文件的 java 目录.一旦我将它们添加到 Makefile 中,我就能够通过 JNI 方法调用从 java 通信到 c.

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.

这篇关于无法在 Windows XP 上使用 DLL 调用本机 SWIG 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆