views:

2544

answers:

12

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

+1  A: 

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

axk
A: 

Quite simply, no.

you might consider deleting this obviously unpopular and incorrect response. just sayin...
Sky Sanders
+13  A: 

Remotesoft has one: Salamander .NET Linker

I don't have any experience with it though.

OregonGhost
this question has been asked before and again this is the only correct answer. being, it is possible but at a price.
chrissie1
+13  A: 

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.

FlySwat
Why are ngen assemblies slower? ngen is invoked on the target machine, so that should not actually make a difference.Oh, and ngen is not the actual answer to the question.
OregonGhost
Ngen is invoked by the developer to precompile the app, not on the target machine. Please read the section on Ngen in CLR Via C#.
FlySwat
Jon, that depends. `ngen` can be invoked by the installation process on the target machine, and as far as I know this is in fact done for various Microsoft products implemented in .NET.
Konrad Rudolph
However, NGen can made into part of the application install process, allowing it to compile to the target machine, I'll edit my post.
FlySwat
EDIT to my comment: you're in fact wrong (and so was I), `ngen` *has* to be invoked on the target machine. `ngen` installs a native image in the *machine-local* GAC! Precompiling on the developer's machine would be useless.
Konrad Rudolph
Odd, that means CLR via C# is also incorrect.
FlySwat
I guess the most prominent example ngen'ing its files on the target machine is the .NET Framework 2.0 itself - next time you install it somewhere, take a look at the text it displays :)
OregonGhost
And don't forget paint.NET. When it states it optimizes for your computer during installation, it's using ngen.
Morten Christiansen
Also, NGEN can't take into account the current value of any static variables, where JIT can. We rely on the JIT to actually omit lines of code from the machine instruction based on the value of readonly static variables.
WaldenL
A: 

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.

Pablo Marambio
+5  A: 

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

Turnkey
+1  A: 

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...

Jacob
A: 

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

Adrian
+1  A: 

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

CodeChef
+1  A: 

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.

C. Dragon 76
+1  A: 

There is IL2CPU for the compilation part.

Sven Hecht
broken link (-1)
Luca Martinetti
fixed, strange thinks happen
Sven Hecht
A: 

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

LicenseQ