本文档介绍了如何在 Arch Linux 环境下,使用 lld 和 msvc-wine-git 交叉编译 C# Native AOT 项目,使其能够在 Windows 上运行。
使用的 Linux 发行版: Arch Linux
使用 AUR 助手 (例如 yay 或 paru) 安装 lld 和 msvc-wine-git。这一步是为了确保系统中存在 lld-link 命令以及 Windows SDK。
# 使用 yay
yay -S lld msvc-wine-git
# 或者使用 paru
paru -S lld msvc-wine-git注意
msvc-wine-git的默认安装目录是/opt/msvc。如果您的安装目录不同,请在.csproj项目文件中添加MSVCWineBinPath属性来指定正确的路径,例如:<PropertyGroup> <MSVCWineBinPath>/path/to/your/msvc/bin</MSVCWineBinPath> </PropertyGroup>
在 .csproj 文件中引入 PublishAotCross.LinuxToWin 以启用交叉编译功能。
在您的项目根目录打开终端,运行以下命令来为 Windows x64 平台发布 Release 版本的应用:
dotnet publish ./ -r win-x64 -c Release编译成功后,可以使用 wine 运行生成的可执行文件,验证程序是否能正常工作。
wine ./bin/Release/net8.0/win-x64/publish/YourProjectName.exe如果一切顺利,您应该能看到程序成功打印出 Hello, World! 或其他预期的输出。
This document describes how to cross-compile a C# Native AOT project on Arch Linux using lld and msvc-wine-git to run on Windows.
Linux Distribution Used: Arch Linux
Use an AUR helper (like yay or paru) to install lld and msvc-wine-git. This step ensures that the lld-link command and the Windows SDK are available on your system.
# Using yay
yay -S lld msvc-wine-git
# Or using paru
paru -S lld msvc-wine-gitNote
The default installation directory for
msvc-wine-gitis/opt/msvc. If your installation path is different, please add theMSVCWineBinPathproperty in your.csprojproject file to specify the correct path, for example:<PropertyGroup> <MSVCWineBinPath>/path/to/your/msvc/bin</MSVCWineBinPath> </PropertyGroup>
Import PublishAotCross.LinuxToWin in your .csproj file to enable the cross-compilation functionality.
Open a terminal in your project's root directory and run the following command to publish a Release version of your application for the Windows x64 platform:
dotnet publish ./ -r win-x64 -c ReleaseAfter a successful compilation, you can use wine to run the generated executable and verify that the program works correctly.
wine ./bin/Release/net8.0/win-x64/publish/YourProjectName.exeIf everything is successful, you should see the program print Hello, World! or other expected output.