0Pricing
Kotlin Multiplatform Academy · درس

التكامل البيني مع مكتبات C

اربط تبعية C لاستخدامها في التعليمات البرمجية المشتركة.

التكامل البيني مع مكتبات C درس مجاني في Kotlin Multiplatform Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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» مجاني؟

نعم — نص درس «التكامل البيني مع مكتبات C» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Kotlin Multiplatform Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Kotlin Multiplatform Academy 4 دروس في المجموع.

ماذا ستتعلم في «التكامل البيني مع مكتبات C»؟

اربط تبعية C لاستخدامها في التعليمات البرمجية المشتركة. تتمرن على Kotlin Multiplatform Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Kotlin Multiplatform Academy؟

لا تُشترط خبرة سابقة. Kotlin Multiplatform Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «التكامل البيني مع مكتبات C»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Kotlin Multiplatform Academy هذا؟

نعم. كل درس في Kotlin Multiplatform Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. استخدام أطر Apple من Kotlin
  2. الذاكرة وARC في Kotlin/Native
  3. التكامل البيني مع مكتبات C
  4. تغليف Native SDK بطريقة نظيفة
← العودة إلى Kotlin Multiplatform Academy