1
1
[ // ] : # ( title: Mapping primitive data types from C – tutorial )
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
- In this tutorial, you will learn what C data types are visible in Kotlin/Native and vice versa. You will:
13
- - See what [ Data types are in C language] ( #types-in-c-language ) .
14
- - Create a [ tiny C Library] ( #example-c-library ) that uses those types in exports.
15
- - [ Inspect generated Kotlin APIs from a C library] ( #inspect-generated-kotlin-apis-for-a-c-library ) .
16
- - Find how [ Primitive types in Kotlin] ( #primitive-types-in-kotlin ) are mapped to C.
17
-
18
- ## Types in C language
19
-
20
- What types are there in the C language? Let's take the
21
- [ C data types] ( https://en.wikipedia.org/wiki/C_data_types ) article from Wikipedia as a basis.
22
- There are following types in the C programming language:
23
- - basic types ` char, int, float, double ` with modifiers ` signed, unsigned, short, long `
24
- - structures, unions, arrays
25
- - pointers
26
- - function pointers
27
-
28
- There are also more specific types:
29
- - boolean type (from [ C99] ( https://en.wikipedia.org/wiki/C99 ) )
30
- - ` size_t ` and ` ptrdiff_t ` (also ` ssize_t ` )
31
- - fixed width integer types, such as ` int32_t ` or ` uint64_t ` (from [ C99] ( https://en.wikipedia.org/wiki/C99 ) )
32
-
33
- There are also the following type qualifiers in the C language: ` const ` , ` volatile ` , ` restrict ` , ` atomic ` .
34
-
35
- The best way to see what C data types are visible in Kotlin is to try it.
3
+ * goal
4
+ * what C data types are visible | Kotlin/Native <- & -> vice versa
5
+ * see what [ Data types | C language] ( #types-in-c-language )
6
+ * create a [ tiny C Library] ( #example-c-library ) / uses those types | exports
7
+ * [ inspect generated Kotlin APIs -- from a -- C library] ( #inspect-generated-kotlin-apis-for-a-c-library )
8
+ * [ primitive types in Kotlin -- are mapped to -- C] ( #primitive-types-in-kotlin )
9
+
10
+ ## Types | C language
11
+
12
+ * see [ C data types article] ( https://en.wikipedia.org/wiki/C_data_types )
13
+ - basic types ` char, int, float, double ` / modifiers ` signed, unsigned, short, long `
14
+ - structures, unions, arrays
15
+ - pointers
16
+ - function pointers
17
+ - other specific types
18
+ - boolean type (from [ C99] ( https://en.wikipedia.org/wiki/C99 ) )
19
+ - ` size_t ` and ` ptrdiff_t ` (also ` ssize_t ` )
20
+ - fixed width integer types, such as ` int32_t ` or ` uint64_t ` (from [ C99] ( https://en.wikipedia.org/wiki/C99 ) )
21
+
22
+ * type qualifiers
23
+ * ` const ` ,
24
+ * ` volatile ` ,
25
+ * ` restrict ` ,
26
+ * ` atomic `
36
27
37
28
## Example C library
38
29
39
- Create a ` lib.h ` file to see how C functions are mapped into Kotlin:
30
+ * see ` lib.h `
31
+ * goal
32
+ * how C functions -- are mapped into -- Kotlin
40
33
41
- ``` c
42
- #ifndef LIB2_H_INCLUDED
43
- #define LIB2_H_INCLUDED
44
-
45
- void ints(char c, short d, int e, long f);
46
- void uints(unsigned char c, unsigned short d, unsigned int e, unsigned long f);
47
- void doubles(float a, double b);
48
-
49
- #endif
50
- ```
51
-
52
- The file is missing the ` extern "C" ` block, which is not needed for this example, but may be
53
- necessary if you use C++ and overloaded functions. The
54
- [ C++ compatibility thread] ( https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c ) on Stackoverflow
55
- contains more details on this.
56
-
57
- For every set of ` .h ` files,
58
- you will be using the [ ` cinterop ` tool] ( native-c-interop.md )
59
- from Kotlin/Native to generate a Kotlin/Native library,
60
- or ` .klib ` . The generated library will bridge calls from Kotlin/Native to C. It includes
61
- respective Kotlin declarations for the definitions form the ` .h ` files.
62
- It is only necessary to have a ` .h ` file to run the ` cinterop ` tool. And you do not need to create a
63
- ` lib.c ` file, unless you want to compile and run the example.
64
- More details on this are covered in the [ C interop] ( native-c-interop.md ) page. It is enough for
65
- the tutorial to create the ` lib.def ` file with the following content:
34
+ * [ ` cinterop ` tool] ( native-c-interop.md )
35
+ * from ` .h ` -- you can generate a -- Kotlin/Native library (== ` .klib ` ) /
36
+ * calls from Kotlin/Native -- are bridged to -- C
37
+ * TODO:
38
+ * It includes respective Kotlin declarations for the definitions form the ` .h ` files.
39
+ It is only necessary to have a ` .h ` file to run the ` cinterop ` tool.
40
+ And you do not need to create a ` lib.c ` file, unless you want to compile and run the example.
41
+ More details on this are covered in the [ C interop] ( native-c-interop.md ) page.
42
+ It is enough for the tutorial to create the ` lib.def ` file with the following content:
66
43
67
44
``` c
68
45
headers = lib.h
69
46
```
70
47
71
48
You may include all declarations directly into the ` .def ` file after a ` --- ` separator.
72
49
It can be helpful to include macros or other C defines into the code generated by the ` cinterop ` tool.
73
- Method bodies are compiled and fully included into the binary too. Use
74
- that feature to have a runnable example without a need for a C compiler.
75
- To implement that, you need to add implementations to the C functions from the ` lib.h ` file,
76
- and place these functions into a ` .def ` file.
50
+ Method bodies are compiled and fully included into the binary too.
51
+ Use that feature to have a runnable example without a need for a C compiler.
52
+ To implement that, you need to add implementations to the C functions from the ` lib.h ` file, and place these functions into a ` .def ` file.
77
53
You will have the following ` interop.def ` result:
78
54
79
55
``` c
@@ -89,6 +65,8 @@ The `interop.def` file is enough to compile and run the application or open it i
89
65
Now it is time to create project files, open the project in
90
66
[IntelliJ IDEA](https://jetbrains.com/idea) and run it.
91
67
68
+ * TODO:
69
+
92
70
## Inspect generated Kotlin APIs for a C library
93
71
94
72
While it is possible to use the command line, either directly or
@@ -236,27 +214,24 @@ as it is usually an 8-bit signed value.
236
214
237
215
## Fix the code
238
216
239
- You've seen all definitions and it is the time to fix the code.
240
- Run the ` runDebugExecutableNative ` Gradle task [ in IDE] ( native-get-started.md )
241
- or use the following command to run the code:
242
-
243
- ``` bash
244
- ./gradlew runDebugExecutableNative
245
- ```
217
+ * run
246
218
247
- The final code in the ` hello.kt ` file may look like that:
219
+ ``` bash
220
+ ./gradlew runDebugExecutableNative
221
+ ```
222
+ * final code | ` hello.kt`
248
223
249
- ``` kotlin
250
- import interop.*
251
-
252
- fun main () {
253
- println (" Hello Kotlin/Native!" )
254
-
255
- ints(1 , 2 , 3 , 4 )
256
- uints(5 , 6 , 7 , 8 )
257
- doubles(9.0f , 10.0 )
258
- }
259
- ```
224
+ ` ` ` kotlin
225
+ import interop.*
226
+
227
+ fun main () {
228
+ println(" Hello Kotlin/Native!" )
229
+
230
+ ints(1, 2, 3, 4)
231
+ uints(5, 6, 7, 8)
232
+ doubles(9.0f, 10.0)
233
+ }
234
+ ` ` `
260
235
261
236
# # Next steps
262
237
0 commit comments