I'm testing a simple example of jni (on windows 7) by calling java functions in a c++ main.
I read that "relocation truncated to fit" error is due to a large project (not the case : just a main and a compiled java class named Sample with a static method intMethod(int)), linking object files in a different order (i only have one object file), or an expansive mapping scheme ... stack overflow thread
I use the following compiling command line:
g++ -D __int64="long long" -I"C:\Program Files\Java\jdk1.7.0_55\include" -I"C:\Program Files\Java\jdk1.7.0_55\include\win32" -L"C:\Program Files\Java\jdk1.7.0_55\lib" -o sample sample.cpp -ljvm
and my main function:
#include <jni.h>
#include <cstring>
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif
int main()
{
JavaVMOption options[1];
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
jclass cls;
jmethodID mid;
jint square;
jboolean no;
options[0].optionString ="-Djava.class.path=.";
//memset(&vm_args, 0, sizeof(vm_args));
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
cls = env->FindClass("Sample");
mid = env->GetStaticMethodID(cls, "intMethod", "(I)I");
if(mid !=0)
{ square = env->CallStaticIntMethod(cls, mid);
printf("Result of intMethod: %d\n", square);
}
jvm->DestroyJavaVM();
return 0;
}
The error is the following:
C:\Program Files\Java\jdk1.7.0_55\lib/jvm.lib(jvm.dll.b):(.text+0x2): relocation truncated to fit: R_X86_64_32 against symbol __imp_JNI_CreateJavaVM defined in section .idata$5 in C:\Program Files\Java\jdk1.7.0_55\lib/jvm.lib(jvm.dll.b)
error: collect2: ld returned 1 exit status
Can't seem to find a good answer anywhere about how to handle this error methodically!
-> Solution: Add -L"C:\Program Files\Java\jdk1.7.0_55\jre\bin\server" in compilation