2013-07-18 34 views
0

我想從另一個類的實例編輯一個靜態變量。然而,我得到一個錯誤,說我的靜態變量是我的共享庫中的一個未定義的符號。JNI靜態變量是未定義的符號

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可以,但任何幫助將不勝感激。

+0

@AlexCohn有我的電腦上沒有JNI/Android.mk文件。 –

回答

1

錯誤的定義,在staticClass.cpp

使用 INT staticClass :: NUM = 0; // //定義

+0

我試過這個。我得到這個錯誤:'*** glibc的檢測***的java:雙重釋放或腐敗(出):0x00007f4017220ad0 ***' –

+0

單純的 「INT NUM = 0;」在CPP文件中與類無關,無論類和源文件的名稱如何。僅僅是「int num = 0;」只需定義一個名爲num的全局變量,而不是靜態成員。 – manuell

相關問題