Get started with Kotlin/Native using the command-line compiler

Obtain the compiler

The Kotlin/Native compiler is available for macOS, Linux, and Windows. It is available as a command line tool and ships as part of the standard Kotlin distribution and can be downloaded from GitHub Releases. It supports different targets including iOS (arm32, arm64, simulator x86_64), Windows (mingw32 and x86_64), Linux (x86_64, arm64, MIPS), macOS (x86_64), Raspberry PI, SMT32, WASM. See the full list of targets here. While cross-platform compilation is possible, which means using one platform to compile for a different one, in this Kotlin case we'll be targeting the same platform we're compiling on.

While the output of the compiler does not have any dependencies or virtual machine requirements, the compiler itself requires Java 1.8 or higher runtime.

Install the compiler by unpacking its archive to a directory of your choice and adding the path to its /bin directory to the PATH environment variable.

Write "Hello Kotlin/Native" program

The application will print "Hello Kotlin/Native" on the standard output. In a working directory of choice, create a file named hello.kt and enter the following contents:

fun main() { println("Hello Kotlin/Native!") } 

Compile the code from the console

To compile the application use the downloaded compiler to execute the following command:

kotlinc-native hello.kt -o hello 

The value of -o option specifies the name of the output file, so this call should generate a hello.kexe (Linux and macOS) or hello.exe (Windows) binary file. For the full list of available compiler options, see the compiler options reference.

While compilation from the console seems to be easy and clear, it does not scale well for larger projects with hundreds of files and libraries. For real-world projects, it is recommended to use a build system and IDE.

Last modified: 08 September 2021

© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/native-command-line-compiler.html