如何NGEN的作品?作品、NGEN

2023-09-04 05:41:52 作者:流年、只为邂逅最美的红颜

所以,从我的理解,它确实需要写在 MSIL 并做了JIT作业:转换MSIL code到本机code。对? 所以跑 NGEN 生成使用命令我的程序的本机映像:

So, from what I understand, it does take an executable/dll file written in MSIL and does the JIT-job: convert MSIL code to native machine code. Right? So ran the ngen to generate a native image of my program using the command:

ngen install myProgram.exe

这需要一段时间,但我发现生成的文件是在C:\Windows\assembly\NativeImages_v4.0.30319_32\myProgram\db1496cf0295bbe6a9242d86c0d8e091\myProgram.ni.exe

但是,什么是该可执行文件完全相同的内容是什么?我的C#程序机器code版?它不运行(见下文)。如果我想向用户提供我的程序的原生版本,如果我跑 NGEN 中的每个 *。exe文件 *。dll的并给予生成图像文件给用户,它将会运行得很好?做它仍然需要.NET框架?

But what's exactly the contents of that executable file? a machine code version of my C# program? it doesn't run (see below). if I want to provide to the user a native version of my program, if I run ngen in each *.exe and *.dll and give that generate image files to the user, it will run fine? does it still needs the .NET framework?

但是,生成的图像不工作,并给予这个程序不能在你的电脑上运行的错误消息。

However, that generate image doesn't works and give the "This app can't run on your PC" error message.

推荐答案

您是正确的,NGEN编译JIT code到机code,但你仍然需要.NET运行库运行程序。如果你的目标是使一个程序,并不需要.NET框架是行不通的。

You are correct that ngen compiles the JIT code to machine code but you still need the .net runtime libraries to run the program. If your goal was to make a program that did not need the .net framework it will not work.

你可以把它像编写动态链接到C ++运行时本机程序,C ++运行时,必须安装该程序的工作。 .NET没有类似的静态链接像本地应用程序做,你必须已安装运行时。

Think of it like writing a native program that is dynamically linked to the C++ runtime, the c++ runtime must be installed for the program to work. .net has no equivalent of statically linking like native apps do, you must always have the runtime installed.

你也不能运行NGEN直接内置图像,当您运行NGEN它安装它的程序集缓存(列出的目录),当您运行NET应用程序找到该副本在高速缓存中,并使用pre - 生成code,不能直接运行它。

Also you can not run ngen built images directly, when you run ngen it installs it in to the assembly cache (the directory listed) when you run your .net app it finds the copy in cache and uses the pre-generated code, you can't run it directly.