graalvm

Crispin 2022-09-29T14:08:35.741259Z

Hi everyone. Im using native-image to build an executable of a clojure codebase that links with C code. It all works great by building the C as a *.a file and linking it in at compile time during native-image. But running the project under graals java, how do I consume the C library then?

Crispin 2022-09-29T14:09:17.143959Z

I can compile it as a *.so, but if I loadLibrary it when I call the C code I get an UnsatisfiedLink error.

Crispin 2022-09-29T14:09:58.031749Z

If I add JNI to the C library, and then compile it as a .so, and then loadLibrary it, then it does load and work (including the substrate C interop calls)

Crispin 2022-09-29T14:10:43.077689Z

Is there any way of loading vanilla compiled C into the graal java runtime?

Crispin 2022-09-29T14:11:30.938479Z

it will be slow and annoying to develop when every change requires a full native-image

Crispin 2022-09-29T14:13:29.547179Z

(examining the symbols in the JNI .so there is a JvRegisterClasses call that is not present in the plain .so file. I think this has the magic that makes loadLibrary link it correctly)

lukasz 2022-09-29T14:15:18.836549Z

it's been loooooong time since I touched C etc - does graal / jni support similar mechanism to LD_LIBRARY_PATH? That's how dynamic linking works in other languages/runtimes

Crispin 2022-09-29T14:16:23.446499Z

it does. thats all set up. the loadLibrary of the vanilla code loads... it just then doesnt work when called

borkdude 2022-09-29T14:17:38.541169Z

I think you need to do JNI in the JVM still so you have to maintain two versions of the FFI code

borkdude 2022-09-29T14:17:49.151499Z

Cc @huahaiy

Crispin 2022-09-29T14:18:14.986069Z

yeah thats what I thought too. Just hoping someone with more experience shows me a magical graal blessed way that I dont know about 🙂

Crispin 2022-09-29T14:19:02.443749Z

have to go sleep now. But will check back in the morning if anyone has any better solutions.

phronmophobic 2022-09-29T18:14:23.435429Z

I've started writing all my ffi code using dtype-next's ffi since you can define the interface once and use it in a number of contexts. I'm not sure it covers the exact contexts you've listed, but I think it does. If it doesn't, the ffi interface is extensible. https://github.com/cnuernber/dtype-next/tree/master/examples/clj-ffi

borkdude 2022-09-29T18:37:51.801459Z

Good tip!

Crispin 2022-09-29T23:10:32.505839Z

@smith.adriane that looks really great! I will give that a go. Thanks for the pointer.