dotnet / dotnet/samples

NativeLibrary example is missing mention of F#

Open
#5,647 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
5.1k
Avg merge
9h 11m
Merged PRs (30d)
1

Description

**Issue description**

The [AOT Native Library example](https://github.com/dotnet/samples/tree/main/core/nativeaot/NativeLibrary) does not include any F# code or documentation. Attempting to adapt the example to F# as in the below example causes a compiler error `This attribute is currently unsupported by the F# compiler. Applying it will not achieve its intended effect.` but I cannot find any mention of this limitation, workarounds, future support date, etc.

This is particularly confusing/frustrating because there is [this lovely 2+ year old example of doing an F# native library in .NET SDK version 3](https://secanablog.wordpress.com/2020/02/01/writing-a-native-library-in-f-which-can-be-called-from-c/), but there seems to be little else on the web discussing F# native libraries in more modern .NET.

It would be superb if someone would be willing to add some or all of the following information to either to this example repo or the [official docs](https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/#build-native-libraries) or both!

Information which would be super helpful to me and others like me
- Whether there is an official mainline way to compile a Native Library from F# in the same way as C# in .NET 7
- The backstory to the compiler error and the current outlook for adding `UnmanagedCallersOnly` support to the F# compiler
- Best practices for working around the limitations in the current release of .NET 7 (is it best to use CoreRT despite it being superseded by mainline .NET?)

Thank you for reading!

```fsharp
// My attempt to expose a simple function in a Native Library build.
namespace HelloLib

open System.Runtime.InteropServices

module Say =
[]
let hello name =
printfn "Hello %s" name
```

**Target framework**

Check the .NET target framework(s) being used, and include the version number(s).

- [ ] .NET Core
- [ ] .NET Framework
- [X] .NET Standard 7.0.102

If using the .NET Core SDK, include `dotnet --info` output. If using .NET Framework without the .NET Core SDK, include info from Visual Studio's **Help** > **About Microsoft Visual Studio** dialog.

dotnet --info output or About VS info

```console
dotnet --info
.NET SDK:
Version: 7.0.102
Commit: 4bbdd14480

Runtime Environment:
OS Name: manjaro
OS Version:
OS Platform: Linux
RID: manjaro-x64
Base Path: /usr/share/dotnet/sdk/7.0.102/

Host:
Version: 7.0.2
Architecture: x64
Commit: d037e070eb

.NET SDKs installed:
7.0.102 [/usr/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.NETCore.App 7.0.2 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
None

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.