Windows上面编译.net9.0的linux平台代码

December 24, 2024 | 0 Minute Read

  1. 安装.net 9.0 sdk
  2. visual stuio code 安装 c# 和 .net 的扩展
  3. dotnet.exe build -c Release
  4. dotnet publish -c Release -f net9.0 -r linux-x64 -p:PublishSingleFile=true –self-contained true [project_file_folder]/bin/[configuration]/[framework]/[runtime]/publish/
    生成的publish目录下面的文件 拿到linux就能运行

要在项目文件 *.csproj 里面配置 PublishSingleFile ,打包时才会精简和打包为单独一个文件。 但 PublishTrimmed 会有告警,设置了可能运行不起来

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <LangVersion>13</LangVersion>
    <PublishTrimmed>true</PublishTrimmed>
    <PublishSingleFile>true</PublishSingleFile>
    <SelfContained>true</SelfContained>
  </PropertyGroup>

参考:
https://learn.microsoft.com/zh-cn/dotnet/core/tools/dotnet-publish
https://learn.microsoft.com/zh-cn/dotnet/core/deploying/deploy-with-cli
https://learn.microsoft.com/zh-cn/dotnet/standard/frameworks
https://learn.microsoft.com/zh-cn/dotnet/core/rid-catalog