使用SWIG%本机函数编​​译错误

时间:2012-04-11 03:27:06

标签: java-native-interface swig

我正在尝试使用SWIG%本机函数编​​译本机JNI调用,并且我得到头文件的以下异常。我在下面的makefile中包含了jdk-1.6.0_30 / include和jdk-1.6.0_30 / include / linux,有什么想法吗?我正在编译32位linux。

Sample.h:

JNIEXPORT jobject JNICALL Java_test_jni_GetData (JNIEnv *, jclass);

SWIG.i:

%module Sample
%{
   #include "Sample.h"
%}
%include "Sample.h"
%typemap(jstype) DeviceId getID "com.test.jni.DeviceId"
%typemap(jtype) DeviceId getID "com.test.jni.DeviceId"
%typemap(javaout) DeviceId getID { return $jnicall; }
%native(getID) DeviceId getID();

例外:

[exec]Sample.h: Error: Syntax error in input(1).
[exec] make-3.79.1-p7: *** [sample_wrap.c] Error 1

Makefile(不是完整文件):

     PACKAGE_DIR = src/java/com/test/jni
     PACKAGE = com.test.jni
     INCLUDES = -I/user/java/jdk-1.6.0_30/include/linux \
                -I/user/java/jdk-1.6.0_30/include \
                -I/user/src/include #Sample.h resides here
     CFLAGS = -Wall -fpic $(INCLUDES) -O0 -g3
     SFLAGS = -java $(INCLUDES) -package $(PACKAGE) -outdir $(PACKAGE_DIR)

1 个答案:

答案 0 :(得分:1)

我认为你的订单错误了。您需要第一次写:

%{
JNIEXPORT jobject JNICALL Java_test_jni_GetData(JNIEnv *, jclass);
%}

这样当你写:

%native (GetData) jobject GetData();

该函数的声明已存在于SWIG将生成的包装器代码中。


如果它有本机功能,你可以直接%include Sample.h。 SWIG预处理器不知道JNIEXPORT或JNICALL是什么 - 它们看起来像语法错误,除非它们被作为宏给出。

我建议将原生函数放在一个单独的头文件中,然后 #include,而不是%include该文件。

虽然没有打开几个选项,但是从SWIG隐藏了本机功能,例如:

#ifndef SWIG
JNIEXPORT jobject JNICALL Java_test_jni_GetData (JNIEnv *, jclass);
#endif
头文件中的

可以工作,或者您可以修改接口文件以使SWIG忽略JNIEXPORT和JNICALL:

%module Sample
%{
   #include "Sample.h"
%}
#define JNIEXPORT
#define JNICALL
%include "Sample.h"
%typemap(jstype) DeviceId getID "com.test.jni.DeviceId"
%typemap(jtype) DeviceId getID "com.test.jni.DeviceId"
%typemap(javaout) DeviceId getID { return $jnicall; }
%native(getID) DeviceId getID();

虽然在这种情况下,SWIG会尝试将其包装起来并采用可能不是您想要的%native指令!