Skip to content

Commit 5efcdd2

Browse files
authoredDec 1, 2023
Update NativeLibrary sample (#6456)
- Add prefix to native symbol names to follow best practices for avoiding name collisions - Make the sample work on .NET 8
1 parent f3523bf commit 5efcdd2

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed
 

‎core/nativeaot/NativeLibrary/Class1.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ namespace NativeLibrary
88
{
99
public class Class1
1010
{
11-
[UnmanagedCallersOnly(EntryPoint = "add")]
11+
// Use common prefix for all entrypoints to avoid symbol name collisions
12+
[UnmanagedCallersOnly(EntryPoint = "aotsample_add")]
1213
public static int Add(int a, int b)
1314
{
1415
return a + b;
1516
}
1617

17-
[UnmanagedCallersOnly(EntryPoint = "write_line")]
18+
[UnmanagedCallersOnly(EntryPoint = "aotsample_write_line")]
1819
public static int WriteLine(IntPtr pString)
1920
{
2021
// The marshalling code is typically auto-generated by a custom tool in larger projects.
@@ -35,7 +36,7 @@ public static int WriteLine(IntPtr pString)
3536
return 0;
3637
}
3738

38-
[UnmanagedCallersOnly(EntryPoint = "sumstring")]
39+
[UnmanagedCallersOnly(EntryPoint = "aotsample_sumstring")]
3940
public static IntPtr sumstring(IntPtr first, IntPtr second)
4041
{
4142
// Parse strings from the passed pointers

‎core/nativeaot/NativeLibrary/LoadLibrary.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
//Set this value accordingly to your workspace settings
77
#if defined(_WIN32)
8-
#define PathToLibrary "bin\\Debug\\net8.0\\win-x64\\publish\\NativeLibrary.dll"
8+
#define PathToLibrary "bin\\Release\\net8.0\\win-x64\\publish\\NativeLibrary.dll"
99
#elif defined(__APPLE__)
10-
#define PathToLibrary "./bin/Debug/net8.0/osx-x64/publish/NativeLibrary.dylib"
10+
#define PathToLibrary "./bin/Release/net8.0/osx-x64/publish/NativeLibrary.dylib"
1111
#else
12-
#define PathToLibrary "./bin/Debug/net8.0/linux-x64/publish/NativeLibrary.so"
12+
#define PathToLibrary "./bin/Release/net8.0/linux-x64/publish/NativeLibrary.so"
1313
#endif
1414

1515
#ifdef _WIN32
@@ -43,11 +43,11 @@ int main()
4343
}
4444

4545
// Sum two integers
46-
int sum = callSumFunc(PathToLibrary, "add", 2, 8);
46+
int sum = callSumFunc(PathToLibrary, "aotsample_add", 2, 8);
4747
printf("The sum is %d \n", sum);
4848

4949
// Concatenate two strings
50-
char *sumstring = callSumStringFunc(PathToLibrary, "sumstring", "ok", "ko");
50+
char *sumstring = callSumStringFunc(PathToLibrary, "aotsample_sumstring", "ok", "ko");
5151
printf("The concatenated string is %s \n", sumstring);
5252

5353
// Free string

‎core/nativeaot/NativeLibrary/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Create a .NET class library project using `dotnet new classlib -o NativeLibrary`
99
## Building shared libraries
1010

1111
```bash
12-
> dotnet publish /p:NativeLib=Shared
12+
> dotnet publish /p:NativeLib=Shared --use-current-runtime
1313
```
1414

1515
The above command will drop a shared library (Windows `.dll`, macOS `.dylib`, Linux `.so`) in `./bin/Release/net8.0/[RID]/publish/` folder and will have the same name as the folder in which your source file is present.
@@ -67,7 +67,7 @@ For a C# method in the native library to be consumable by external programs, it
6767
Apply the attribute to the method, specifying the `EntryPoint`:
6868

6969
```csharp
70-
[UnmanagedCallersOnly(EntryPoint = "add")]
70+
[UnmanagedCallersOnly(EntryPoint = "aotsample_add")]
7171
public static int Add(int a, int b)
7272
{
7373
return a + b;
@@ -86,7 +86,7 @@ The sample [source code](Class1.cs) demonstrates common techniques used to stay
8686
## Building static libraries
8787

8888
```bash
89-
> dotnet publish /p:NativeLib=Static
89+
> dotnet publish /p:NativeLib=Static --use-current-runtime
9090
```
9191

9292
The above command will drop a static library (Windows `.lib`, macOS/Linux `.a`) in `./bin/Release/net8.0/[RID]/publish/` folder and will have the same name as the folder in which your source file is present.

0 commit comments

Comments
 (0)
Please sign in to comment.