You can use NGEN.EXE to compile a MSIL assembly to a native assembly. But when you have the source code, you can also choose "Platform target" in the project options. If I choose, say, x64 there, will that be the same thing as running NGEN.EXE on it on an x64 machine?
Asked
Active
Viewed 203 times
2
-
No, choosing a platform target of x64 will not natively compile it. It still will be compiled to an intermediate language, though it will only run on an x64 platform. The problem with NGEN is you cannot distribute an NGEN'ed assembly - it's a service that runs on the machine. So you can distribute an assembly, then have the service ngen it. – vcsjones Mar 20 '13 at 12:19
-
@vcsjones - Even if the target computer has the same architecture (x64/x86)? – Vilx- Mar 20 '13 at 12:20
-
@vcsjones - No, I mean about NGEN not being distributable. Are the NGEN'd assemblies then tightly bound to the machine on which they have been created? – Vilx- Mar 20 '13 at 12:26
-
@Vilx AOT/NGEN have issues when using third-party libraries and generics. See for example the disclaimers on the AOT Mono page I linked – Lorenzo Dematté Mar 20 '13 at 12:30
-
@Vilx- see this answer too http://stackoverflow.com/a/45710/863564 – Lorenzo Dematté Mar 20 '13 at 12:33
-
@Vilx NGEN *may* make use of CPU features that don't exist on all procs, like AES-NI. I don't know for sure if that happens in practice - but it certainly could, but yes it is bound to the machine for all practical purposes. – vcsjones Mar 20 '13 at 13:14
1 Answers
3
No, it is not the same thing. Platform target only sets a bit in the PE header, telling the runtime if it prefers to run on the x86 or x64 version of the CLR (or neutral, if you specify "Any CPU"). Compilation will still produce MSIL code; AFAIK, it is not possible to tell to the MS compilers to directly emit native code (for now).
The flag is useful, for example, when you are PInvoking native DLLs for which you have only the x86 version.
In fact, you can also modify this bit later, using CorFlags
NGEN instead will compiler the IL to assembly code (native). It is called also AOT (Ahead Of Time) compilation, as opposed to JIT (Just In Time)

Lorenzo Dematté
- 7,638
- 3
- 37
- 77
-
See also, for comparison, mono AOT (which is not exactly the same of NGEN) http://www.mono-project.com/AOT – Lorenzo Dematté Mar 20 '13 at 12:26