https://kotlinlang.org logo
a

Alexandre Brown

04/01/2022, 7:29 PM
Hello, I'm new to native, is it possible to use a c++ library from kotlin ? Use case : Multiplatform kotlin library that targets native would be able to use existing c++ library in its native code definition (since most Kotlin Multiplatform library are JVM or JS only)
b

Big Chungus

04/01/2022, 7:35 PM
Only via C. Direct cpp interop is postponed
a

Alexandre Brown

04/01/2022, 7:37 PM
In general is it the recommended approach (to use C interop and try to call c libs)? Or is it better to look for ways the users of your kotlin multiplatform lib could call the JS target instead of native ? Native seems to be still relatively new in the ecosystem, not a lot of KMP seem to offer it
From my understanding this means that if you want to call a c++ lib then trying to adapt your lib users to use the JS target might be your only option.
b

Big Chungus

04/01/2022, 7:39 PM
Not sure how cpp relates to js, sorry 😀
a

Alexandre Brown

04/01/2022, 7:40 PM
What I'm saying is maybe if your library users are c++ apps, then maybe it's better to ditch native and target JS (Assuming c++ can call JS). Would have to double check but pretty sure c++ can call JS
b

Big Chungus

04/01/2022, 7:41 PM
But kotlin really cares only about the headers. Some cpp issues provide c headers so see if your lib has that
a

Alexandre Brown

04/01/2022, 7:41 PM
Ok thanks
b

Big Chungus

04/01/2022, 7:41 PM
Even if cpp can call js, that would be a significant performance hit and as such I'd personally try to avoid it.
a

Alexandre Brown

04/01/2022, 7:42 PM
Yes for sure, but at least that'd be a solution to start off with.
b

Big Chungus

04/01/2022, 8:06 PM
I think writing your own C layer to interop between the two would be a better way to go
a

Alexandre Brown

04/01/2022, 8:07 PM
Interesting idea, I would need to define C headers for the kotlin lib and call my c++ lib from
.c
files?
b

Big Chungus

04/01/2022, 8:08 PM
Something like that. I've never actually done it myself, just an idea.
👍 1
18 Views