“错误:'xxx'未声明”_and_“错误:未使用的变量'xxx'”同时出现在同一个文件中

时间:2011-12-25 16:04:21

标签: c java-native-interface

我对JNI有一个非常奇怪的编译问题,根本不知道如何解决它......

这是一个在我的Java代码中从静态上下文调用的函数:

private static final Logger logger
    = LoggerFactory.getLogger(PamHandle.class);

private static native void initLog(final Logger logger);

static {
    System.loadLibrary("pam4j");
    initLog(logger);
}

声明此功能的标题如下:

#ifndef __LOG__H__
#define __LOG__H__

#include <jni.h>

JNIEXPORT void JNICALL Java_org_eel_kitchen_pam_PamHandle_initLog(JNIEnv *env,
    jclass cls, jobject jlogger);

struct javaLogger {
    jobject logger;
    jmethodID debugID;
    void (*debug)(JNIEnv *env, struct javaLogger *logger, char *msg);
};

extern struct javaLogger logger;

void debug(JNIEnv *env, struct javaLogger *logger, char *msg);

#endif /* __LOG_H__ */

这是C文件:

#include "log.h"

struct javaLogger logger;

JNIEXPORT void JNICALL Java_org_eel_kitchen_pam_PamHandle_initLog(JNIEnv *env,
    jclass cls, jobject jlogger)
{
    jclass class = (*env)->GetObjectClass(env, jlogger);

    logger.logger = jlogger;
    logger.debugID = (*env)->GetMethodID(env, class, "debug",
        "(Ljava/lang/String;)V");
    logger.debug = debug;
}

void debug(JNIEnv *env, struct javaLogger *logger, char *msg)
{
    jstring jsmg = (*env)->NewStringUTF(env, (const char *)msg);

    if (jmsg)
        (*env)->CallVoidMethod(env, logger->logger, logger->debugID, jmsg);
}

但是当我尝试编译它时(使用gcc -c -Wall -Werror -Wstrict-prototypes -Wmissing-prototypes -fPIC -g -c -I$JAVA_HOME/include -I$JAVA_HOME/include/linux log.c),这是我得到的输出:

log.c: In function ‘debug’:
log.c:20:9: error: ‘jmsg’ undeclared (first use in this function)
log.c:20:9: note: each undeclared identifier is reported only once for each function it appears in
cc1: warnings being treated as errors
log.c:18:13: error: unused variable ‘jsmg’
呃......我非常惊讶。这里发生了什么? :/我必须遗漏一些非常明显的东西,我只是无法想象......

1 个答案:

答案 0 :(得分:4)

jmsgjsmg是两回事。