|
1 | 1 | [//]: # (title: Interoperability with C)
|
2 | 2 |
|
3 |
| -> The C libraries import is [Experimental](components-stability.md#stability-levels-explained). |
4 |
| -> All Kotlin declarations generated by the `cinterop` tool from C libraries |
5 |
| -> should have the `@ExperimentalForeignApi` annotation. |
6 |
| -> |
7 |
| -> Native platform libraries shipped with Kotlin/Native (like Foundation, UIKit, and POSIX), |
8 |
| -> require opt-in only for some APIs. In such cases, you get an IDE warning. |
9 |
| -> |
10 |
| -{type="warning"} |
11 |
| - |
12 |
| -Kotlin/Native follows the general tradition of Kotlin to provide excellent |
13 |
| -existing platform software interoperability. In the case of a native platform, |
14 |
| -the most important interoperability target is a C library. So Kotlin/Native |
15 |
| -comes with a `cinterop` tool, which can be used to quickly generate |
16 |
| -everything needed to interact with an external library. |
17 |
| - |
18 |
| -The following workflow is expected when interacting with the native library: |
19 |
| -1. Create a `.def` file describing what to include into bindings. |
20 |
| -2. Use the `cinterop` tool to produce Kotlin bindings. |
21 |
| -3. Run the Kotlin/Native compiler on an application to produce the final executable. |
22 |
| - |
23 |
| -The interoperability tool analyses C headers and produces a "natural" mapping of |
24 |
| -the types, functions, and constants into the Kotlin world. The generated stubs can be |
25 |
| -imported into an IDE for the purpose of code completion and navigation. |
26 |
| - |
27 |
| -Interoperability with Swift/Objective-C is provided too and covered in [Objective-C interop](native-objc-interop.md). |
| 3 | +* Kotlin/Native's software interoperability |
| 4 | + * -- as good as -- Kotlin's software interoperability |
| 5 | + * | ALL native platform, |
| 6 | + * 👀C library == MOST important interoperability target👀 |
| 7 | + * -> Kotlin/Native -- comes with a -- `cinterop` tool |
| 8 | + * Swift/Objective-C |
| 9 | + * see [Objective-C interop](native-objc-interop.md) |
| 10 | + |
| 11 | +* `cinterop` tool |
| 12 | + * allows |
| 13 | + * generating everything needed -- to interact with an -- external library |
| 14 | + * analyses C headers |
| 15 | + * produces a "natural" mapping | Kotlin, of |
| 16 | + * types, |
| 17 | + * functions, |
| 18 | + * constants |
| 19 | + |
| 20 | +* workflow | interacting with the native library |
| 21 | + 1. Create a `.def` / describe what to include | bindings |
| 22 | + 2. Use `cinterop` tool -- to generate -- Kotlin bindings |
| 23 | + 3. Run the Kotlin/Native compiler | application -- to produce the -- final executable |
28 | 24 |
|
29 | 25 | ## Platform libraries
|
30 | 26 |
|
31 |
| -Note that in many cases there's no need to use custom interoperability library creation mechanisms described below, |
32 |
| -as for APIs available on the platform standardized bindings called [platform libraries](native-platform-libs.md) |
33 |
| -could be used. For example, POSIX on Linux/macOS platforms, Win32 on Windows platform, or Apple frameworks |
34 |
| -on macOS/iOS are available this way. |
| 27 | +* | MANY cases, NOT need to use custom interoperability library creation mechanisms |
| 28 | + * Reason: 🧠you can use [platform libraries](native-platform-libs.md) 🧠 |
| 29 | +* _Example:_ POSIX | Linux/macOS platforms, Win32 | Windows platform, Apple frameworks |
| 30 | +| macOS/iOS |
35 | 31 |
|
36 | 32 | ## Simple example
|
37 | 33 |
|
38 |
| -Install libgit2 and prepare stubs for the git library: |
| 34 | +* [install libgit2](https://github.com/libgit2/libgit2?tab=readme-ov-file#installation) |
| 35 | + * TODO: |
| 36 | +* prepare stubs for the git library |
39 | 37 |
|
40 |
| -```bash |
41 |
| -cd samples/gitchurn |
42 |
| -../../dist/bin/cinterop -def src/nativeInterop/cinterop/libgit2.def \ |
43 |
| - -compiler-option -I/usr/local/include -o libgit2 |
44 |
| -``` |
| 38 | + ```bash |
| 39 | + cd samples/gitchurn |
| 40 | + ../../dist/bin/cinterop -def src/nativeInterop/cinterop/libgit2.def \ |
| 41 | + -compiler-option -I/usr/local/include -o libgit2 |
| 42 | + ``` |
45 | 43 |
|
46 |
| -Compile the client: |
| 44 | +* compile the client |
47 | 45 |
|
48 |
| -```bash |
49 |
| -../../dist/bin/kotlinc src/gitChurnMain/kotlin \ |
50 |
| - -library libgit2 -o GitChurn |
51 |
| -``` |
| 46 | + ```bash |
| 47 | + ../../dist/bin/kotlinc src/gitChurnMain/kotlin \ |
| 48 | + -library libgit2 -o GitChurn |
| 49 | + ``` |
52 | 50 |
|
53 |
| -Run the client: |
| 51 | +* run the client |
54 | 52 |
|
55 |
| -```bash |
56 |
| -./GitChurn.kexe ../.. |
57 |
| -``` |
| 53 | + ```bash |
| 54 | + ./GitChurn.kexe ../.. |
| 55 | + ``` |
58 | 56 |
|
59 | 57 | ## Create bindings for a new library
|
60 | 58 |
|
|
0 commit comments