Anyone knows how kotlin/native can interop with c+...
# kotlin-native
k
Anyone knows how kotlin/native can interop with c++ code? It's available now to wrap the c++ code with c ones, any better solutions?
m
You could build a static library and use the cinterop tool to interact with it (via headers) - for me this was the most straight forward way .But I think there is no escape from "extern C" right now 🤔
k
It's available to interop with c apis and data types. However, as many libraries are written in cpp, their headers contains c++ elements, like namespace, class, structs, memory management, etc. I have to give every c++ element a c wrapper, it takes a lot of efforts for me and the approach is much vulnerable to api changes in cpp codes.
m
Gotcha. Right now there is no interop with C++ and I doubt it will ever be. It seems like JetBrains is focusing mainly on iOS when it comes to native targets. You can see here how cinterop is beta since Kotlin 1.3 but cocoapods integration is already stable. It is sad because Kotlin/Native could be a great replacement for JVM backends if we had more mature tooling kodee sad
c
It is sad because Kotlin/Native could be a great replacement for JVM backends if we had more mature tooling
I don't see this happening. The JVM has had millions of dollars of investment over the past three decades to be as good as possible for backends. There's no beating the JVM for backends. Kotlin/Native is aimed towards embedded/client applications, which is why they are working with iOS integration. That's where there is a market.
m
@CLOVIS Yeah, a bit of exaggeration on my part. It definitely won't render the JVM obsolete, but there are scenarios where startup times and memory footprint are crucial. It would be nice to have some services running on bare metal and others on the JVM. At the end of the day, it's all about picking the right tool for the job. Sadly, right now, Kotlin/Native has a very narrow field of usefulness. However, I hope the tooling and ecosystem will grow, and Kotlin/Native will become as easy to use as, for example, Go. 🤗
c
^ I think Kotlin/WASM has a chance there. There are already Kubernetes options to run WASM modules natively, without a JVM or containers.
a
"However, as many libraries are written in cpp, their headers contains c++ elements, like namespace, class, structs, memory management, etc." you can create a copy C header with only the functions you want to expose, and the necessary structs that represent C++ class variables. use the copy C header to generate cinterop in the original c++ header, wrap the functions with extern C build a stactic C++ library using the original headers. user native.def file to include the library
âž• 1