c12
c12

Reputation: 9827

Compile Error Using SWIG %native Function

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)

Upvotes: 0

Views: 661

Answers (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!

Upvotes: 1

Related Questions