0Pricing
Kotlin Multiplatform Academy · บทเรียน

การเชื่อมต่อกับไลบรารี C ด้วย cinterop

ผูกการพึ่งพา C เพื่อใช้งานในโค้ดร่วม

การเชื่อมต่อกับไลบรารี C ด้วย cinterop เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 = 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. 🎉

คำถามที่พบบ่อย

บทเรียน “การเชื่อมต่อกับไลบรารี C ด้วย cinterop” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเชื่อมต่อกับไลบรารี C ด้วย cinterop” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเชื่อมต่อกับไลบรารี C ด้วย cinterop”

ผูกการพึ่งพา C เพื่อใช้งานในโค้ดร่วม คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การเชื่อมต่อกับไลบรารี C ด้วย cinterop” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม

ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ใช้เฟรมเวิร์กของ Apple จาก Kotlin
  2. หน่วยความจำและ ARC ใน Kotlin/Native
  3. การเชื่อมต่อกับไลบรารี C ด้วย cinterop
  4. ห่อหุ้ม SDK แบบเนทีฟอย่างเป็นระเบียบ
← กลับไปที่ Kotlin Multiplatform Academy