Compile Error Using SWIG %native Function

让人想犯罪 __ 提交于 2019-12-11 16:07:04

问题


I'm attempting to compile a native JNI call using the SWIG %native function and I'm getting the below exception for the header file. I'm including both the jdk-1.6.0_30/include and jdk-1.6.0_30/include/linux in the makefile below, Any ideas? I'm compiling on 32bit 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();

Exception:

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

Makefile (not complete file):

     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:


I think you've got this in the wrong order. You need to first write:

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

so that when you write:

%native (GetData) jobject GetData();

a declaration of the function already exists in the wrapper code that SWIG will generate.


You can't %include Sample.h directly like that if it's got native functions in it. The SWIG preprocessor doesn't know what JNIEXPORT or JNICALL are - they look like syntax errors unless they've been given as a macro.

I'd suggest putting the native functions in a separate header file which you then only #include, not %include that file.

Failing that you have several options open though, hiding the native function from SWIG, e.g.:

#ifndef SWIG
JNIEXPORT jobject JNICALL Java_test_jni_GetData (JNIEnv *, jclass);
#endif

in the header file would work, or you could modify the interface file to make SWIG ignore JNIEXPORT and 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();

Although in that case SWIG will try to wrap it as well as take the %native directive which probably isn't what you want!



来源:https://stackoverflow.com/questions/10099397/compile-error-using-swig-native-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!