使用 C 库进行互操作
绑定 C 依赖项,以便在共享代码中使用
使用 C 库进行互操作 是 CoddyKit 上的免费 Kotlin Multiplatform Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Kotlin Multiplatform Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Kotlin Multiplatform Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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. 🎉
常见问题解答
「使用 C 库进行互操作」课时是免费的吗?
是的 — 「使用 C 库进行互操作」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Multiplatform Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Multiplatform Academy 课程共包含 4 节课。
「使用 C 库进行互操作」这节课中我会学到什么?
绑定 C 依赖项,以便在共享代码中使用 你通过在浏览器中直接运行的动手代码来练习 Kotlin Multiplatform Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Kotlin Multiplatform Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Kotlin Multiplatform Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「使用 C 库进行互操作」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Kotlin Multiplatform Academy 课中编写并运行代码吗?
能。每节 Kotlin Multiplatform Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。