繁体   English   中英

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

[英]Loading a dynamic C shared library using JNI which also loads another shared library

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

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

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

提前致谢!

使用JNI加载的库似乎无法在运行时加载其他C库,因为我们在Java环境中。 我的假设是否正确?

没有。

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

我发现了类似的东西:

static {
    System.loadLibrary("second");
    System.loadLibrary("first");
}

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

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

Test.java

public class Test {

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

    private native static void test();

    static {
        System.loadLibrary("first");
    }
} 

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

second.c - libsecond.so的唯一翻译单元

#include <stdio.h>

void
second(void)
{
  printf("hello from second\n");
}

Makefile文件

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

Test.h可以通过javah Test 请注意, libfirst.solibsecond.so未链接在一起。

所有通常的规则都适用于从Java加载的库中的dopen() 特别是,检查你的LD_LIBRARY_PATHrpath,runpath等。另见dlopen失败:无法打开共享对象文件:没有这样的文件或目录

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM