问题
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
It take a while but I found the generated file is at C:\Windows\assembly\NativeImages_v4.0.30319_32\myProgram\db1496cf0295bbe6a9242d86c0d8e091\myProgram.ni.exe
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.
回答1:
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.
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.
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.
来源:https://stackoverflow.com/questions/29464018/how-does-ngen-works