undefined symbol: _ZTTN2cv4SURFE - java

I want to call C++ code from java using JNI. I already followed many tutorials on this, and all are working, when the C++ code is "simple". But the C++ code which I am going to call inludes the opencv library and uses various functions of it. And this is, where I get problems...
What I am doing is the following:
1.) g++ -fPIC -shared -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include/linux -o libHello.so Hello.cpp
which creates the .so file.
2.) I copy my created .so file to the directory where I use it, just as I did with the tutorials, and load it:
static {
System.loadLibrary("Hello");
}
But I get an UnSatisfiedLinkError with undefined symbol: _ZTTN2cv4SURFE
Most probably there is an error in the first step, such that it can not find my opencv library. But I don't know what the correct command would be.
ldd -d libHello.so produces:
linux-gate.so.1 => (0xb777d000)
libstdc++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6 (0xb765e000)
libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xb7640000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb7496000)
libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xb746a000)
/lib/ld-linux.so.2 (0xb777e000)
undefined symbol: _ZTTN2cv4SURFE (./libHello.so)
undefined symbol: _ZTV11CvStatModel (./libHello.so)
undefined symbol: _ZTVN2cv24BackgroundSubtractorMOG2E (./libHello.so)
undefined symbol: _ZTVN2cv4SURFE (./libHello.so)
undefined symbol: _ZTV5CvSVM (./libHello.so)
edit:
For anyone else getting the same problem:
1.) As some people mentioned, I was not linking against the opencv library.
2.) But this was not enough. I was using this command, as it is also done in some tutorials:
g++ -shared -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include/linux -I/usr/local/include/opencv -L/usr/local/lib -lopencv_nonfree -lopencv_features2d -lopencv_core -lopencv_highgui Hello.cpp -o libHello.so
which caused the undefined symbol error when doing "ldd"
I changed the command to:
g++ Hello.cpp -shared -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include/linux -I/usr/local/include/opencv -L/usr/local/lib -lopencv_nonfree -lopencv_features2d -lopencv_core -lopencv_highgui -o libHello.so
i.e. I just wrote Hello.cpp directly after g++ instead at the end. And this solved the problem.
I don't know why this is working and the previos one didn't. But maybe someone can tell me.

You may need to specify a path to your additional libraries so the JVM can load them. Add an argument to Java:
-Djava.library.path=/path/to/opencv/libs:/other/paths/as/needed
This argument must come before your class name so that Java knows it is an argument for the JVM and not for your program.

Related

gcc compilation of C file: error in mainCRTStartup

I'm trying to compile a 27KB .c file in Powershell (Windows 10) using java and the TDM-GCC MinGW provided on sourceforge (https://sourceforge.net/projects/tdm-gcc/). The code I am using (from published paper http://journals.plos.org/ploscompbiol/article?id=10.1371%2Fjournal.pcbi.1001059) generates an executable file using the following code:
gcc -O3 -finline-functions -fomit-frame-pointer -DNDEBUG -fno-strict-aliasing --param max-inline-insns-single=1800 -std=c99 -msse2 -DHAVE_SSE2 -DDSFMT_MEXP=521 -o toy_bat toy_bat.c
When I compile a 6KB example file this works fine. However when I compile the 27KB file I get the following error:
C:/TDM-GCC-64/bin/.../lib/crt2.o: In function '__tmainCRTStartup' followed by a load of 'relocation truncated to fit' messages.
From what I've read onling this error normally comes from a file being too large, but I find it hard to believe that A 27KB file is causing this. Does anyone have any suggestions as to what may be causing this?
Thanks!

java: symbol lookup error: undefined symbol: _ZN2cv6String8allocateEm

When I type
java -Djava.library.path="path to opencv lib folder" : "path to my libJavaFileName.so" JavaFileName
I receive the following message:
java: symbol lookup error: "path to my libJavaFileName.so": undefined symbol: _ZN2cv6String8allocateEm
What is the meaning of undefined symbol: _ZN2cv6String8allocateEm and how can I fix it?
I suspect you have a version mismatch between the Java JNI bindings and your version of opencv.
The undefined symbol: _ZN2cv6String8allocateEm arises because your libJavaFileName.so contains a reference to that symbol but the linker cannot find any object file which defines it.
You can use the program c++filt to unmangle the symbol in to a readable name:
> c++filt -n _ZN2cv6String8allocateEm
cv::String::allocate(unsigned long)
If you look in the source of opencv 3.1 you can see that this function does indeed exist (its in stl.cpp in the core module).
The symbol is not defined in the libraries for opencv on my machine (version 2.4.8) but it is defined in libopencv_core.so if I build version 3.1 from source.
Therefore I'm guessing its probably the case that you need to install a newer version of opencv.
Its also worth checking that its actually loading the version of the shared library that you think it is. If you do:
strace -o log.txt java rest_of_your_command_line
Then you can look through log.txt to see which shared libraries are being opened. Try grepping this file for opencv to see if the right version is being loaded.
Your library uses the symbol cv::String::allocate( unsigned long ).
You need to load the proper shared object(s) that provide the symbols that your native code depends on. The best way to do that is to compile your native shared object such that it has dynamic dependencies on those objects by using the proper -L library_name option(s).

linux 64bit - jni linking error

I'm trying to get a working example of calling Java from C using JNI on ubuntu 64bit.
The code from: calling java function from c using jni I am compiling using:
gcc test.c -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/linux -c
gcc test.o -L/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64/server/ -ljvm -o jniTest
This doesnt generate any errors but if I try ldd jniTest I get the following:
linux-vdso.so.1 => (0x00007fffe55d5000)
libjvm.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f85f2928000)
/lib64/ld-linux-x86-64.so.2 (0x00007f85f2d0a000)
And if I try ./jniTest it gives this error:
test.o: In function `create_vm':
test.c:(.text+0x35): undefined reference to `JNI_CreateJavaVM'
collect2: error: ld returned 1 exit status
With the -L/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64/server/ you tell the linker where to find libjvm.so. At runtime you will need to do the same (http://linux.die.net/man/8/ld.so). And you'll also need the parent directory because other needed libs are in there. One way to do this is by specifying it on the command line: LD_LIBRARY_PATH=/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64/server:/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64 ./jniTest.
A way to get around the need to specify the path is to not link to libjvm.so at all but to find it at runtime and then use the functions in dlfcn.h (http://linux.die.net/man/3/dlopen, http://linux.die.net/man/3/dlsym) to use it.

How to build javafuse on Ubuntu?

I'm trying to implement the JavaFuse project but when I execute the make command I'm getting this error:
gcc -shared build/contextClass.o build/conninfoClass.o build/javafuse_jni.o build/statvfsClass.o build/fsClass.o build/utimebufClass.o build/fileinfoClass.o build/statClass.o -o build/libjavafuse.so
Error:
build/contextClass.o: file not recognized: File format not recognized
collect2: ld returned 1 exit status
make: * [libjavafuse.so] Error 1
I'm using Ubuntu.
Could anyone please suggest what is the problem?
I recommend fuse-jna, it's a project that I've used and it doesn't need to be compiled and also, it's alive and you may find it here fuse-jna. hope it'll help you

trouble installing JRI (R to java)

I'm wondering if anyone has experience with my error message or has any advice.
When I run the ./configure file it runs with no error. When I run the make file I get
make -C src JRI.jar
gcc-4.2 -arch x86_64 -std=gnu99 -c -o Rengine.o Rengine.c -g -Iinclude -g -O2 -I/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/include -I/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/include/. -fno-common -I/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/include -I/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/include/. -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/x86_64
Rengine.c: In function ‘Java_org_rosuda_JRI_Rengine_rniParse’:
Rengine.c:89: error: too few arguments to function ‘R_ParseVector’
Rengine.c: In function ‘Java_org_rosuda_JRI_Rengine_rniRunMainLoop’:
Rengine.c:181: warning: implicit declaration of function ‘run_Rmainloop’
Rengine.c: In function ‘Java_org_rosuda_JRI_Rengine_rniGetList’:
Rengine.c:313: warning: implicit declaration of function ‘jri_error’
Rengine.c: In function ‘Java_org_rosuda_JRI_Rengine_rniStop’:
Rengine.c:377: warning: implicit declaration of function ‘getpid’
make[1]: *** [Rengine.o] Error 1
make: *** [src/JRI.jar] Error 2
I've seen the same or similar question asked on other forums but it didn't seem that anyone had there question answered. Wondering if anyone knew what this means on stackoverflow.
Try using the rJava distribution instead (JRI is now part of rJava).

Categories

Resources