How to create a .NET Core F# application and generate executables for multiple operating systems
Posted on August 13, 2018 by Paul
In this article I will show you how to create a simple F# console application that runs on .NET Core and how to generate executables for various operating systems.
Start by downloading the .NET Core SDK for your operating system from https://www.microsoft.com/net/download. Run the installer and accept the default settings.
Check if everything works, by opening a Terminal (or a Command Prompt on Windows) and execute the next command:
This is what I see on a macOS machine:
Now, let’s create a simple F# console application named HelloWorld:
If this is the first time you are creating an app with the dotnet command, the command will print a verbose Welcome message, I suggest to read it carefully. Next time, when you will create a new app with the dotnet command the above message won’t be shown.
The F# source code is in Program.fs:
In order to build and run the code, go to the application folder and execute the next command:
This is what I see on my machine:
dotnet run does a lot of work in background and is kinda slow. If you just need to run the application, without modifying the code, there is a faster alternative:
just keep in mind that the above will skip the build phase. If you modify the code, use dotnet run.
An even faster way to start and run the app is to use the full path to the generated dll:
As a side note, on my test machine, dotnet run takes about 2.8 seconds, dotnet run
--no-build about 0.8 seconds and dotnet full_path_dll about 0.1 seconds. This is why I’ve presented you the above alternatives.
How about generating an executable from our F# code ? If you need an executable for macOS, Windows or Linux use one of the next commands:
If you target Raspberry Pi, use:
The above will create a portable folder named publish that contains everything you need in order to run the executable.
If you want to see the list of all available targets check https://docs.microsoft.com/en-us/dotnet/core/rid-catalog#using-rids
For example, if I want to create a Windows executable from my macOS machine I will use:
The resulting publish folder is in the application folder in bin/release/netcoreapp2.1/win-x64. If I want to send the executable to someone that uses Windows, I will simply zip the publish folder and send it to my client.