如何将 .NET 应用程序编译为本机代码?
假设我想在 .NET 框架不可用的计算机上运行 .NET 应用程序; 有什么方法可以将应用程序编译为本机代码吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
假设我想在 .NET 框架不可用的计算机上运行 .NET 应用程序; 有什么方法可以将应用程序编译为本机代码吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(16)
我认为这是不可能的。 您还需要分发 .NET FW。 如果要将 .NET 应用程序编译为本机代码,请使用 NGen 工具
I think it's not possible. You will need to distribute .NET FW as well. If you want to compile .NET app to native code, use NGen tool
Microsoft 有一篇文章介绍了如何将 MSIL 编译为本机代码< /a>
您可以使用 Ngen。
不幸的是,您仍然需要框架中的库才能运行您的程序。 据我所知,MS .Net 框架 SDK 没有任何功能可以让您将所有必需的文件编译为单个可执行文件
Microsoft has an article describing how you can Compile MSIL to Native Code
You can use Ngen.
Unfortunately, you still need the libraries from the framework in order to run your program. There's no feature that I know of with the MS .Net framework SDK that allows you to compile all the required files into a single executable
正如这里的一些其他答案所提到的,您可以使用 .NET Native 将您的应用程序编译为本机机器代码的工具。 然而,与这些答案不同的是,我将解释如何做到这一点。
步骤:
安装dotnet CLI(命令行界面)工具,该工具是新的 .NET Core 工具链。 我们将使用它来编译我们的应用程序; 您可以在此处找到一篇关于它的好文章。
打开 shell 提示符并
cd
到您的应用程序的目录。输入:
就是这样! 完成后,您的应用程序将被编译为单个二进制文件,如下所示:
它将是一个独立的可执行文件; 不包含 PDB、程序集或配置文件(万岁!)。
或者,如果您想要更快的程序,您可以运行以下命令:
这将使用 C++ 代码生成器(而不是 RyuJIT)优化您的程序,因此您的应用程序针对 AOT 场景更加优化。
您可以在 dotnet CLI GitHub 存储库。
As some of the other answers here have mentioned, you can use the .NET Native tool to compile your app to native machine code. Unlike those answers, however, I will explain how to do it.
Steps:
Install the dotnet CLI (command line interface) tool, which is part of the new .NET Core toolchain. We'll use this to compile our app; you can find a good article about it here.
Open up a shell prompt and
cd
to the directory of your app.Type this:
That's it! When you're done, your app will be compiled down to a single binary, like this:
It'll be a standalone executable; no PDBs, assemblies, or config files included (hooray!).
Alternatively, if you want an even faster program, you can run this:
That will optimize your program using the C++ code generator (as opposed to RyuJIT), so your app is even more optimized for AOT scenarios.
You can find more info on this at the dotnet CLI GitHub repo.
Microsoft 发布了.NET Native Preview,允许在不安装框架的情况下运行.NET 应用程序。
看一下:http://blogs.msdn。 com/b/dotnet/archive/2014/04/02/announcing-net-native-preview.aspx
常见问题解答:http://msdn.microsoft.com/en-US/vstudio/dn642499.aspx
您可以从此处下载 Microsoft .NET Native for VS2013:http://msdn.microsoft.com/en-US/vstudio/dotnetnative
Microsoft has announced its .NET Native Preview that will allow to run .NET applications without having the framework installed.
Take a look: http://blogs.msdn.com/b/dotnet/archive/2014/04/02/announcing-net-native-preview.aspx
FAQ: http://msdn.microsoft.com/en-US/vstudio/dn642499.aspx
You can download Microsoft .NET Native for VS2013 from here: http://msdn.microsoft.com/en-US/vstudio/dotnetnative
我已经测试了其中的几个,目前唯一支持 .NET 3.5 并且具有出色的虚拟化堆栈的是 Xenocode< /a> Postbuild
使用 ngen,您仍然需要安装 .NET 框架,但需要使用工具因此,您的所有托管代码都会编译为本机代码,因此您可以将其部署到没有框架的计算机上。
I have tested several of them and at this moment the only one that supports .NET 3.5 and also has a great virtualization stack is Xenocode Postbuild
With ngen you still need to have the .NET framework installed but using a tool as such all your managed code is compiled into native code so you can deploy it to machines without the framework presence.
RemoteSoft 制作了一个工具,可以将 .NET 应用程序编译成无需安装 .NET 即可运行的包。 我没有任何经验:
RemoteSoft Salamander
RemoteSoft makes a tool that compiles a .NET application into a package that can be run without .NET installed. I don't have any experience with it:
RemoteSoft Salamander
你可以! 但是,您仅限于 .NET 1.1(没有适合您的泛型):
Mono 提前编译 (AOT)
但是,这意味着编译实际上是本机的,因此您将不再能够部署一个字节码程序集,每个平台都需要一个。
它最初是因为没有适用于 iPhone 的 .NET 或 Mono 而设计的,所以他们就是这样制作 MonoTouch 的。
You can! However you're restricted to .NET 1.1 (no generics for you):
Mono Ahead-Of-Time compilation (AOT)
However, this means compiling is really native, so you'll no longer be able to deploy one single bytecode assembly, you'll need one per platform.
It was originally designed because there's no .NET or Mono for iPhone, so that's how they made MonoTouch.
是的,使用 Ngen(本机映像生成器)。 但是,您需要注意以下几点:
总而言之,只有当您需要减少应用程序的启动时间时才值得使用 Ngen。
Yes, using Ngen, the Native Image Generator. There are, however, a number of things you need to be aware of:
All in all, it's only worth using Ngen if you need to reduce the startup time of your application.
您可以使用 NativeAOT (.NET 7 的一部分,以前是 CoreRT)。
这项技术有一点限制,因为你不能过多依赖反射,但总的来说你可以用它来编译应用程序的范围。 Web 应用程序、WinForms 应用程序、控制台应用程序。
从 .NET 7 Preview 5 开始,您只需添加
如果您想使用 NativeAOT 的日常构建,您需要在项目文件中添加以下行
,并将
dotnet8
Nuget feed 添加到您的 nuget.config 中,就像这样您可以定位.NET 6 应用程序,并且随着 .NET 6 中 ILTrim 的改进,越来越多的代码将准备好进行本机编译。
对于简单的应用程序,您可以尝试使用 BFlat 这可能会给您带来更好的结果。
You can use NativeAOT (part of .NET 7, formerly CoreRT).
This technology is a bit limiting, since you cannot rely on reflection too much, but overall you can compile range of application with it. Web apps, WinForms apps, console apps.
As of .NET 7 Preview 5 you can just add
If you want use daily builds of NativeAOT you need add following lines in your project file
and add
dotnet8
Nuget feed into your nuget.config like thatYou can target .NET 6 apps, and with ILTrim improvements in .NET 6 the more and more code would be ready for native compilation.
For simple applications you can try use BFlat which may give you even better results.
您可以使用称为 .NET Native 的新预编译技术来完成此操作。 在这里查看:http://msdn.microsoft.com/en-US/vstudio/dotnetnative
目前它仅适用于 Windows 应用商店应用程序。 它执行单组件链接。 因此 .NET Framework 库静态链接到您的应用程序中。 所有内容都编译为本机,并且不再部署 IL 程序集。 应用程序不是针对 CLR 运行,而是针对称为托管运行时 (Mrt.dll) 的精简优化运行时运行
如上所述,NGEN 使用混合编译模型,并依赖 IL 和 JIT 来实现动态场景。 .NET Native 不使用 JIT,但它支持各种动态场景。 代码作者需要利用运行时指令来提供提示.NET Native 编译器希望支持的动态场景。
You can do this using the new precompilation technology called .NET Native. Check it out here: http://msdn.microsoft.com/en-US/vstudio/dotnetnative
Currently it is only available for Windows Store Apps. It performs single component linking. So .NET Framework libraries are statically linked into your app. Everything is compiled to native and IL assemblies are no longer deployed. Apps do not run against CLR but a stripped down, optimized runtime called Managed Runtime (Mrt.dll)
As stated above, NGEN used a mix compilation model and relied on IL and JIT for dynamic scenarios. .NET Native does not utilise JIT but it does support various dynamic scenarios. Code authors would need to utilize Runtime Directives to provide hints to the .NET Native compiler on the dynamic scenarios they wish to support.
看起来像net core RT可行的解决方案;
很快所有应用程序都将转向.net core;
https://www.codeproject.com/Articles/5262251/Generate-Native-Executable-from-NET-Core-3-1-Proje?msg=5753507#xx5753507xx
https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/november/net-core-publishing-options-with-net-core
可能未使用旧的 win .net sdk 进行测试可能做类似的事情。
Looks like net core RT workable solutions;
soon all apps will go to .net core;
https://www.codeproject.com/Articles/5262251/Generate-Native-Executable-from-NET-Core-3-1-Proje?msg=5753507#xx5753507xx
https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/november/net-core-publishing-options-with-net-core
not tested maybe with old win .net sdk possible do similar.
编译为 Native 的主要原因是为了保护您的代码,否则编译的 MSIL 就像将源代码部署在客户端计算机中一样。
NGEN编译成native还需要部署IL代码,这样做的目的只是为了减少启动时间但也没什么用。
CoreRt 是 alpha 版本,仅适用于简单的 helloworld 类型的应用程序。
.Net Core 编译成单个可执行文件,但它也不是本机 exe,这只是 IL 代码的压缩文件,运行时它将把代码解压缩到临时文件夹中。
Microsoft 向我提出的简单问题是,如果 RyuJIT 可以动态地将 IL 编译为本机,那么为什么不可以提前编译相同的 IL (AOT)。
The main reason to compile into Native is to secure your codes, otherwise the MSIL compiled is like deploying the source codes in the client's machine.
NGEN compiles into native but also need to deploy IL codes, this purpose is just to reduce the startup time but it is also useless.
CoreRt is alpha version and working only with simple helloworld type apps.
.Net Core compiles into single executable files but it is also not native exe, this is just a zipped file of IL codes and it will unzip the codes into temp folder while running.
My simple question from Microsoft is, if RyuJIT can compile IL into native on the fly then why not you can compile the same IL ahead-of-time (AOT).
.NET 的本质是能够安装已编译为 MSIL 的应用程序,然后通过 JIT 或 Ngen,将 MSIL 编译为本机代码并存储在本地缓存中。 它从来没有打算生成一个可以独立于 .NET 框架运行的真正的本机 .exe。
也许有一些黑客可以做到这一点,但对我来说听起来并不安全。 有太多的动态需要框架,例如:动态程序集加载、MSIL代码生成等。
The nature of .NET is to be able to install apps that have been compiled to MSIL, then either by JIT or Ngen, MSIL is compiled to native code and stored locally in a cache. It was never intended on generating a true native .exe that can be run independently of the .NET framework.
Maybe there's some hack that does this, but it doesn't sound safe to me. There are too many dynamics that require the framework, such as: dynamic assembly loading, MSIL code generation, etc.
2019 年答案:使用 dotnet/corert。 它可以将 .NET Core 项目编译为独立的
.exe
文件。 没有依赖项(除了像kernel32.dll
这样的系统库)。 我敢打赌这正是OP所需要的。从其 GitHub 主页:
2019 Answer: Use dotnet/corert. It can compile .NET Core projects into standalone
.exe
files. No dependencies (except for system libraries likekernel32.dll
). I bet this is exactly what the OP need.From its GitHub home page:
您可以使用 ngen.exe 生成本机映像但是您仍然必须分发原始非本机代码,并且它仍然需要在目标上安装框架机器。
这确实不能解决你的问题。
You can use ngen.exe to generate a native image but you still have to distribute the original non-native code as well, and it still needs the framework installed on the target machine.
Which doesn't solve your problem, really.
试试这个(http://www.dotnetnative.online/)将.net编译的exe编译为本机exe ,我尝试过这个,它很新但是很好。
try this (http://www.dotnetnative.online/) to compile .net compiled exe into native exe, I tried this, its new but good.