diff --git a/docs/topics/command-line.md b/docs/topics/command-line.md index 30cb3fb1433..a068cceebe1 100644 --- a/docs/topics/command-line.md +++ b/docs/topics/command-line.md @@ -110,7 +110,7 @@ kotlin -classpath hello.jar HelloKt `HelloKt` is the main class name that the Kotlin compiler generates for the file named `hello.kt`. -> To compile a Kotlin/Native library, use the [Kotlin/Native compiler](native-libraries.md#kotlin-compiler-specifics). +> To compile a Kotlin/Native library, use the [Kotlin/Native compiler](native-libraries.md#using-kotlin-native-compiler). > {style="note"} diff --git a/docs/topics/native/native-libraries.md b/docs/topics/native/native-libraries.md index b747e3c04b7..f16a676ec27 100644 --- a/docs/topics/native/native-libraries.md +++ b/docs/topics/native/native-libraries.md @@ -1,146 +1,154 @@ [//]: # (title: Kotlin/Native libraries) -## Kotlin compiler specifics +## Library compilation -To produce a library with the Kotlin/Native compiler use the `-produce library` or `-p library` flag. For example: +You can use your project's build file or the Kotlin/Native compiler to produce a `*.klib` artifact for your library. -```bash -$ kotlinc-native foo.kt -p library -o bar -``` - -This command will produce a `bar.klib` with the compiled contents of `foo.kt`. +### Using Gradle build file -To link to a library use the `-library ` or `-l ` flag. For example: - -```bash -$ kotlinc-native qux.kt -l bar -``` +You can compile a `*.klib` library artifact by specifying a [Kotlin/Native target](native-target-support.md) +in your Gradle build file: -This command will produce a `program.kexe` out of `qux.kt` and `bar.klib` +1. In your `build.gradle(.kts)` file, declare at least one Kotlin/Native target. For example: -## cinterop tool specifics + ```kotlin + // build.gradle.kts + plugins { + kotlin("multiplatform") version "%kotlinVersion%" + } + + kotlin { + macosArm64() // on macOS + // linuxArm64() // on Linux + // mingwX64() // on Windows + } + ``` -The **cinterop** tool produces `.klib` wrappers for native libraries as its main output. -For example, using the simple `libgit2.def` native library definition file provided in your Kotlin/Native distribution +2. Run the `Klib` task. For example: -```bash -$ cinterop -def samples/gitchurn/src/nativeInterop/cinterop/libgit2.def -compiler-option -I/usr/local/include -o libgit2 -``` + ```bash + ./gradlew macosArm64Klib + ``` -we will obtain `libgit2.klib`. +Gradle automatically compiles source files for that target and produces the `.klib` artifact in the project's `build/libs` +directory. -See more details in [C Interop](native-c-interop.md). +### Using Kotlin/Native compiler -## klib utility +To produce a library with the Kotlin/Native compiler: -The **klib** library management utility allows you to inspect and install the libraries. +1. [Download and install the Kotlin/Native compiler.](native-get-started.md#download-and-install-the-compiler) +2. To compile a Kotlin/Native source file into a library, use the `-produce library` or `-p library` option: -The following commands are available: + ```bash + kotlinc-native foo.kt -p library -o bar + ``` -* `content` – list library contents: + This command compiles the contents of the `foo.kt` file into a library with the name `bar`, producing a `bar.klib` artifact. - ```bash - $ klib contents - ``` +3. To link another file to a library, use the `-library ` or `-l ` option. For example: -* `info` – inspect the bookkeeping details of the library + ```bash + kotlinc-native qux.kt -l bar + ``` + + This command compiles the contents of the `qux.kt` source file and the `bar.klib` library and produces the `program.kexe` + final executable binary. - ```bash - $ klib info - ``` +## klib utility -* `install` – install the library to the default location use +The **klib** library management utility allows you to inspect libraries using the following syntax: - ```bash - $ klib install - ``` +```bash +klib [