Skip to content

Instantly share code, notes, and snippets.

@0xced
Last active November 16, 2022 15:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0xced/c814bef25d55de97e09782d918117a89 to your computer and use it in GitHub Desktop.
Save 0xced/c814bef25d55de97e09782d918117a89 to your computer and use it in GitHub Desktop.
Building a .NET Core single exe for Linux on macOS or Windows

Building a .NET Core single exe for Linux on macOS or Windows

Run the .NET Core SDK docker image and share your working directory containing your .csproj file at /home in the container.

On Linux/macOS:

docker run --interactive --tty --rm --volume "$(pwd):/home" mcr.microsoft.com/dotnet/core/sdk:2.2 /bin/bash

On Windows:

set pwd=/%cd:\=/%
docker run --interactive --tty --rm --volume "%pwd::=%:/home" mcr.microsoft.com/dotnet/core/sdk:2.2 /bin/bash

Build the single exe with warp

cd /home
dotnet tool install --global dotnet-warp
/root/.dotnet/tools/dotnet-warp -l aggressive

The binary is produced in the working directory.

Build the single exe with CoreRT

Note that CoreRT is an experimental .NET Core runtime optimized for AOT so it's not really surprising that it doesn't work.

Installing clang-3.9 is required else we get this error:

/root/.nuget/packages/microsoft.dotnet.ilcompiler/1.0.0-alpha-27910-01/build/Microsoft.NETCore.Native.Unix.props(109,5): error : Platform linker ('clang-3.9') not found. Try installing clang-3.9 or the appropriate package for your platform to resolve the problem.

apt update
apt --yes install clang-3.9
rm -r bin obj
dotnet add package --source https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json Microsoft.DotNet.ILCompiler -v 1.0.0-alpha-*
dotnet publish --source https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json --runtime linux-x64 --configuration Release

On a real console app with a few dependencies (CommandLineParser, CsvHelper, FluentEmail, EntityFrameworkCore + Npgsql, Serilog) the compilation failed after 67 minutes with the following error:

Process is terminating due to StackOverflowException. /root/.nuget/packages/microsoft.dotnet.ilcompiler/1.0.0-alpha-28007-01/build/Microsoft.NETCore.Native.targets(249,5): error MSB3073: The command ""/root/.nuget/packages/runtime.linux-x64.microsoft.dotnet.ilcompiler/1.0.0-alpha-28007-01/tools/ilc" @"obj/Release/netcoreapp2.2/linux-x64/native/my-console-app.ilc.rsp"" exited with code 134.

See CoreRT issues #6372 Process is terminating due to StackOverflowException and #363 Detect infinite generic expansion on GitHub.

Build the single exe with .NET Core 3.0

docker run --interactive --tty --rm --volume "$(pwd):/home" mcr.microsoft.com/dotnet/core/sdk:3.0 /bin/bash

In the container:

cd /home
dotnet publish --runtime linux-x64 --configuration Release /p:TargetFramework=netcoreapp3.0 /p:PublishSingleFile=true /p:PublishTrimmed=true

The binary is produced in bin/Release/netcoreapp3.0/linux-x64/publish.

See also Announcing .NET Core 3.0 Preview 6, The PublishTrimmed Flag With IL Linker and https://aka.ms/dotnet-illink

Notes

Might be worth investigating Trim and Link as described in Reducing the size of self-contained .NET Core applications

Other interesting links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment