As seen e.g. here[1], we can export a function from .NET to be statically linked to another executable later.
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 the other direction also possible?
Something like this (pseudo code), is what I'm looking for:
using System;
using System.Runtime.InteropServices;
public static class NativeImports
{
[UnmanagedImpl(EntryPoint = "multiply")]
public static extern int Multiply(int a, int b);
}
Later on linked with this statically:
// multiply.c
int multiply(int a, int b)
{
return a*b;
}
Overall goal is to have a single, statically linked, dependency free executable mainly written in C#.
I know about P/Invoke et. al. that's my current workaround.
Answering my own question after getting some help (https://github.com/dotnet/runtime/issues/89044):
Minimal example for direct P/Invoke calls with static linking
This is the function we would like to statically link against our executable written in C#
This is our C# application. External functions to be linked statically are declared like normal P/Invoke functions.
This part of the project file is responsible for generating the direct calls and linking statically against the library written in C:
app.csprojReferences