使用SWIG%native函数的编译错误 [英] Compile Error Using SWIG %native Function
问题描述
我正在尝试使用SWIG%native函数编译本机JNI调用,并且我得到了头文件的以下异常.我在下面的生成文件中同时包含了jdk-1.6.0_30/include和jdk-1.6.0_30/include/linux,有什么想法吗?我在32位linux上编译.
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();
例外:
[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)
推荐答案
我认为您的顺序错误.您需要先写:
I think you've got this in the wrong order. You need to first write:
%{
JNIEXPORT jobject JNICALL Java_test_jni_GetData(JNIEnv *, jclass);
%}
这样当您写:
%native (GetData) jobject GetData();
SWIG将生成的包装器代码中已经存在该函数的声明.
a declaration of the function already exists in the wrapper code that SWIG will generate.
如果其中包含本机函数,则不能像这样直接%include
Sample.h. SWIG预处理器不知道JNIEXPORT或JNICALL是什么-它们看起来像语法错误,除非已将它们作为宏给出.
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.
我建议将本机函数放在单独的头文件中,然后仅 #include
,而不是%include
该文件.
I'd suggest putting the native functions in a separate header file which you then only #include
, not %include
that file.
但是如果您没有打开几个选项,则对SWIG隐藏了本机函数,例如:
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
头文件中的
将起作用,或者您可以修改接口文件以使SWIG忽略JNIEXPORT和JNICALL:
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();
尽管在这种情况下,SWIG也会尝试包装它并采用%native
指令,这可能不是您想要的!
Although in that case SWIG will try to wrap it as well as take the %native
directive which probably isn't what you want!
这篇关于使用SWIG%native函数的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!