0Pricing
Kotlin Academy · Lesson

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

All lessons in this course

  1. Kotlin/Native Overview: Targets, Toolchain & Compilation
  2. Memory Management in Kotlin/Native: The New MM
  3. C Interop with cinterop and .def Files
  4. Kotlin/WASM: Compiling to WebAssembly for the Browser
← Back to Kotlin Academy