dotnet/runtime

Native AOT application linking with external static library or object files

sneusse opened this issue · 6 comments

We currently support linking AOT library functions to third party applications with code like this:

using System;
using System.Runtime.InteropServices;

public static class NativeExports
{
    [UnmanagedCallersOnly(EntryPoint = "multiply")]
    public static int Multiply(int a, int b)
    {
        return a * b;
    }
}

Is there a way to have third party libraries or object files statically linked to an .NET AOT application?

Pseudocode:

using System;
using System.Runtime.InteropServices;

public static class NativeImports
{
    [UnmanagedImpl(EntryPoint = "multiply")]
    public static extern int Multiply(int a, int b); // implemented in multiplylib.obj
}

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Issue Details

We currently support linking AOT library functions to third party applications with code like this:

using System;
using System.Runtime.InteropServices;

public static class NativeExports
{
    [UnmanagedCallersOnly(EntryPoint = "multiply")]
    public static int Multiply(int a, int b)
    {
        return a * b;
    }
}

Is there a way to have third party libraries or object files statically linked to an .NET AOT application?

Pseudocode:

using System;
using System.Runtime.InteropServices;

public static class NativeImports
{
    [UnmanagedImpl(EntryPoint = "multiply")]
    public static extern int Multiply(int a, int b); // implemented in multiplylib.obj
}
Author: sneusse
Assignees: -
Labels:

area-System.Runtime.InteropServices

Milestone: -
agocke commented

In short, yes: https://github.com/dotnet/samples/tree/main/core/nativeaot/NativeLibrary#building-static-libraries

However this mode is still somewhat experimental because it's hard to ensure that the compiler tooling versions match up exactly, which is a requirement for most native linkers.

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

We currently support linking AOT library functions to third party applications with code like this:

using System;
using System.Runtime.InteropServices;

public static class NativeExports
{
    [UnmanagedCallersOnly(EntryPoint = "multiply")]
    public static int Multiply(int a, int b)
    {
        return a * b;
    }
}

Is there a way to have third party libraries or object files statically linked to an .NET AOT application?

Pseudocode:

using System;
using System.Runtime.InteropServices;

public static class NativeImports
{
    [UnmanagedImpl(EntryPoint = "multiply")]
    public static extern int Multiply(int a, int b); // implemented in multiplylib.obj
}
Author: sneusse
Assignees: -
Labels:

untriaged, area-NativeAOT-coreclr

Milestone: -

Are you trying to statically link native code into a Native AOT app? If so, this is the doc to look at: https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/interop

Basically, you declare a DllImport as usual, but tell the compiler to treat is a a DirectPInvoke, and pass the obj file as NativeLibrary to the linker.

This issue has been marked needs-author-action and may be missing some important information.

Awesome, thanks! This is exactly what I was looking for!
I tried a minimal example and thought I'd share it, in case anybody stumbles over this issue :)

https://github.com/sneusse/aot-static-example