Search code examples
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

  • In contrast to Windows, Linux does not automatically search for libraries outside of the system path.

    You can use a modified search path for a program by linking it with the -rpath '$ORIGIN' option. (note the single quotes) The dynamic linker (ld.so) will resolve this path to the directory of the running program and look there for libraries too.