0Pricing
Kotlin Multiplatform Academy · Lekcja

cinterop z bibliotekami C

Dowiąż zależność C, aby używać jej we współdzielonym kodzie.

cinterop z bibliotekami C to bezpłatna lekcja Kotlin Multiplatform Academy na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Kotlin Multiplatform Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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 = mylib

Wire 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. 🎉

Często zadawane pytania

Czy lekcja „cinterop z bibliotekami C” jest bezpłatna?

Tak — pełny tekst „cinterop z bibliotekami C” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Kotlin Multiplatform Academy, przejdź na CoddyKit PRO. Kurs Kotlin Multiplatform Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „cinterop z bibliotekami C”?

Dowiąż zależność C, aby używać jej we współdzielonym kodzie. Ćwiczysz Kotlin Multiplatform Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Kotlin Multiplatform Academy?

Nie wymagamy żadnego doświadczenia. Kotlin Multiplatform Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „cinterop z bibliotekami C”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Kotlin Multiplatform Academy?

Tak. Każda lekcja Kotlin Multiplatform Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Korzystanie z frameworków Apple w Kotlinie
  2. Pamięć i ARC w Kotlin/Native
  3. cinterop z bibliotekami C
  4. Czyste opakowanie Native SDK
← Powrót do Kotlin Multiplatform Academy