使用JNI加载动态C共享库,JNI还加载另一个共享库

Ori*_*Gil 5 c eclipse linux java-native-interface shared-libraries

在Java eclipse(Linux)上使用JNI,我正在加载一个名为first.so的动态共享库.到目前为止一切顺利.问题在于,first.so还会加载一个名为second.so的动态库.

运行程序时,我收到许多关于位于second.so中的符号的"未定义符号"错误.

使用JNI加载的库似乎无法在运行时加载其他C库,因为我们在Java环境中.我的假设是否正确?我是否需要特殊的编译标志来编译first.so库,或者是告诉eclipse它会在运行时尝试加载.so的特殊参数吗?

提前致谢!

lda*_*v1s 2

看来使用 JNI 加载的库无法在运行时加载其他 C 库,因为我们处于 Java 环境中。我的假设正确吗?

不。

如何libsecond.so使用libfirst.so?它是链接依赖项,还是由 加载的dlopen

我发现类似的东西:

static {
    System.loadLibrary("second");
    System.loadLibrary("first");
}
Run Code Online (Sandbox Code Playgroud)

在使用 JNI 的类中通常可以工作。

编辑:现在我知道你如何加载libsecond.so这对我有用:

测试.java

public class Test {

    public static void main (String args[]) {
        test();
    }

    private native static void test();

    static {
        System.loadLibrary("first");
    }
} 
Run Code Online (Sandbox Code Playgroud)

first.c——唯一的翻译单元libfirst.so

#include <jni.h>
#include "Test.h"
#include <dlfcn.h>
#define LIBNAME "libsecond.so"

#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Test
 * Method:    test
 * Signature: ()V
 */
JNIEXPORT void JNICALL
Java_Test_test(JNIEnv *env , jclass cls)
{
  void* h;
  void (*sym)(void);

  h = dlopen(LIBNAME, RTLD_LAZY|RTLD_GLOBAL);
  if (h) {
    printf("dlopen " LIBNAME " worked\n");
    sym = (void (*)(void))dlsym(h,"second");
    sym();
  } else {
    printf("dlopen " LIBNAME " failed\n");
  }
}

#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)

secondary.c——唯一的翻译单元libsecond.so

#include <stdio.h>

void
second(void)
{
  printf("hello from second\n");
}
Run Code Online (Sandbox Code Playgroud)

生成文件

CFLAGS=-fPIC

all : libfirst.so libsecond.so 

libsecond.so : second.o
        $(CC) -shared -Wl,-soname,libsecond.so.0 -o $@ $^ -lc

libfirst.so : first.o
        $(CC) -shared -Wl,-soname,libfirst.so.0 -o $@ $^ -ldl -lc

clean:
        rm -f *.o *.so
Run Code Online (Sandbox Code Playgroud)

Test.h可以由 生产javah Test。请注意,libfirst.solibsecond.so没有链接在一起。