如何将 .NET 应用程序编译为本机代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:44:49  来源:igfitidea点击:

How to compile a .NET application to native code?

.netcompilationnative-code

提问by Niyaz

If I want to run a .NET application in a machine where the .NET framework is not available; Is there any way to compile the application to native code?

如果我想在 .NET 框架不可用的机器上运行 .NET 应用程序;有没有办法将应用程序编译为本机代码?

采纳答案by Espo

Microsoft has an article describing how you can Compile MSIL to Native Code

Microsoft 有一篇文章描述了如何将MSIL 编译为本机代码

You can use Ngen.

您可以使用Ngen

The Native Image Generator (Ngen.exe) is a tool that improves the performance of managed applications. Ngen.exe creates native images, which are files containing compiled processor-specific machine code, and installs them into the native image cache on the local computer. The runtime can use native images from the cache instead using the just-in-time (JIT) compiler to compile the original assembly.

本机映像生成器 (Ngen.exe) 是一种可提高托管应用程序性能的工具。Ngen.exe 创建本机映像,这些文件包含已编译的特定于处理器的机器代码,并将它们安装到本地计算机上的本机映像缓存中。运行时可以使用缓存中的本机图像,而不是使用即时 (JIT) 编译器来编译原始程序集。

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

不幸的是,您仍然需要框架中的库才能运行您的程序。我所知道的 MS .Net 框架 SDK 没有任何功能可以让您将所有必需的文件编译成单个可执行文件

回答by Simon Steele

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 制作了一种工具,可将 .NET 应用程序编译为无需安装 .NET 即可运行的包。我没有任何经验:

RemoteSoft Salamander

远程软蝾螈

回答by Erick Sgarbi

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 XenocodePostbuild

我已经测试了其中的几个,目前唯一一个支持 .NET 3.5 并且有一个很棒的虚拟化堆栈的是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.

使用 ngen,您仍然需要安装 .NET 框架,但使用这样的工具,您的所有托管代码都被编译为本机代码,因此您可以将其部署到没有框架存在的机器上。

回答by thepirat000

Microsoft has announced its .NET Native Previewthat will allow to run .NET applications without having the framework installed.

微软已经宣布了它的.NET Native Preview,它将允许在没有安装框架的情况下运行 .NET 应用程序。

Take a look: http://blogs.msdn.com/b/dotnet/archive/2014/04/02/announcing-net-native-preview.aspx

看一看:http: //blogs.msdn.com/b/dotnet/archive/2014/04/02/annoucing-net-native-preview.aspx

FAQ: http://msdn.microsoft.com/en-US/vstudio/dn642499.aspx

常见问题解答: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

您可以从这里下载适用于 VS2013 的 Microsoft .NET Native:http: //msdn.microsoft.com/en-US/vstudio/dotnetnative

回答by James Ko

As some of the other answers here have mentioned, you can use the .NET Nativetool to compile your app to native machine code. Unlike those answers, however, I will explain howto do it.

正如这里的其他一些答案所提到的,您可以使用.NET Native工具将您的应用程序编译为本机机器代码。然而,与这些答案不同的是,我将解释如何去做。

Steps:

脚步:

  1. 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.

  2. Open up a shell prompt and cdto the directory of your app.

  3. Type this:

    dotnet compile --native
    
  1. 安装dotnet CLI(命令行界面)工具,它是新的 .NET Core 工具链的一部分。我们将使用它来编译我们的应用程序;你可以在这里找到一篇关于它的好文章

  2. 打开一个 shell 提示并cd进入您的应用程序目录。

  3. 输入:

    dotnet compile --native
    

That's it! When you're done, your app will be compiled down to a single binary, like this:

就是这样!完成后,您的应用程序将被编译为单个二进制文件,如下所示:

Native compiled .NET Core EXE

本机编译的 .NET Core EXE

It'll be a standalone executable; no PDBs, assemblies, or config files included (hooray!).

它将是一个独立的可执行文件;不包含 PDB、程序集或配置文件(万岁!)。



Alternatively, if you want an even faster program, you can run this:

或者,如果你想要一个更快的程序,你可以运行这个:

dotnet compile --native --cpp

That will optimize your program using the C++ code generator (as opposed to RyuJIT), so your app is even more optimized for AOT scenarios.

这将使用 C++ 代码生成器(而不是 RyuJIT)优化您的程序,因此您的应用程序针对 AOT 场景进行了更加优化。

You can find more info on this at the dotnet CLI GitHub repo.

您可以在 dotnet CLI GitHub 存储库中找到更多信息。

回答by Chris Zwiryk

Yes, using Ngen, the Native Image Generator. There are, however, a number of things you need to be aware of:

是的,使用Ngen,原生图像生成器。但是,您需要注意以下几点:

  • You still need the CLR to run your executable.
  • The CLR will not dynamically optimize your assemblies based on the environment it's run in (e.g. 486 vs. 586 vs. 686, etc.)
  • 您仍然需要 CLR 来运行您的可执行文件。
  • CLR 不会根据它运行的环境动态优化你的程序集(例如 486 对 586 对 686 等)

All in all, it's only worth using Ngen if you need to reduce the startup time of your application.

总而言之,只有当您需要减少应用程序的启动时间时,才值得使用 Ngen。

回答by Camilo Martin

You can! However you're restricted to .NET 1.1 (no generics for you): Mono Ahead-Of-Time compilation (AOT)

你可以!但是,您仅限于 .NET 1.1(没有适合您的泛型): Mono Ahead-Of-Time 编译 (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.

它最初的设计是因为 iPhone 没有 .NET 或 Mono,所以他们就是这样制作 MonoTouch 的。

回答by m_eric

You can do this using the new precompilation technology called .NET Native. Check it out here: http://msdn.microsoft.com/en-US/vstudio/dotnetnative

您可以使用称为 .NET Native 的新预编译技术来执行此操作。在这里查看: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)

目前它仅适用于 Windows 应用商店应用。它执行单个组件链接。因此 .NET Framework 库静态链接到您的应用程序中。一切都编译为本机,不再部署 IL 程序集。应用程序不针对 CLR 运行,而是针对称为托管运行时 (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 Directivesto provide hints to the .NET Native compiler on the dynamic scenarios they wish to support.

如上所述,NGEN 使用混合编译模型并依赖 IL 和 JIT 来处理动态场景。.NET Native 不使用 JIT,但它支持各种动态场景。代码作者需要利用 运行时指令向 .NET Native 编译器提供关于他们希望支持的动态场景的提示。

回答by Matt Bishop

You can use ngen.exe to generate a native image butyou still have to distribute the original non-native code as well, and it still needs the framework installed on the target machine.

您可以使用 ngen.exe 生成本机映像,您仍然需要分发原始的非本机代码,并且它仍然需要在目标机器上安装框架。

Which doesn't solve your problem, really.

这并不能解决你的问题,真的。

回答by brk

2019 Answer: Use dotnet/corert. It can compile .NET Core projects into standalone .exefiles. No dependencies (except for system libraries like kernel32.dll). I bet this is exactly what the OP need.

2019 答案:使用dotnet/corert。它可以将 .NET Core 项目编译成独立.exe文件。没有依赖项(除了像 的系统库kernel32.dll)。我敢打赌这正是 OP 所需要的。

From its GitHub home page:

从其 GitHub 主页:

The CoreRT compiler can compile a managed .NET Core application into a native (architecture specific) single-file executable that is easy to deploy. It can also produce standalone dynamic or static libraries that can be consumed by applications written in other programming languages.

CoreRT 编译器可以将托管的 .NET Core 应用程序编译为易于部署的本机(特定于体系结构)单文件可执行文件。它还可以生成独立的动态或静态库,供以其他编程语言编写的应用程序使用。