NativeSdk 0.5.1
See the version list below for details.
<Sdk Name="NativeSdk" Version="0.5.1" />
#:sdk NativeSdk@0.5.1
NativeSdk
A MSBuild SDK package for building Native AOT applications and libraries with C#.
Requirements
- .NET 8.0 or later
Installation
Use NativeSdk as your project SDK:
<Project Sdk="NativeSdk/0.5.1">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
This SDK automatically inherits from Microsoft.NET.Sdk, so you don't need to specify it separately.
Features
This SDK automatically configures your project with:
Common Settings (All Project Types)
| Setting | Default Value | Description |
|---|---|---|
OutputType |
Exe |
Builds as executable by default |
PublishAot |
true |
Enables Native AOT compilation |
IsAotCompatible |
true |
Marks the project as AOT compatible |
AllowUnsafeBlocks |
true |
Enables unsafe code |
TrimMode |
full |
Full trimming for size optimization |
InvariantGlobalization |
true |
Invariant globalization mode |
EnableTrimAnalyzer |
true |
Enables trim compatibility analyzer |
EnableAotAnalyzer |
true |
Enables AOT compatibility analyzer |
TreatWarningsAsErrors |
true |
Treats warnings as errors |
Library-Specific Settings
When OutputType is set to Library, the following additional settings are applied:
| Setting | Default Value | Description |
|---|---|---|
NativeLib |
Shared |
Builds as a shared/dynamic library (can be overridden to Static) |
DnneGenerateExports |
true |
DNNE export generation enabled |
DnneBuildExports |
false |
DNNE build exports disabled |
Debug Configuration
- Stack trace support enabled
- Metadata preserved for debugging
Release Configuration
- Stack trace support disabled
- Metadata trimmed for smaller binaries
Usage Examples
Building a Native Executable (Default)
<Project Sdk="NativeSdk/0.5.1">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>
Building a Shared Library (DLL/SO)
<Project Sdk="NativeSdk/0.5.1">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Library</OutputType>
</PropertyGroup>
</Project>
Building a Static Library
<Project Sdk="NativeSdk/0.5.1">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Library</OutputType>
<NativeLib>Static</NativeLib>
</PropertyGroup>
</Project>
DNNE Integration
This SDK includes DNNE for generating native exports when building as a library. Use the [UnmanagedCallersOnly] attribute to export functions:
using System.Runtime.InteropServices;
public static class NativeExports
{
[UnmanagedCallersOnly(EntryPoint = "add")]
public static int Add(int a, int b) => a + b;
}
Note: DNNE is only included when
OutputTypeis set toLibrary.
Customization
All settings can be overridden in your project file:
<PropertyGroup>
<OutputType>Library</OutputType>
<NativeLib>Static</NativeLib>
<IlcOptimizationPreference>Size</IlcOptimizationPreference>
<AllowUnsafeBlocks>false</AllowUnsafeBlocks>
</PropertyGroup>
Building
dotnet publish -c Release -r win-x64
dotnet publish -c Release -r linux-x64
dotnet publish -c Release -r osx-arm64
License
MIT
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.