|
小弟使用JAVA的JNI写动态连接库。恩~先上代码:
头文件 Cdemo.h
[code:1]
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Cdemo */
#ifndef _Included_Cdemo
#define _Included_Cdemo
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Cdemo
* Method: showMsgBox
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_Cdemo_showMsgBox
(JNIEnv *, jobject, jstring);
/*
* Class: Cdemo
* Method: nativeMethod
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Cdemo_nativeMethod
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
[/code:1]
C文件:
[code:1]
#include "Cdemo.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_Cdemo_showMsgBox(JNIEnv * env, jobject obj, jstring str)
{
const char *msg =(*env)->GetStringUTFChars(env,str,0);
printf("\tFrom DLL/SO!\t%s\n",msg);
(*env)->ReleaseStringUTFChars(env,str,msg);
}
JNIEXPORT void JNICALL Java_Cdemo_nativeMethod(JNIEnv * env, jobject obj)
{
jclass cls = (*env)->GetObjectClass(env, obj);
jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "()V");
if (mid == 0)
return;
printf("In C,about to enter Java\n");
(*env)->CallVoidMethod(env, obj, mid);
}
[/code:1]
Makefile
[code:1]
libdlltest.so:dlltest.o
gcc -Wall -rdynamic -shared -o libdlltest.so dlltest.o
dlltest.o:dlltest.c
gcc -Wall -c dlltest.c -I./ -I/usr/jdk1.5/include -I/usr/jdk1.5/include/linux
cl:
rm -rf *.o *.so
[/code:1]
问题是,这个程序使用GCC编译毫无问题,但是要使用g++编译,就会出现如下错误:
[code:1]
dlltest.c:11: 错误:‘->’ 的基操作数具有非指针类型 ‘JNIEnv_’
dlltest.c:15: 错误:‘->’ 的基操作数具有非指针类型 ‘JNIEnv_’
dlltest.c: In function ‘void Java_Cdemo_nativeMethod(JNIEnv*, _jobject*)’:
dlltest.c:21: 错误:‘->’ 的基操作数具有非指针类型 ‘JNIEnv_’
dlltest.c:22: 错误:‘->’ 的基操作数具有非指针类型 ‘JNIEnv_’
dlltest.c:26: 错误:‘->’ 的基操作数具有非指针类型 ‘JNIEnv_’
[/code:1]
但用GCC是没有问题的 |
|