cinterop mit C-Bibliotheken
Binden Sie eine C-Abhängigkeit zur Verwendung im gemeinsam genutzten Code ein.
cinterop mit C-Bibliotheken ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Reaching Into C
Sometimes the code you need is a plain C library. Kotlin/Native can call it directly through a tool called cinterop. 🔌
What cinterop Produces
cinterop reads C headers and generates Kotlin bindings. After that, the C functions and types look like ordinary Kotlin declarations you can call.
The .def File
You describe the library in a small def file: which headers to read and which library to link. This is the recipe cinterop follows.
headers = mylib.h
package = mylibWire It Into Gradle
Inside the kotlin block you register a cinterop entry for your target. Gradle then runs the tool and adds the generated bindings to compilation.
cinterops.create("mylib")Calling a C Function
Once generated, a C function is just a Kotlin function. Import it from your def package and call it like any other shared function.
import mylib.compute
val r = compute(21)C Pointers Become CPointer
A raw C pointer maps to Kotlin CPointer. It is typed, so the compiler knows what it points at instead of leaving you with a bare address.
Memory You Must Manage
C does not free things for you. Allocate inside a memScoped block so Kotlin releases the native memory automatically when the block ends.
memScoped {
val buf = allocArray<ByteVar>(16)
}Strings Cross the Line
Use helpers to convert between Kotlin and C strings. A Kotlin String becomes a C buffer with cstr, and you convert back when reading results.
Opaque Types and Structs
C structs map to Kotlin classes with field access. An opaque type you never inspect is just a pointer you pass straight back to the library.
Keep It on Native Targets
cinterop bindings exist only for Kotlin/Native targets like iOS. Wrap the calls behind expect/actual so commonMain stays platform-free.
Wrap, Then Expose
Hide pointers and memScoped behind a friendly Kotlin API. Callers get clean types and never touch raw C details directly.
Quick Check
A quick check on managing C memory from Kotlin.
Recap: cinterop
You saw that a def file plus Gradle generates Kotlin bindings for a C library, pointers map to CPointer, and memScoped manages native memory. 🎉
Häufig gestellte Fragen
Ist die Lektion „cinterop mit C-Bibliotheken“ kostenlos?
Ja — der vollständige Text von „cinterop mit C-Bibliotheken“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „cinterop mit C-Bibliotheken“?
Binden Sie eine C-Abhängigkeit zur Verwendung im gemeinsam genutzten Code ein. Du übst Kotlin Multiplatform Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Kotlin Multiplatform Academy zu starten?
Keine Vorkenntnisse erforderlich. Kotlin Multiplatform Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „cinterop mit C-Bibliotheken“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?
Ja. Jede Kotlin Multiplatform Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Apple-Frameworks aus Kotlin verwenden
- Speicher und ARC in Kotlin/Native
- cinterop mit C-Bibliotheken
- Ein Native SDK sauber kapseln