ahfuzhang

https://github.com/ahfuzhang

导航

[DotNet] Linux 下如何编译,才能得到一个不依赖任何 so 的独立二进制文件?

作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!


直接上答案:

dotnet publish $(PRJ).csproj \
	  -r linux-x64 \
	  -p:DefineConstants=UNIX -p:AllowUnsafeBlocks=true \
	  -p:PublishAot=true \
	  -p:StripSymbols=false \
	  -p:StaticLinkedRuntime=true \
	  -p:StaticExecutable=true \
	  -p:PositionIndependentExecutable=false \
	  -p:InvariantGlobalization=true \
	  -p:OptimizationPreference=Size \
	  -p:CppCompilerAndLinker=clang \
	  --self-contained true \
	  -c Release -o $(BUILD_DIR)

验证:

ldd ./Http2EchoServer
        not a dynamic executable

在 docker 中运行:

docker run --rm \
	  --platform=linux/amd64 \
	  --cpuset-cpus="19" \
		-m 256m \
	  -e DOTNET_HOSTBUILDER__RELOADCONFIGONCHANGE=false \
	  -v $(BUILD_DIR):/app \
	  --network=host \
	  alpine:latest \
	  /app/Http2EchoServer \
	    -http2.port=9081 \
		-http1.port=9082 \
		-threadpool.max=1
  • -e DOTNET_HOSTBUILDER__RELOADCONFIGONCHANGE=false 这个需要加上。
    • 避免 ASP.NET Core 在 Alpine 里因为 inotify 限额启动失败。

否则出现异常:

Unhandled exception: System.IO.IOException: The configured user limit (128) on the number of inotify instances has been reached, or the per-process limit on the number of open file descriptors has been reached.
   at System.IO.FileSystemWatcher.StartRaisingEvents() + 0x1d4
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.TryEnableFileSystemWatcher() + 0x60
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String) + 0x6d
   at Microsoft.Extensions.FileProviders.PhysicalFileProvider.Watch(String) + 0xc6
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1, Action) + 0xa0
   at Microsoft.Extensions.Configuration.FileConfigurationProvider..ctor(FileConfigurationSource) + 0x7b
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder) + 0x2e
   at Microsoft.Extensions.Configuration.ConfigurationManager.AddSource(IConfigurationSource) + 0x23
   at Microsoft.Extensions.Configuration.ConfigurationManager.Microsoft.Extensions.Configuration.IConfigurationBuilder.Add(IConfigurationSource) + 0x5e
   at Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.ApplyDefaultAppConfiguration(HostBuilderContext, IConfigurationBuilder, String[]) + 0x45
   at Microsoft.Extensions.Hosting.HostApplicationBuilder..ctor(HostApplicationBuilderSettings) + 0x170
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder..ctor(WebApplicationOptions, Action`1) + 0xad
   at Http2EchoServer.Program.<RunServerAsync>d__4.MoveNext() + 0xa2
--- End of stack trace from previous location ---
   at Http2EchoServer.Program.<>c__DisplayClass2_0.<<Main>b__3>d.MoveNext() + 0x66
--- End of stack trace from previous location ---
   at System.CommandLine.Invocation.AnonymousCommandHandler.<InvokeAsync>d__6.MoveNext() + 0xa2
--- End of stack trace from previous location ---
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext() + 0x53
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass17_0.<<UseParseErrorReporting>b__0>d.MoveNext() + 0x58
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass12_0.<<UseHelp>b__0>d.MoveNext() + 0x51
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseVersionOption>b__0>d.MoveNext() + 0x59
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass19_0.<<UseTypoCorrections>b__0>d.MoveNext() + 0x5b
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__18_0>d.MoveNext() + 0x5f
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseParseDirective>b__0>d.MoveNext() + 0x56
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__5_0>d.MoveNext() + 0xec
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass8_0.<<UseExceptionHandler>b__0>d.MoveNext() + 0x57

posted on 2026-03-17 13:24  ahfuzhang  阅读(7)  评论(0)    收藏  举报