rustshared-librariesffikotlin-native

Rust Interop with Kotlin/Native


I am working on a simple Rust and Kotlin/Native interop.

I have a C header that declares my Rust function. I compiled the Rust code to a dynamic library. The cinterop tool builds the bindings based on the C header.

The entire process finishes smoothly. However, when I run the final compiled executable, I get a link error at runtime and complains that it can't find the dynamic library.

So, I placed the dynamic library in a system folder. The program ran successfully without any problems.

I want to know how to get this setup working without moving the dynamic library to a system folder.

The source code is on Github.

Btw, I also tried creating a static library and linking it rather. I found out that those def files really dont do much and I had to pass those link arguments manually to cinterop. Static libraries work out of the box since its linked to the klib.

Some insight on dynamic linking WITHOUT USING GRADLE would be nice!


Solution

  • The final executable can be linked to the dynamic library by passing this -rpath=/your_absolute_path_to_dynamic_lib flag to the -linker-option flag in the kotlinc-native compiler. The compile time linking should be done using the .def files or can be done manually, by passing flags to cinterop. The executable wont be linked if its NOT linked during compile time.