https://kotlinlang.org logo
#kotlin-native
Title
# kotlin-native
c

cyberavater

11/12/2023, 5:46 PM
How do I use c libraries in in kotlin/native? take this one for example https://github.com/libarchive/libarchive Is there a straightforward way to add this to my project with Gradle? (I've seen some docs that say's manually downloading and make def file from header but I'd like some automatic solution by version tag)
j

jw

11/12/2023, 8:58 PM
A download task and copy task to extract will get you very far toward only having a URL and a def file rather than checked-in sources
a

Adam S

11/12/2023, 9:36 PM
It's possible to download sources by defining an Ivy repo in Gradle https://stackoverflow.com/a/34327202/4161471 This is pretty neat because you can then define a custom Configuration and use the Gradle
dependencies {}
block, with a git tag or version. I use this in Dokkatoo to download the Dokka source code (using a buildSrc convention plugin https://github.com/adamko-dev/dokkatoo/blob/v2.0.0/buildSrc/src/main/kotlin/buildsrc/conventions/dokka-source-downloader.gradle.kts)
The most difficult thing with building C libraries for use in Kotlin Native is that they often need to be built using the gcc that Kotlin Native itself uses, and it's a specific version that often isn't available to download. I tried building rocksdb from sources for use in a K/N project but I don't think I managed to get all platforms working. The project is here, if you'd like to look https://github.com/aSemy/kotlin-on-the-rocksdb/blob/feat/embedded_static_libs/externals/lib-rocksdb/build.gradle.kts Maybe it's easier for libarchive...
1
c

cyberavater

11/13/2023, 9:23 AM
Thanks bro, I'll look into it and ask again if stuck
m

Matt Nelson

11/14/2023, 4:32 AM
If you wanted to compile the C library as a static lib, CKLib is where it's at. It uses the kotlin llvm compiler and embeds the bitcode into the
.klib
https://github.com/touchlab/cklib
c

cyberavater

11/16/2023, 9:24 AM
@Matt Nelson Hi! thanks for the suggestion, it seems like what I'd need. But one more thing that I'd like to know is whether this .klib will work on work on Android too (via JNI maybe)?
m

Matt Nelson

11/16/2023, 10:56 AM
For Jvm/Android I use docker to compile native code. See: https://github.com/05nelsonm/build-env Usage Example: https://github.com/05nelsonm/kmp-tor-binary/blob/master/external/task.sh
🙌 1
2 Views