I followed the instructions for the .NET Core 5.0 “Hello World” demo code available here[1]. What I have added in this version of the tutorial are the command line arguments for a single executable in Linux.
In brief, the Linux version compiles into two files and you only need one, the hello executable, to run. Windows dotnet compiles with other garbage still.
I think I would like to see this as the compiler option for a single executable on all platforms:
rem Example of prefered code for compiling to a single executable with all libraries and runtime together: dotnet --compile rem This is not how it works though! rem You could add debug... dotnet --compile --debug rem or... dotnet publish <profile> dotnet publish release rem or... dotnet -p=release rem = myfk.exe
I’m a bit gaslit having found there is WPF and not UWP in Core 5.0.
Steps:
1. Install dotnet
On Fedora 34 just run this:
sudo dnf install dotnet
For Windows there is an installer on the original tutorial[2].
2. Test the Install
dotnet --info
3. Create a New Console App
Note that the Program.cs will be created automatically for you including the code.
dotnet new console -o hello cd hello
The project file looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
The code in Program.cs looks like this:
using System;
namespace hello
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
4. Compile the Project
LINUX
dotnet publish -c Release -r linux-x64 -p:PublishSingleFile=true --self-contained true cd bin/Release/net5.0/linux-x64/publish
The compiler output (linux): (note: it still runs without the .pdb)
60M hello 9.4K hello.pdb
Run the program (Linux) – I tested this on a VM with Fedora 34 and no ‘dotnet’ installed. Yes the output is a little big at 60M but writing a larger program would probably not add much overhead to that.
./hello
Result:
Hello World!
Ya!
WINDOWS
Then I’ve run the compiler on Windows:
dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true --self-contained true
The compiled files (windows): (multiple files? :|) Maybe it can go in a self extracting zip?
.\hello\bin\release\net5.0\win-x64\publish
748,936 clrcompression.dll
1,274,248 clrjit.dll
5,140,360 coreclr.dll
53,113,128 hello.exe
9,584 hello.pdb
1,056,632 mscordaccore.dll
7 File(s) 61,342,888 bytes
References
[1, .NET Tutorial | Hello World in 10 minutes, https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/intro, accessed 9th Jun 2021]
[2, .NET Tutorial | Hello World in 10 minutes, Install, https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial/install, accessed 9th Jun 2021]
