W3cubDocs

/Kotlin

Hello Kotlin/Native using Command Line Compiler

Last Updated 15 January 2020
A look at how to compile Kotlin/Native applications using the command line compiler

Obtaining 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, STM32, WASM. For the full list of targets please see the Kotlin/Native overview.

While cross-platform compilation is possible, which means using one platform to compile for a different one, in this 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.

Creating Hello Kotlin/Native

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!")
}

Compiling 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.

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