Sean Sen Wang
Sean Sen Wang

Reputation: 193

JNI Static Variable is undefined symbol

I am trying to edit a static variable from an instance of another class. However, I get an error that says my static variable is an undefined symbol in my shared library.

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);
}

this is the error message:

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)

So yeah, I know how tricky UnsatisfiedLinkError can be but any help would be appreciated.

Upvotes: 0

Views: 438

Answers (1)

manuell
manuell

Reputation: 7620

Wrong definition, in staticClass.cpp

Use int staticClass::num = 0; // //definition

Upvotes: 1

Related Questions