JNI静态变量是未定义的符号

时间:2013-07-18 19:35:51

标签: java-native-interface static-members unsatisfiedlinkerror

我正在尝试从另一个类的实例编辑静态变量。但是,我得到一个错误,说我的静态变量是我的共享库中的未定义符号。

staticClass.h

class staticClass{
public:

    static int num; //declaration

}

staticClass.cpp

#include "staticClass.h"

int num = 0; //definition

... //some functions that use num

的main.cpp

#include "staticClass.h"
#include <jni.h>
#include "main.h" //this is the header file for the JNI, I will not include this unless someone finds it necessary because all these variables are made up for clarity

JNIEXPORT jint JNICALL Java_main_foo(JNIEnv *env, jobject thisobj, jint someInt){

    staticClass example;
    int intIWant = (int)someint;

    example.num = intIWant;    
    printf("%d\n",example.num);
}

这是错误消息:

Exception in thread "main" java.lang.UnsatisfiedLinkError: .../libfoo.so: .../libfoo.so: undefined symbol: _ZN5staticClass9numE
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1935)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1860)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1850)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at tcr.<clinit>(tcr.java:9)

所以是的,我知道UnsatisfiedLinkError是多么棘手,但任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

错误的定义,在staticClass.cpp中

使用 int staticClass :: num = 0; // //定义