JNI “symbol lookup error” in shared library on Linux

前端 未结 2 516
不知归路
不知归路 2021-01-04 12:39

What do you do when the Java VM has a “symbol lookup error” when executing a JNI function? The symbol lookup error is not in the primary shared object library that support

2条回答
  •  孤街浪徒
    2021-01-04 13:22

    You can get rid of it by using LD_PRELOAD (but i don't think this is a proper solution)

    The Story

    I faced the same problem, and my scenario is...

    *javaHelloWorldApp.java --> JNI_Hello_world.c --> this native c function calls to snmp library*

    java: symbol lookup error: /home/source/bin/libmytest.so: undefined symbol: init_snmp

    I used LD_PRELOAD as shown here and solves the problem for now. export LD_PRELOAD=/usr/local/lib/libnetsnmp.so.30

    Open Question

    ldd gives, (note, there is no reference to snmp library)

    ldd ../libmytest/bin/libmytest.so linux-gate.so.1 => (0xb7777000) libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb75a3000) /lib/ld-linux.so.2 (0xb7778000)

    with export LD_PRELOAD=/usr/local/lib/libnetsnmp.so.30 I see the references to snmp,

    snmp$ldd ../libredsnmp/bin/libredsnmp.so

    linux-gate.so.1 =>  (0xb770c000)
        /usr/local/lib/libnetsnmp.so.30 (0xb763a000)
        libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb746c000)
        librt.so.1 => /lib/i386-linux-gnu/librt.so.1 (0xb7462000)
        libcrypto.so.1.0.0 => /lib/i386-linux-gnu/libcrypto.so.1.0.0 (0xb72b7000)
        /lib/ld-linux.so.2 (0xb770d000)
        libpthread.so.0 => /lib/i386-linux-gnu/libpthread.so.0 (0xb729c000)
        libdl.so.2 => /lib/i386-linux-gnu/libdl.so.2 (0xb7297000)
        libz.so.1 => /lib/i386-linux-gnu/libz.so.1 (0xb7281000)
    

    my compilation line,

    gcc -fPIC -shared -o ./bin/libmytest.so -I/usr/lib/jvm/java-6-openjdk-i386/include/ -I/usr/lib/jvm/java-6-openjdk-i386/include/linux -static -lc JNI_Hello_world.c

    Other options i tried, -L/usr/local/lib -lnetsnmp, doesn't have any effect so i removed from compilation.

    EDIT-1

    got it worked with these two commands and no other environment variable, First, compile the source

    gcc -c -fPIC ./mytest.c -o mytest.o -I$(JAVA_INC_PATH) -I$(JAVA_INC_PATH)linux
    

    Now, use -rpath linker option when building the shared library.

    gcc -shared -o ./bin/libmytest.so  mytest.o -Wl,-rpath,/usr/local/lib -L/usr/local/lib -lnetsnmp
    

    For Detailed Reference

    For a detailed description refer this book (or just this google book search result, @ ch.41.10)

提交回复
热议问题