【问题标题】:Add external .so library to jni project with nar-maven-plugin使用 nar-maven-plugin 将外部 .so 库添加到 jni 项目
【发布时间】:2017-06-09 07:06:10
【问题描述】:

我有 file.c,我想将它包含到我的 Java 应用程序中。 我已经创建了JNI 模块并将其包含在nar-maven-plugin 的应用程序中。

这是它的配置:

<plugin>
    <groupId>com.github.maven-nar</groupId>
    <artifactId>nar-maven-plugin</artifactId>
    <version>3.5.2</version>
    <extensions>true</extensions>
    <configuration>
        <libraries>
            <library>
                <type>jni</type>
                <narSystemPackage>my.package.nativelib</narSystemPackage>
            </library>
        </libraries>
    </configuration>
</plugin>

在我运行mvn clean install 之后,它会创建libmy-native-lib.so 文件。

我设法从 Java 访问 C 函数,但有一个问题:file.c 使用外部 libexternal.so 库(作为 #include "external.h" 包含在 file.c 中),当它从 @987654331 访问任何函数时@ 它因“符号查找错误”而失败。

我尝试过使用

static {
    System.load("/usr/lib/libexternal.so");
}

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

但似乎没有任何效果。

那么如何将外部 *.so 库添加到 JNI 项目中?

【问题讨论】:

    标签: java c maven java-native-interface shared-libraries


    【解决方案1】:

    看这里,看看如何处理 JNI 和共享库:

    http://jnicookbook.owsiak.org/recipe-No-023/

    您需要做的是将您的共享库 (JNI) 与 JNI 代码使用的库链接。

    然后,您可以为该库提供您的代码,或者您可以要求用户将其安装在系统中(例如通过包)。

    享受 JNI 的乐趣

    【讨论】:

    • 据我了解,您这里没有 *.so 库,您有另一个 *.c 文件。是不是很不一样?
    • 这个另一个文件“c/recipeNo023_AnotherFunction.c”被编译为共享库。然后,“c/recipeNo023_HelloWorld.c”与这个共享库链接。你可以看看这里到底发生了什么:github.com/mkowsiak/jnicookbook/tree/master/recipeNo023
    • 是的,对不起,我的错。我是 C 语言的菜鸟。当我尝试运行第二个命令时,我得到 cc: error: unrecognized command line option ‘-install_name’
    • 您可以删除此选项。我想这是我的 macOS 版本中剩下的一些东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 2018-03-24
    相关资源
    最近更新 更多