Compiling stand-alone file in classic .NET vs Dotnet core

I realized that compiling a single-file program became much more complicated in Dotnet core compared to regular .NET.

For starters, here’s how you run a Python file: python foo.py

Here’s how you used to run a C# file in .NET 4:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe foo.cs
foo.exe

This is a little bit longer than with Python, but also fine. The only problem was to figure out the Framework version.

Dotnet core brings several problems:

  • You must specify all reference assemblies, even standard ones
  • The file produced is not directly executable, you must use dotnet foo.exe
  • Even that does not work until you manually create a file named foo.runtimeconfig.json. The compiler cannot help with that.
set DOTNET_CLI_TELEMETRY_OPTOUT=1
set LIB_PATH=C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.11
set LIBS="-r:%LIB_PATH%\System.Private.CoreLib.dll" "-r:%LIB_PATH%\System.Console.dll" "-r:%LIB_PATH%\System.Runtime.dll" "-r:%LIB_PATH%\System.Text.RegularExpressions.dll" "-r:%LIB_PATH%\System.Linq.dll"
dotnet "C:\Program Files\dotnet\sdk\2.0.0\Roslyn\csc.exe" -nologo %LIBS% foo.cs
dotnet foo.exe

foo.runtimeconfig.json:

{
  "runtimeOptions": {
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "6.0.0"
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *