How to compile a .NET application to native code?

2019-01-03 08:13发布

If I want to run a .NET application in a machine where the .NET framework is not available; Is there any way to compile the application to native code?

11条回答
Summer. ? 凉城
2楼-- · 2019-01-03 08:44

You can use ngen.exe to generate a native image but you still have to distribute the original non-native code as well, and it still needs the framework installed on the target machine.

Which doesn't solve your problem, really.

查看更多
地球回转人心会变
3楼-- · 2019-01-03 08:48

Microsoft has announced its .NET Native Preview that will allow to run .NET applications without having the framework installed.

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

查看更多
祖国的老花朵
4楼-- · 2019-01-03 08:50

Microsoft has an article describing how you can Compile MSIL to Native Code

You can use Ngen.

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

Unfortunately, you still need the libraries from the framework in order to run your program. There's no feature that I know of with the MS .Net framework SDK that allows you to compile all the required files into a single executable

查看更多
小情绪 Triste *
5楼-- · 2019-01-03 08:51

You can do this using the new precompilation technology called .NET Native. Check it out here: http://msdn.microsoft.com/en-US/vstudio/dotnetnative

Currently it is only available for Windows Store Apps. It performs single component linking. So .NET Framework libraries are statically linked into your app. Everything is compiled to native and IL assemblies are no longer deployed. Apps do not run against CLR but a stripped down, optimized runtime called 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 utilise JIT but it does support various dynamic scenarios. Code authors would need to utilize Runtime Directives to provide hints to the .NET Native compiler on the dynamic scenarios they wish to support.

查看更多
放我归山
6楼-- · 2019-01-03 09:00

Yes, using Ngen, the Native Image Generator. There are, however, a number of things you need to be aware of:

  • You still need the CLR to run your executable.
  • The CLR will not dynamically optimize your assemblies based on the environment it's run in (e.g. 486 vs. 586 vs. 686, etc.)

All in all, it's only worth using Ngen if you need to reduce the startup time of your application.

查看更多
Anthone
7楼-- · 2019-01-03 09:00

The nature of .NET is to be able to install apps that have been compiled to MSIL, then either by JIT or Ngen, MSIL is compiled to native code and stored locally in a cache. It was never intended on generating a true native .exe that can be run independently of the .NET framework.

Maybe there's some hack that does this, but it doesn't sound safe to me. There are too many dynamics that require the framework, such as: dynamic assembly loading, MSIL code generation, etc.

查看更多
登录 后发表回答