Using C library from Kotlin through cinterop-generated bindings

I would like to use the Kotlin REPL to interact with C libraries. My understanding is that Kotlin/Native ships with a tool called cinterop that can generate bindings both for the JVM and for native compilation.

Executing cinterop with no arguments shows that the default flavor is jvm. So I executed
cintertop -def=libcurl.def and obtained the file nativelib-build\kotlin\libcurl\libcurl.kt.

Then I tried to compile this file using kotlinc (the one bundled with Kotlin not with Kotlin/Native) and the compilation failed with lots of unresolved references to the kotlinx, cnames and platform packages.

How can I compile the cinterop-generated bindings in order to use them in a Kotlin JVM program?

As far as I know you can’t. If you want to use native c code with kotlin jvm you should look into jni. Maybe we get some sort of bridge between kotlin native and kotlin jvm that can be used instead but right now the cinterop tool only works for native.

It is not possible right now. There is no bridge between kotlin-native and kotlin-jvm. The tooling for that was requested multiple times, and as far as I know, there is some work being done in that direction, but it is not that simple to do, so we have to either wait or contribute solutions.

1 Like