How does ngen works?
Asked Answered
T

1

2

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.

Tymbal answered 6/4, 2015 at 0:57 Comment(1)
Good question! I'm really curious if you got any good solution for this by now. Or, have you tried AOT Compilation from the Mono project?Stringhalt
A
4

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.

Autostability answered 6/4, 2015 at 1:53 Comment(1)
Thanks for clarify, I thought it would act like a native C# compiler. I'll try luck with this one csnative.codeplex.comTymbal

© 2022 - 2024 — McMap. All rights reserved.