Miguel
Miguel

Reputation: 303

Is it possible to use a C++ library from a kotlin multiplatform mobile app?

I have a C++ library that I need to use from android and iOS, so my idea is to create a multiplatform module that would wrap the library to use it from both implementations.

In other hand, I've seen a sample where C code is used by kotlin native: https://theprogrammershangout.com/resources/kotlin/native/creating-c-bindings.md/

Also this one from kotlin documentation: https://kotlinlang.org/docs/native-c-interop.html

Would that be possible to make that interoperability work with jvm and iOS?

Upvotes: 7

Views: 3813

Answers (1)

Kevin Galligan
Kevin Galligan

Reputation: 17342

You cannot directly interop with C++ from Kotlin/Native (using cinterop). Your C++ library would need to expose an extern C version of itself, or you'd need to create a C compatible bridge to your C++ library, and let cinterop look at that.

It might be more natural to wrap that with an Objective-C interface rather than a C one, but that will obviously depend on what the library does and your comfort level with C vs Objective-C.

Upvotes: 7

Related Questions