Skip to content

Commit 0304395

Browse files
author
alfredo-toledano
committed
doc(docs.topics.native.mappingPrimitiveDataTypesFromC): add notes
1 parent 2af4135 commit 0304395

File tree

2 files changed

+67
-82
lines changed

2 files changed

+67
-82
lines changed

docs/topics/native/lib.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef LIB2_H_INCLUDED
2+
#define LIB2_H_INCLUDED
3+
4+
void ints(char c, short d, int e, long f);
5+
void uints(unsigned char c, unsigned short d, unsigned int e, unsigned long f);
6+
void doubles(float a, double b);
7+
8+
#endif
9+
10+
// if you use C++ and overloaded functions -> `extern "C"` block is missing -- https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c --

docs/topics/native/mapping-primitive-data-types-from-c.md

Lines changed: 57 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,55 @@
11
[//]: # (title: Mapping primitive data types from C – tutorial)
22

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`
3627

3728
## Example C library
3829

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
4033

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:
6643

6744
```c
6845
headers = lib.h
6946
```
7047

7148
You may include all declarations directly into the `.def` file after a `---` separator.
7249
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.
7753
You will have the following `interop.def` result:
7854

7955
```c
@@ -89,6 +65,8 @@ The `interop.def` file is enough to compile and run the application or open it i
8965
Now it is time to create project files, open the project in
9066
[IntelliJ IDEA](https://jetbrains.com/idea) and run it.
9167
68+
* TODO:
69+
9270
## Inspect generated Kotlin APIs for a C library
9371
9472
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.
236214

237215
## Fix the code
238216

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
246218

247-
The final code in the `hello.kt` file may look like that:
219+
```bash
220+
./gradlew runDebugExecutableNative
221+
```
222+
* final code | `hello.kt`
248223

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+
```
260235

261236
## Next steps
262237

0 commit comments

Comments
 (0)