Obtaining the compiler

Kotlin/Native is available for macOS, Linux and Windows. Depending on the operating system we’re working on, we’ll need to download the correct compiler. While cross-platform compilation is possible (i.e. using one platform to compile for another), in this first tutorial we’re going to compile for the same operating system we’re running on. In our case this will be Windows 10.

We can obtain the latest version of the compiler from the:

I downloaded version: kotlin-native-windows-1.3.11 and added to path (environmental var) :

C:\Program Files\kotlin-native-windows-1.3.11\bin

Once downloaded, we can uncompress it in any folder, e.g. ~/kotlin-native. For convenience it’s useful to add the bin folder to the system path so that we can invoke the compiler from any location. (If we uncompressed to ~/kotlin-native, this would be ~/kotlin-native/bin)

While the output by the compiler does not have any dependencies, the compiler itself does require Java 8, which should be on the system.

If you get the error (it means your Java version is 32 bit, 32-bit Windows is not supported)

Error occurred during initialization of VM
Could not reserve enough space for 3145728KB object heap

Add the _JAVA_OPTIONS with param to the path (environmental var) :

Or change the Java version to jre-8u191-windows-x64, do that!

Ok, let’s code before we read more…

Create a Main.kt file and added the following code:

fun main(args: Array<String>) {
println("Kotlin/Native app")
println("Enter some text")
val st = readLine()!!
println("Input : $st")

So, first up is compiling the file, I am compiling the Main.kt from the dir where the files is, the reason for that is so that the executable file lands in the same folder

The Kotlin compiler uses a technology known as LLVM to target multiple platforms. LLVM requires as input what’s known as intermediate representation or IR. This IR is represented by a bitcode file, which is a bitstream file format.
We now need to compile our application, which is done using the compiler downloaded in the first step. If we have the bin folder correctly added to the path, we should be able to invoke the compiler using

C:\Users\espen\VirtualKotlin\Basic>kotlinc-native Main.kt

The first time the compiler runs, it downloads a list of necessary requirements, thus first run does take longer. If everything runs correctly, the output should be program.exe

So let’s have a look in the folder and run the program.exe

If you want a different name for the .exe simply use -o for output name

C:\Users\espen\VirtualKotlin\Basic>kotlinc-native -o app.exe Main.kt

And there you have all three files and Kotlin-nativ is running.

The extension cannot be set and is determined based on the target platform, but we can of course rename the executable to anything we like, using the usual system commands to rename files.

It’s important to understand that this is now a native application, and no runtime or virtual machine is required.

Whenever you do changes to a kt file, of course compile it again.

Compile .kt files in a folder

Lets say you have a Main.kt and a Player.kt in folder Test:


Then you need to compile the source

path-to-file\kotlinc src

Then you will get a compiled version of program.exe

Kotlin/Native for Native

Kotlin/Native is a technology for compiling Kotlin code to native binaries, which can run without a virtual machine. It is an LLVM based backend for the Kotlin compiler and native implementation of the Kotlin standard library.
Why Kotlin/Native?

Kotlin/Native is primarily designed to allow compilation for platforms where virtual machines are not desirable or possible, for example, embedded devices or iOS. It solves the situations when a developer needs to produce a self-contained program that does not require an additional runtime or virtual machine.
Target Platforms

Kotlin/Native supports the following platforms:

iOS (arm32, arm64, emulator x86_64)
MacOS (x86_64)
Android (arm32, arm64)
Windows (mingw x86_64)
Linux (x86_64, arm32, MIPS, MIPS little endian)
WebAssembly (wasm32)


Kotlin/Native supports two-way interoperability with the Native world. On the one hand, 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

On the other hand, 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 a compiled Kotlin code into 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.

Kotlin/Native libraries help to share Kotlin code between projects. POSIX, gzip, OpenGL, Metal, Foundation, and many other popular libraries and Apple frameworks are pre-imported and included as Kotlin/Native libraries into the compiler package.
Sharing Code between Platforms

Multiplatform projects are supported between different Kotlin and Kotlin/Native targets. This is the way to share common Kotlin code between many platforms, including Android, iOS, server-side, JVM, client-side, JavaScript, CSS, and native.

Multiplatform libraries provide the necessary APIs for the common Kotlin code and help to develop shared parts of a project in Kotlin code once and share it with all of the target platforms.

Next up is Kotlin basics