Fork me on GitHub
#graalvm
<
2022-09-29
>
Crispin14:09:35

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?

Crispin14:09:17

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

Crispin14:09:58

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)

Crispin14:09:43

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

Crispin14:09:30

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

Crispin14:09:29

(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)

lukasz14:09:18

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

Crispin14:09:23

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

borkdude14:09:38

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

Crispin14:09:14

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

Crispin14:09:02

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

phronmophobic18:09:23

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

Crispin23:09:32

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