Location>code7788 >text

Cross-compiling Native AOT

Popularity:557 ℃/2024-12-19 23:02:37

How to publish .NET apps to Honcho is surely a topic of interest to many people.NET is now fully capable of running on the OpenHarmony system.NET now has many options CoreCLR, Mono and NativeAOT. due to the limitation of OpenHarmony's sandbox environment, NativeAOT is the best choice. After several months of exploration, Sun Ce student shared his experience and results of his exploration at the .NET Conf China 2024 conference on December 14, 2024 in Shanghai. OpenHarmony, as an open source operating system, has strong compatibility and extensibility in its own right. Avalonia, on the other hand, is a cross-platform UI framework that provides a consistent user experience across different operating systems. Combining the two is simply a powerful combination! Today's post is going to tell you about a cross-platform cross-compilation NativeAOT technique that Sunze used in this quest: using Zig as a linker and sysroot, it allows cross-compiling from Windows machines to Linux-x64, Linux-arm64, Linux-musl-x64, and Linux-musl- arm64.

NativeAOT (Native Ahead-Of-Time Compilation) is a technique for compiling .NET programs into native machine code to improve application performance and startup speed. Cross-compilation is the process of generating code on one platform for another. For example, generating executables on Windows for Linux.

In order to cross-compile, you need to install the appropriate toolchain for the target platform. For example, if you want to cross-compile for Linux, you need to install the Linux toolchain (e.g. GCC, Make, etc.) on Windows. This can usually be done by installing the Windows Subsystem for Linux (WSL) or using other tools like MinGW. We have a better way: the address of this project:/CeSun/PublishAotCross

Steps to use:

1. from the official zig website:/download/Download and configure Zig: Extract zig-windows-x86_64-0.14.0-dev.2540+ and add it to the PATH. One thing to note here is that the whole package should be complete, copy the binary file and also the lib directory, otherwise you may get a not found error, refer to/notes/unable-to-find-zig-installation-directory-filenotfound/

2. From/ Download the LLVM and set thellvm-objcopy The easiest way to add a file to the PATH is to put it in the same directory.

3. Add in the projectPublishAotCross The citation, specifically, can be found in:/CeSun/

Once you have done the above, you can use VS's publishing features. The following configuration is to publish the project using the new RID, for example, publish linux-musl-arm64 :

dotnet publish -r linux-musl-arm64