Intro
By the end of this guide, you will have all what is needed to convert a hello.kt
file containing this code:
fun main() {
println("Hello, World!")
}
to hello.exe
, a CLI (command-line interface) executable, which you can run and have "Hello, World!" printed in the terminal.
Note
If you want to build with Gradle, this is not the guide you're looking for. This guide shows you how to build by calling kotlinc-native
from your terminal.
Prerequisites
- Java JDK or JRE version 8 or higher to be installed on your system
- you know what "Kotlin/Native" is and does (https://kotlinlang.org/docs/reference/native-overview.html)
- you have read "A Basic Kotlin/Native Application" (https://kotlinlang.org/docs/tutorials/native/basic-kotlin-native-app.html) but for whatever reason you don't want to build with Gradle and an IDE
Let's Go Kotlin/Native
- download "the Kotlin/Native compiler" from the GitHub repository (https://github.com/JetBrains/kotlin-native/releases)
- look for the "Latest release" and follow the link to "Download the binary distribution from..."
- scroll to the bottom of the page and locate the "Assets" section
- click on the
kotlin-native-windows-...zip
link - save the
.zip
file on your computer
- place "the Kotlin/Native compiler" in the right location
- in your "home directory" (which you can locate if you run
echo %HOMEPATH%
in a terminal (usually "C:\Users\YourName")) create a directory.konan
- from the contents of
kotlin-native-windows-...zip
take the folder named something likekotlin-native-windows-...
and copy it in the.konan
directory - inside the
kotlin-native-windows-...
directory, there is abin
directory, add the path to thisbin
directory to your "Environment Variables..." > "System variables" > "Path" so thatkotlinc-native
can be called from any location - if you need help with what the "Path" environment variable is, here is a nice guide (https://docs.telerik.com/teststudio/features/test-runners/add-path-environment-variables)
- in your "home directory" (which you can locate if you run
- test
- open a terminal, run
cd /
then runkotlinc-native -version
, if all went well so far the version of the compiler should show and you are (almost) ready to compile
- open a terminal, run
First compilation
- create a directory
mkdir C:\apps\kotlin
- create a file
hello.kt
insideC:\apps\kotlin
- with your text editor, edit
hello.kt
and put this code in it:
fun main() {
println("Hello, World!")
}
- save
hello.kt
- in a terminal
cd C:\apps\kotlin
- run
kotlinc-native hello.kt -o hello
- because this is the first run of the compiler, the "native dependencies" will be downloaded, extracted and saved to that
.konan
folder from your "home directory" (this is a one-time operation which might take a few minutes)
- because this is the first run of the compiler, the "native dependencies" will be downloaded, extracted and saved to that
- when the compiler is done, you'll find a
hello.exe
in the same folder with the source filehello.kt
- run
hello.exe
in the terminal to see the "Hello, World!" text
Summary
This guide showed you how to install and then create a "Hello, World" CLI executable with the Kotlin/Native compiler.
Now that the toolchain is installed on your system, go ahead and create your "real" executable.
The syntax to use the compiler is:
kotlinc-native <file-name>.kt -o <file-name>
You can reach me on Twitter at: @alexbaban if you have any questions.
Happy "Kotlin/Native" coding!
Top comments (0)