问题
I have a Java program that needs to load a native code library (through JNI with SWIG) but when I tried to run it I got an error as follows:
Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/local/lib/libmyapp_jni.so.0.0.0: /usr/local/lib/libmyapp_jni.so.0.0.0: undefined symbol: __cxa_pure_virtual
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1939)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1864)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1854)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at org.myapp.myapp.main(myapp.java:11)
by using ldd -d /usr/local/lib/libmyapp_jni.so.0.0.0
$ldd -d /usr/local/lib/libmyapp_jni.so.0
linux-vdso.so.1 => (0x00007ffffedfe000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007feacde2a000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007feacdc26000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007feacda0e000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007feacd7f8000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007feacd42f000)
/lib64/ld-linux-x86-64.so.2 (0x00007feace5a2000)
undefined symbol: __cxa_pure_virtual (/usr/local/lib/libmyapp_jni.so.0)
undefined symbol: _ZTVN10__cxxabiv120__si_class_type_infoE (/usr/local/lib/libmyapp_jni.so.0)
undefined symbol: _ZTVN10__cxxabiv117__class_type_infoE (/usr/local/lib/libmyapp_jni.so.0)
undefined symbol: _ZTVN10__cxxabiv121__vmi_class_type_infoE (/usr/local/lib/libmyapp_jni.so.0)
undefined symbol: _ZSt7nothrow (/usr/local/lib/libmyapp_jni.so.0)
undefined symbol: __gxx_personality_v0 (/usr/local/lib/libmyapp_jni.so.0)
I really wonder where went wrong?
Any thoughts?
Many thanks.
回答1:
After rechecking all the answers related to my question on stackoverflow, I found out the answer by Mark Seaborn to question: What is the purpose of __cxa_pure_virtual? fixed my problem.
As my jni library is managed by autotools, after adding "-Wl,--gc-sections -fPIC -shared -lstdc++" to AM_LDFLAGS of Makefile.am problems solved. libstdc++ might not necessary if code is compiled by a c++ compiler (e.g. g++).
Hope this could help others who have the same problem.
来源:https://stackoverflow.com/questions/19533014/undefined-symbol-cxa-pure-virtual-error-when-loading-library-from-java