Is it possible to compile .NET IL code to machine

2019-01-17 00:35发布

问题:

I would like to distribute my .NET programs without the .NET framework. Is it possible to compile a .NET program to machine code?

回答1:

Yes, you can precompile using Ngen.exe, however this does not remove the CLR dependence.

You must still ship the IL assemblies as well, the only benefit of Ngen is that your application can start without invoking the JIT, so you get a real fast startup time.

According to CLR Via C#:

Also, assemblies precompiled using Ngen are usually slower than JIT'ed assemblies because the JIT compiler can optimize to the targets machine (32-bit? 64-bit? Special registers? etc), while NGEN will just produce a baseline compilation.

EDIT:

There is some debate on the above info from CLR Via C#, as some say that you are required to run Ngen on the target machine only as part of the install process.



回答2:

Remotesoft has one: Salamander .NET Linker

I don't have any experience with it though.



回答3:

There are some third party tools that do this, e.g.

  • http://www.remotesoft.com/linker/
  • http://www.xenocode.com/


回答4:

Another (expensive and proprietary, licenses start at $1599) product that can do this is Xenocode Postbuild. Haven't used it myself though, with it costing about the gross national product of a small African country and all...



回答5:

Is it possible to compile .NET IL code to machine code?

Yes, but the .NET Framework does it for you at runtime (default) or at install time (ngen). Among other reasons, this IL -> machine code is done separately on each install machine so it can be optimized for that particular machine.

I would like to distribute my .NET programs without the .NET framework. Is it possible to compile a .NET program to machine code?

No, for all intents and purposes you cannot do this. The 3rd party workarounds may work in some scenarios, but by then I wouldn't really consider it "managed code" or ".NET" anymore.



回答6:

Yes, you can now! Recently Microsoft announced .NET Native, a piece of technology that compiles your application with .NET Native runtime (they call it MRT) into one binary (actually one executable and one dynamic library).

See links: http://blogs.msdn.com/b/dotnet/archive/2014/04/02/announcing-net-native-preview.aspx http://blogs.msdn.com/b/dotnet/archive/2014/04/24/dotnetnative-performance.aspx



回答7:

There is IL2CPU for the compilation part.



回答8:

I don't think that it is possible. You can make a prejitted assembly but you still need the framework.



回答9:

If you just concerned with the size of deploying the Framework, you might read up on this.



回答10:

I think you should not: That's the task of the JIT compiler.

However, you could use ClickOnce or Windows Installer to deploy it so that the missing framework isn't such a big problem: you could tell the installer to download the Framework and install it.



回答11:

Look at MONO project: mono command with --aot option. http://www.mono-project.com/AOT



回答12:

I'd always thought it would be cool to compile c# to machine code directly without the CLR dependency though....