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

发布于 2024-07-04 23:45:49 字数 64 浏览 13 评论 0 原文

假设我想在 .NET 框架不可用的计算机上运行 .NET 应用程序; 有什么方法可以将应用程序编译为本机代码吗?

Let's say I want to run a .NET application on a machine where the .NET framework is not available; Is there any way to compile the application to native code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

丿*梦醉红颜 2024-07-11 23:45:50

我认为这是不可能的。 您还需要分发 .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

没有你我更好 2024-07-11 23:45:49

Microsoft 有一篇文章介绍了如何将 MSIL 编译为本机代码< /a>

您可以使用 Ngen

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

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

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

You can use 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.

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

为你拒绝所有暧昧 2024-07-11 23:45:49

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

步骤:

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

  2. 打开 shell 提示符并cd 到您的应用程序的目录。

  3. 输入:

    dotnet 编译 --native 
      

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

本机编译的 .NET Core EXE

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


或者,如果您想要更快的程序,您可以运行以下命令:

dotnet compile --native --cpp

这将使用 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:

  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 cd to the directory of your app.

  3. Type this:

    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

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:

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.

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

維他命╮ 2024-07-11 23:45:49

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

滿滿的愛 2024-07-11 23:45:49

我已经测试了其中的几个,目前唯一支持 .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.

-黛色若梦 2024-07-11 23:45:49

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

韬韬不绝 2024-07-11 23:45:49

你可以! 但是,您仅限于 .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.

浮世清欢 2024-07-11 23:45:49

是的,使用 Ngen(本机映像生成器)。 但是,您需要注意以下几点:

  • 您仍然需要 CLR 来运行可执行文件。
  • CLR 不会根据其运行环境动态优化您的程序集(例如486 vs. 586 vs. 686 等)

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

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

  • 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.)

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

瑕疵 2024-07-11 23:45:49

您可以使用 NativeAOT (.NET 7 的一部分,以前是 CoreRT)。

这项技术有一点限制,因为你不能过多依赖反射,但总的来说你可以用它来编译应用程序的范围。 Web 应用程序、WinForms 应用程序、控制台应用程序。

从 .NET 7 Preview 5 开始,您只需添加

<PropertyGroup>
   <PublishAot>true</PublishAot>
</PropertyGroup>

如果您想使用 NativeAOT 的日常构建,您需要在项目文件中添加以下行

<ItemGroup>
  <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="8.0.0-*" />
</ItemGroup>

,并将 dotnet8 Nuget feed 添加到您的 nuget.config 中,就像这样

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <packageSources>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="dotnet-public" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json" />
    <add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
 </packageSources>
</configuration>

您可以定位.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

<PropertyGroup>
   <PublishAot>true</PublishAot>
</PropertyGroup>

If you want use daily builds of NativeAOT you need add following lines in your project file

<ItemGroup>
  <PackageReference Include="Microsoft.DotNet.ILCompiler" Version="8.0.0-*" />
</ItemGroup>

and add dotnet8 Nuget feed into your nuget.config like that

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <packageSources>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="dotnet-public" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json" />
    <add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
 </packageSources>
</configuration>

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

那伤。 2024-07-11 23:45:49

您可以使用称为 .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.

送舟行 2024-07-11 23:45:49

编译为 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).

誰ツ都不明白 2024-07-11 23:45:49

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

庆幸我还是我 2024-07-11 23:45:49

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

从其 GitHub 主页:

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

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

From its GitHub home page:

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.

如痴如狂 2024-07-11 23:45:49

您可以使用 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.

冰雪之触 2024-07-11 23:45:49

试试这个(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文