Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time

Building a Hello World console app with NativeAOT

NativeAOT is an AOT-optimized .NET Core runtime. This document will guide you through compiling a .NET Core Console application with NativeAOT.

Please ensure that pre-requisites are installed.

Create .NET Core Console project

Open a new shell/command prompt window and run the following commands.

> dotnet new console -o HelloWorld
> cd HelloWorld

This will create a simple Hello World console app in Program.cs and associated project files.

Add NativeAOT to your project

To use NativeAOT with your project, you need to add a reference to the ILCompiler NuGet package that contains the NativeAOT ahead of time compiler and runtime. For the compiler to work, it first needs to be added to your project.

In your shell/command prompt navigate to the root directory of your project and run the command:

> dotnet new nugetconfig

This will add a nuget.config file to your application. Open the file and in the <packageSources> element under <clear/> add the following:

<add key="dotnet-experimental" value="" />
<add key="" value="" protocolVersion="3" />

Once you've added the package source, add a reference to the compiler by running the following command:

> dotnet add package Microsoft.DotNet.ILCompiler -v 7.0.0-*

Restore and Publish your app

Once the package has been successfully added it's time to compile and publish your app! In the shell/command prompt window, run the following command:

> dotnet publish -r <RID> -c <Configuration>

where <Configuration> is your project configuration (such as Debug or Release) and <RID> is the runtime identifier (one of win-x64, linux-x64, osx-x64). For example, if you want to publish a release configuration of your app for a 64-bit version of Windows the command would look like:

> dotnet publish -r win-x64 -c release

Once completed, you can find the native executable in the root folder of your project under /bin/<Configuration>/net6.0/<RID>/publish/. Navigate to /bin/<Configuration>/net6.0/<RID>/publish/ in your project folder and run the produced native executable.

Feel free to modify the sample application and experiment. However, keep in mind some functionality might not yet be supported in NativeAOT. Let us know on the Issues page.