krakowski / gradle-jextract

Gradle plugin for integrating Project Panama's jextract tool
https://plugins.gradle.org/plugin/io.github.krakowski.jextract
GNU General Public License v3.0
51 stars 10 forks source link

How to link static lib like `librtl.a` ? #24

Closed marwin1991 closed 2 weeks ago

marwin1991 commented 4 weeks ago

Environment

What would you like to ask?

How to link static libs?

I though it would be somtheing like this:

jextract {
    header("${project.projectDir}/src/main/c/native.h") {
        // The package under which all source files will be generated
        targetPackage = 'org.nativelib'

        // The generated class name
        className = 'NativeLib'

        libraries = ['nativeLib', ':/usr/lib/xharbour/librtl.a', ':/usr/lib/xharbour/libvm.a', ':/usr/lib/xharbour/libcommon.a', ':/usr/lib/xharbour/libmacro.a',
                     ':/usr/lib/xharbour/libpcrepos.a', ':/usr/lib/xharbour/liblang.a', ':/usr/lib/xharbour/librdd.a', ':/usr/lib/xharbour/libct.a',
                     ':/usr/lib/xharbour/libdbfcdx.a',  ':/usr/lib/xharbour/libdbfntx.a', ':/usr/lib/xharbour/libdbffpt.a', ':/usr/lib/xharbour/libhbsix.a',
                     ':/usr/lib/xharbour/libcodepage.a', ':/usr/lib/xharbour/libtip.a', ':/usr/lib/xharbour/libhbcomm.a', ':/usr/lib/xharbour/libdebug.a'

        ]
    }
}

But linking options are:

-shared
-Wl,-soname,libnativeLib.so
-o
/home/pz2/repo/panama-demo-2/build/libs/nativeLib/shared/libnativeLib.so
/home/pz2/repo/panama-demo-2/build/objs/nativeLib/shared/nativeLibC/7iqn0imz8afmagv3khti3yfjc/native.o
/home/pz2/repo/panama-demo-2/build/objs/nativeLib/shared/nativeLibC/9or586zskwg4xeunnqb61mzz5/bccgenerator.o
/home/pz2/repo/panama-demo-2/build/objs/nativeLib/shared/nativeLibC/3s5u5prxpovrs082vjzqxybsk/native.o
-m64

Altho there is succes:

See file:///home/pz2/repo/panama-demo-2/build/tmp/linkNativeLibSharedLibrary/output.txt for all output for linkNativeLibSharedLibrary.
linking libnativeLib.so successful.

Finished linkNativeLibSharedLibrary, see full log file:///home/pz2/repo/panama-demo-2/build/tmp/linkNativeLibSharedLibrary/output.txt.

During execution I get error:

Hello and welcome!i = 1
i = 2
i = 3
i = 4
i = 5
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/pz2/repo/panama-demo-2/build/libs/nativeLib/shared/libnativeLib.so: /home/pz2/repo/panama-demo-2/build/libs/nativeLib/shared/libnativeLib.so: undefined symbol: HB_FUN_SPACE
        at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
        at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:331)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:197)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:139)
        at java.base/jdk.internal.loader.NativeLibraries.findFromPaths(NativeLibraries.java:259)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:251)
        at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2432)
        at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:916)
        at java.base/java.lang.System.loadLibrary(System.java:2068)
        at org.example.Main.main(Main.java:19)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/home/pz2/.sdkman/candidates/java/22.0.1-open/bin/java'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 778ms
5 actionable tasks: 5 executed
    ~/repo/panama-demo-2                                                                                                                                                                                                  1 ✘ 
    ~/repo/panama-demo-2  ./gradlew clean && ./gradlew run                                                                                                                                                                1 ✘ 

BUILD SUCCESSFUL in 427ms
1 actionable task: 1 executed

> Task :run FAILED
Hello and welcome!i = 1
i = 2
i = 3
i = 4
i = 5
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/pz2/repo/panama-demo-2/build/libs/nativeLib/shared/libnativeLib.so: /home/pz2/repo/panama-demo-2/build/libs/nativeLib/shared/libnativeLib.so: undefined symbol: HB_FUN_SPACE
        at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
        at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:331)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:197)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:139)
        at java.base/jdk.internal.loader.NativeLibraries.findFromPaths(NativeLibraries.java:259)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:251)
        at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2432)
        at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:916)
        at java.base/java.lang.System.loadLibrary(System.java:2068)
        at org.example.Main.main(Main.java:19)
krakowski commented 3 weeks ago

Hi @marwin1991,

on Linux systems jextract uses the dlopen function to load libraries at runtime (see Library Loading). Loading a static library using this function won't work.

The missing symbol HB_FUN_SPACE seems to come from xharbour. Did you link your shared library (nativeLib) against it?