I really wanna use C++ library in my Kotlin/Native...
# multiplatform
e
I really wanna use C++ library in my Kotlin/Native, how to reach that?
f
You can’t use C++ directly with Kotlin, you can only use C
question which platform do you want to target?
a
I did this a while ago, maybe I can help. I created K/N bindings for the Jolt C++ physics engine. It wasn't easy! K/N can automatically create cinterop bindings based on C code - but NOT from C++ code. So, if the C++ library already has a 'C compatibility layer', then K/N should just work (e.g. RocksDB is a C++ library, and they also provide C bindings). But, provided C bindings are a pretty rare case - most C++ libs don't have that. Instead, you'd have to create them manually. If you just need a few functions/classes from the C++ lib then writing manual bindings is the easiest way. Try searching for
c bindings for c++
online, there are a few good articles/blogs around. However, if you need exhaustive bindings for a lot of the C++ lib, then it's a much more challenging task. It's hard to write the bindings by hand, and it's a lot of work to test and maintain them. That's the problem I had with Jolt. Fortunately, Jolt has Wasm bindings, with an IDL descriptor file, which is basically the same as the required C bindings. So, I created a 'C bindings generator' that parsed the IDL file, and from it created C bindings for the C++ lib. I'm gong to open-source this at some point, but it might take a while, and the generator is very specific to Jolt. But maybe that general approach could help.
🔥 1