C Interop with cinterop and .def Files
Call C libraries from Kotlin/Native using cinterop tool and definition files.
Why C Interop?
Kotlin/Native can call C libraries directly — no JNI wrappers, no bridging layer. This enables using platform system libraries, hardware drivers, or any C-compatible library from Kotlin code compiled to native binaries.
The .def File
A .def file describes the C library to import. It specifies headers, library name, and optional compiler/linker flags. Place it in src/nativeInterop/cinterop/:
# src/nativeInterop/cinterop/libcurl.def
headers = curl/curl.h
headerFilter = curl/**
linkerOpts.linux = -lcurl
linkerOpts.macos = -lcurl