#Kotlin/Native
Kotlin/Native attempts to compile Kotlin source code directly to native binaries specific to the supported target platform. Kotlin/Native is primarily designed to allow compilation for platforms on which virtual machines are not desirable or possible, such as embedded devices or iOS.
Kotlin/Native provides an LLVM based backend for the Kotlin/Native compiler and native implementations of the Kotlin standard library. The Kotlin/Native compiler itself is known as Konan. LLVM is basically a compiler infrastructure that we can use to develop a front end for any programming language and a back end for any instruction set architecture.
It provides a portable, high-level assembly language optimized for various transformations that serve as a language-independent intermediate representation. Originally implemented for C and C++, today there are several languages with a compiler that supports LLVM, including Kotlin:

Kotlin/Native supports a number of platforms that we can conveniently select through the Gradle configuration:
- Linux (x86_64, arm32, arm64, MIPS, MIPS little-endian)
- Windows (mingw x86_64, x86)
- Android (arm32, arm64, x86, x86_64)
- iOS (arm32, arm64, simulator x86_64)
- macOS (x86_64)
- tvOS (arm64, x86_64)
- watchOS (arm32, arm64, x86)
- WebAssembly (wasm32)
Now, we should notice that in our Gradle configuration, there is a check for the host operating system. This is used to determine what native platform to target i.e. you need to be on macOS to build for that platform and so on.
#Interoperability
Kotlin/Native also supports two-way interoperability with native programming languages for different operating systems. The compiler creates:
- an executable for many platforms
- a static library or dynamic library with C headers for C/C++ projects
- an Apple framework for Swift and Objective-C projects
Kotlin/Native supports interoperability to use existing libraries directly from Kotlin/Native:
- static or dynamic C libraries
- C, Swift, and Objective-C frameworks
It is easy to include compiled Kotlin code in existing projects written in C, C++, Swift, Objective-C, and other languages. It is also easy to use existing native code, static or dynamic C libraries, Swift/Objective-C frameworks, graphical engines, and anything else directly from Kotlin/Native.
Finally, Multiplatform projects allow sharing common Kotlin code between multiple platforms, including Android, iOS, JVM, JavaScript, and native. Multiplatform libraries provide required APIs for common Kotlin code and help develop shared parts of a project in Kotlin in one place and share it with some or all target platforms.
#Getting Started
https://kotlinlang.org/docs/native-get-started.html

Open the build.gradle.kts
file, the build script that contains the project settings. To create Kotlin/Native applications, you need the Kotlin Multiplatform Gradle plugin installed. Ensure that you use the latest version of the plugin:
Build your project. It will produce a native executable under
build/bin/native/debugExecutable/<your_app_name>.kexe
#Example: Native Interop
This tutorial demonstrates how to use IntelliJ IDEA to create a command-line application. You'll learn how to create a simple HTTP client that can run natively on specified platforms using Kotlin/Native and the libcurl
library.
https://kotlinlang.org/docs/native-app-with-c-and-libcurl.html
The full code for this sample is here: https://github.com/Kotlin/kotlin-hands-on-intro-kotlin-native
- Create the project.

Update the build.gradle file.
Create a definition file.
When writing native applications, you often need access to certain functionalities that are not included in the Kotlin standard library, such as making HTTP requests, reading and writing from disk, and so on.
Kotlin/Native helps consume standard C libraries, opening up an entire ecosystem of functionality that exists for pretty much anything you may need. Kotlin/Native is already shipped with a set of prebuilt platform libraries, which provide some additional common functionality to the standard library. We'll link in a standard C library.
Create a directory named
src/nativeInterop/cinterop
.Create a file
libcurl.def
with the following contents.
This defined kotlin header files to be created from the C headers on our system.
- Add interoperrability to your builds.
Add this to your build.gradle file.
- Write the application code.
Update the source file Main.kt
with the following source.
If you build it, you should get a native executable, linked to the curl libraries.
You should be able to run it to see output!