How to compile a .NET application in native code?

If I want to run a .NET application on a computer where the .NET platform is not available; Is there a way to compile the application for native code?

+82
compilation native-code
Sep 05 '08 at 12:46
source share
12 answers

Microsoft has an article describing how you can compile MSIL for native code.

You can use Ngen .

Custom Image Generator (Ngen.exe) is a tool that improves the performance of managed applications. Ngen.exe creates its own images, which are files containing the compiled processor code of the machine, and installs them in the original image cache on the local computer. runtime can use native images from the cache instead of using just-in-time (JIT) to compile the original assembly.

Unfortunately, you still need libraries from the framework to run your program. There is no function that I know with the MS.Net framework SDK that allows you to compile all the necessary files into a single executable

+40
Sep 05 '08 at 12:50
source share

RemoteSoft creates a tool that compiles a .NET application into a package that can be run without .NET installed. I do not have experience:

RemoteSoft Salamander

+23
Sep 05 '08 at 12:51
source share

I tested several of them, and at the moment the only thing that supports .NET 3.5, and also has an excellent Xenocode Postbuild virtualization stack

With ngen, you still need to install the .NET platform, but with the tool as such, all of your managed code is compiled into native code so that you can deploy it to machines without the presence of a framework.

+19
Sep 05 '08 at 13:02
source share

Microsoft has announced its .NET Native Preview , which will allow you to run .NET applications without installing the framework.

Take a look: http://blogs.msdn.com/b/dotnet/archive/2014/04/02/announcing-net-native-preview.aspx

FAQ: http://msdn.microsoft.com/en-US/vstudio/dn642499.aspx

You can download Microsoft.NET Native for VS2013 from here: http://msdn.microsoft.com/en-US/vstudio/dotnetnative

+17
Apr 03 '14 at 13:17
source share

Yes, using Ngen , Own Image Generator. However, there are a number of things you need to know about:

  • CLR is still required to run the executable file.
  • The CLR will not dynamically optimize your builds based on the environment in which it works (for example, 486 versus 586 versus 686, etc.).

In general, you only need to use Ngen if you need to reduce the startup time of your application.

+11
Sep 05 '08 at 12:50
source share

You can! However, you are limited to .NET 1.1 (there are no generics for you): Compiling Mono Ahead-Of-Time (AOT)

However, this means that the compilation is indeed native, so you can no longer deploy a single bytecode assembly, you will need one per platform.

It was originally developed because there is no .NET or Mono for the iPhone, so they made MonoTouch.

+9
Feb 13 2018-12-12T00:
source share

As mentioned in some other answers, you can use .NET Native to compile your application into native machine code. However, unlike these answers, I will explain how to do this.

Steps:

  • Install the dotnet CLI tool (command line interface), which is part of the new .NET Core key binding. We will use this to compile our application; You can find a good article about it here.

  • Open a shell and cd prompt in your application directory.

  • Enter this:

     dotnet compile --native 

What is it! When you are done, your application will be compiled to a single binary file, for example:

Native compiled .NET Core EXE

This will be a standalone executable; no PDBs, assemblies or configuration files (hooray!).




Alternatively, if you want an even faster program, you can run this:

 dotnet compile --native --cpp 

This optimizes your program using a C ++ code generator (unlike RyuJIT), so your application is even more optimized for AOT scripts.

Further information on this can be found in the dotnet CLI GitHub repo .

+8
Feb 17 '16 at 20:48
source share

You can do this using a new pre-compilation technology called .NET Native. Have a look here: http://msdn.microsoft.com/en-US/vstudio/dotnetnative

It is currently only available for Windows Store apps. It performs one-component binding. As such, the .NET Framework libraries are statically linked to your application. Everything is compiled to build native and IL, which are no longer used. Applications do not run in the CLR, but an optimized runtime optimized under the control of the Managed Runtime (Mrt.dll)

As stated above, NGEN used a mix compilation model and relied on IL and JIT for dynamic scenarios..NET Native does not use JIT, but supports various dynamic scenarios. Code authors should use Runtime Directives to give tips to the .NET Native compiler in the dynamic scripts that they want to support.

+6
Apr 21 '14 at 1:28
source share

You can use ngen.exe to create your own image , but you still have to distribute the non-native source code, and it still needs the infrastructure installed on the target machine.

This will not solve your problem.

+4
Sep 05 '08 at 12:56
source share

The nature of .NET is the ability to install applications that have been compiled into MSIL, either using JIT or Ngen, MSIL is compiled into native code and stored locally in the cache. It was never intended to create a genuine native .exe that can be run independently of the .NET platform.

Maybe there is some kind of hack that does this, but it does not seem safe to me. There are too many dynamics that require such a structure as: dynamic assembly loading, MSIL code generation, etc.

+1
Sep 05 '08 at 13:02
source share

I think it's impossible. You will also need to distribute .NET FW. If you want to compile a .NET application for your own code, use the NGen tool

-2
Sep 05 '08 at 12:51
source share

Starting in 2012, you can now use the "portable class library" integrated into VS 2012.

-3
Sep 08 '12 at 10:11
source share



All Articles