<p>Let's say I want to run a .NET application on a machine where the .NET framework is not available; Is there any way to compile the application to native code?</p>

[{'answer_id': 45708, 'author': 'Chris Zwiryk', 'author_id': 734, 'author_profile': 'https://Stackoverflow.com/users/734', 'pm_score': 4, 'selected': False, 'text': '<p>Yes, using <a href="http://msdn.microsoft.com/en-us/library/6t9t5wcf.aspx" rel="noreferrer">Ngen</a>, the Native Image Generator. There are, however, a number of things you need to be aware of:</p>\n\n<ul>\n<li><strong>You still need the CLR to run your executable.</strong></li>\n<li>The CLR will not dynamically optimize your assemblies based on the environment it\'s run in (e.g. 486 vs. 586 vs. 686, etc.)</li>\n</ul>\n\n<p>All in all, it\'s only worth using Ngen if you need to reduce the startup time of your application.</p>\n'}, {'answer_id': 45710, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 7, 'selected': True, 'text': '<p>Microsoft has an article describing how you can <a href="http://msdn.microsoft.com/en-us/library/ht8ecch6(VS.71).aspx" rel="noreferrer">Compile MSIL to Native Code</a></p>\n\n<p>You can use <a href="http://msdn.microsoft.com/en-us/library/6t9t5wcf.aspx" rel="noreferrer">Ngen</a>.</p>\n\n<blockquote>\n  <p>The Native Image Generator (Ngen.exe)\n  is a tool that improves the\n  performance of managed applications.\n  Ngen.exe creates native images, which\n  are files containing compiled\n  processor-specific machine code, and\n  installs them into the native image\n  cache on the local computer. The\n  runtime can use native images from the\n  cache instead using the just-in-time\n  (JIT) compiler to compile the original\n  assembly.</p>\n</blockquote>\n\n<p>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</p>\n'}, {'answer_id': 45713, 'author': 'Simon Steele', 'author_id': 4591, 'author_profile': 'https://Stackoverflow.com/users/4591', 'pm_score': 5, 'selected': False, 'text': '<p>RemoteSoft makes a tool that compiles a .NET application into a package that can be run without .NET installed. I don\'t have any experience with it:</p>\n\n<p><a href="http://www.remotesoft.com/linker/intro.html" rel="noreferrer">RemoteSoft Salamander</a></p>\n'}, {'answer_id': 45714, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': -1, 'selected': False, 'text': '<p>I think it\'s not possible. You will need to distribute .NET FW as well. If you want to compile .NET app to native code, use NGen <a href="http://msdn.microsoft.com/en-us/library/6t9t5wcf.aspx" rel="nofollow noreferrer">tool</a></p>\n'}, {'answer_id': 45724, 'author': 'Matt Bishop', 'author_id': 4301, 'author_profile': 'https://Stackoverflow.com/users/4301', 'pm_score': 3, 'selected': False, 'text': "<p>You can use ngen.exe to generate a native image <strong><em>but</em></strong> you still have to distribute the original non-native code as well, and it still needs the framework installed on the target machine.</p>\n\n<p>Which doesn't solve your problem, really.</p>\n"}, {'answer_id': 45733, 'author': 'spoulson', 'author_id': 3347, 'author_profile': 'https://Stackoverflow.com/users/3347', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 45734, 'author': 'Erick Sgarbi', 'author_id': 4171, 'author_profile': 'https://Stackoverflow.com/users/4171', 'pm_score': 4, 'selected': False, 'text': '<p>I have tested several of them and at this moment the only one that supports .NET 3.5 and also has a great virtualization stack is <a href="http://www.xenocode.com/" rel="noreferrer">Xenocode</a> <a href="http://www.xenocode.com/Products/Postbuild/" rel="noreferrer">Postbuild</a></p>\n\n<p>With ngen you still need to have the .NET framework installed but using a tool as such all  your managed code is compiled into native code so you can deploy it to machines without the framework presence.</p>\n'}, {'answer_id': 9255339, 'author': 'Camilo Martin', 'author_id': 124119, 'author_profile': 'https://Stackoverflow.com/users/124119', 'pm_score': 3, 'selected': False, 'text': '<p>You can! However you\'re restricted to .NET 1.1 (no generics for you):\n<a href="http://www.mono-project.com/AOT" rel="noreferrer">Mono Ahead-Of-Time compilation (AOT)</a></p>\n\n<p>However, this means compiling is really native, so you\'ll no longer be able to deploy one single bytecode assembly, you\'ll need one per platform.</p>\n\n<p>It was originally designed because there\'s no .NET or Mono for iPhone, so that\'s how they made MonoTouch.</p>\n'}, {'answer_id': 22838985, 'author': 'thepirat000', 'author_id': 122195, 'author_profile': 'https://Stackoverflow.com/users/122195', 'pm_score': 4, 'selected': False, 'text': '<p>Microsoft has announced its <strong>.NET Native Preview</strong> that will allow to run .NET applications without having the framework installed.</p>\n\n<p>Take a look: <a href="http://blogs.msdn.com/b/dotnet/archive/2014/04/02/announcing-net-native-preview.aspx">http://blogs.msdn.com/b/dotnet/archive/2014/04/02/announcing-net-native-preview.aspx</a></p>\n\n<p>FAQ: <a href="http://msdn.microsoft.com/en-US/vstudio/dn642499.aspx">http://msdn.microsoft.com/en-US/vstudio/dn642499.aspx</a></p>\n\n<p>You can download Microsoft .NET Native for VS2013 from here: <a href="http://msdn.microsoft.com/en-US/vstudio/dotnetnative">http://msdn.microsoft.com/en-US/vstudio/dotnetnative</a></p>\n'}, {'answer_id': 23189503, 'author': 'm_eric', 'author_id': 1905886, 'author_profile': 'https://Stackoverflow.com/users/1905886', 'pm_score': 3, 'selected': False, 'text': '<p>You can do this using the new precompilation technology called .NET Native. Check it out here: <a href="http://msdn.microsoft.com/en-US/vstudio/dotnetnative">http://msdn.microsoft.com/en-US/vstudio/dotnetnative</a></p>\n\n<p>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)</p>\n\n<p>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  <a href="http://msdn.microsoft.com/en-us/library/dn600639%28v=vs.110%29.aspx">Runtime Directives</a> to provide hints to the .NET Native compiler on the dynamic scenarios they wish to support.   </p>\n'}, {'answer_id': 35467422, 'author': 'James Ko', 'author_id': 4077294, 'author_profile': 'https://Stackoverflow.com/users/4077294', 'pm_score': 5, 'selected': False, 'text': '<p>As some of the other answers here have mentioned, you can use the <a href="https://msdn.microsoft.com/en-us/vstudio/dotnetnative.aspx" rel="noreferrer">.NET Native</a> tool to compile your app to native machine code. Unlike those answers, however, I will explain <strong>how</strong> to do it.</p>\n\n<p>Steps:</p>\n\n<ol>\n<li><p>Install the <a href="http://dotnet.github.io/getting-started/" rel="noreferrer">dotnet CLI</a>  (command line interface) tool, which is part of the new .NET Core toolchain. We\'ll use this to compile our app; you can find a good article about it <a href="http://www.hanselman.com/blog/ExploringTheNewNETDotnetCommandLineInterfaceCLI.aspx" rel="noreferrer">here.</a></p></li>\n<li><p>Open up a shell prompt and <code>cd</code> to the directory of your app.</p></li>\n<li><p>Type this:</p>\n\n<pre><code>dotnet compile --native\n</code></pre></li>\n</ol>\n\n<p>That\'s it! When you\'re done, your app will be compiled down to a single binary, like this:</p>\n\n<p><img src="https://www.hanselman.com/blog/content/binary/Windows-Live-Writer/ef1a3ae5fa91_12832/image_729c4e69-567c-4713-956a-4ee0e8dcc12d.png" alt="Native compiled .NET Core EXE"/></p>\n\n<p>It\'ll be a standalone executable; no PDBs, assemblies, or config files included (hooray!).</p>\n\n<hr>\n\n<p>Alternatively, if you want an even faster program, you can run this:</p>\n\n<pre><code>dotnet compile --native --cpp\n</code></pre>\n\n<p>That will optimize your program using the C++ code generator (as opposed to RyuJIT), so your app is even more optimized for AOT scenarios.</p>\n\n<p>You can find more info on this at the dotnet CLI <a href="https://github.com/dotnet/cli/blob/56e3d8f903320ed721aa442e7b139e9df1512ffa/README.md#basic-usage" rel="noreferrer">GitHub repo</a>.</p>\n'}, {'answer_id': 55796754, 'author': 'kbridge4096', 'author_id': 4379906, 'author_profile': 'https://Stackoverflow.com/users/4379906', 'pm_score': 2, 'selected': False, 'text': '<p>2019 Answer: Use <a href="https://github.com/dotnet/corert" rel="nofollow noreferrer">dotnet/corert</a>. It can compile .NET Core projects into standalone <code>.exe</code> files. No dependencies (except for system libraries like <code>kernel32.dll</code>). I bet this is exactly what the OP need.</p>\n\n<p>From its GitHub home page:</p>\n\n<blockquote>\n  <p>The CoreRT compiler can compile a managed .NET Core application into a native (architecture specific) single-file executable that is easy to deploy. It can also produce standalone dynamic or static libraries that can be consumed by applications written in other programming languages.</p>\n</blockquote>\n'}, {'answer_id': 62744498, 'author': 'Rahim Surani', 'author_id': 2558031, 'author_profile': 'https://Stackoverflow.com/users/2558031', 'pm_score': 0, 'selected': False, 'text': "<p>The main reason to compile into Native is to secure your codes, otherwise the MSIL compiled is like deploying the source codes in the client's machine.</p>\n<p>NGEN compiles into native but also need to deploy IL codes, this purpose is just to reduce the startup time but it is also useless.</p>\n<p>CoreRt is alpha version and working only with simple helloworld type apps.</p>\n<p>.Net Core compiles into single executable files but it is also not native exe, this is just a zipped file of IL codes and it will unzip the codes into temp folder while running.</p>\n<p>My simple question from Microsoft is, if RyuJIT can compile IL into native on the fly then why not you can compile the same IL ahead-of-time (AOT).</p>\n"}, {'answer_id': 63705961, 'author': 'Rahim Surani', 'author_id': 2558031, 'author_profile': 'https://Stackoverflow.com/users/2558031', 'pm_score': -1, 'selected': False, 'text': '<p>try this (<a href="http://www.dotnetnative.online/" rel="nofollow noreferrer">http://www.dotnetnative.online/</a>) to compile .net compiled exe into native exe, I tried this, its new but good.</p>\n'}, {'answer_id': 64195933, 'author': 'user1005462', 'author_id': 1005462, 'author_profile': 'https://Stackoverflow.com/users/1005462', 'pm_score': 0, 'selected': False, 'text': '<p>Looks like net core RT workable solutions;\nsoon all apps will go to .net core;\n<a href="https://www.codeproject.com/Articles/5262251/Generate-Native-Executable-from-NET-Core-3-1-Proje?msg=5753507#xx5753507xx" rel="nofollow noreferrer">https://www.codeproject.com/Articles/5262251/Generate-Native-Executable-from-NET-Core-3-1-Proje?msg=5753507#xx5753507xx</a>\n<a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/november/net-core-publishing-options-with-net-core" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/november/net-core-publishing-options-with-net-core</a></p>\n<p>not tested maybe with old win .net sdk possible do similar.</p>\n'}, {'answer_id': 68139211, 'author': 'codevision', 'author_id': 354473, 'author_profile': 'https://Stackoverflow.com/users/354473', 'pm_score': 3, 'selected': False, 'text': '<p>You can use <a href="https://github.com/dotnet/runtimelab/tree/feature/NativeAOT" rel="nofollow noreferrer">NativeAOT</a> (part of .NET 7, formerly <a href="https://github.com/dotnet/corert" rel="nofollow noreferrer">CoreRT</a>).</p>\n<p>This technology is a bit limiting, since you cannot rely on reflection too much, but overall you can compile range of application with it. Web apps, WinForms apps, console apps.</p>\n<p>As of .NET 7 Preview 5 you can just add</p>\n<pre><code>&lt;PropertyGroup&gt;\n   &lt;PublishAot&gt;true&lt;/PublishAot&gt;\n&lt;/PropertyGroup&gt;\n</code></pre>\n<p>If you want use daily builds of NativeAOT you need add following lines in your project file</p>\n<pre class="lang-xml prettyprint-override"><code>&lt;ItemGroup&gt;\n  &lt;PackageReference Include=&quot;Microsoft.DotNet.ILCompiler&quot; Version=&quot;8.0.0-*&quot; /&gt;\n&lt;/ItemGroup&gt;\n</code></pre>\n<p>and add <code>dotnet8</code> Nuget feed into your nuget.config like that</p>\n<pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n&lt;configuration&gt;\n &lt;packageSources&gt;\n    &lt;!--To inherit the global NuGet package sources remove the &lt;clear/&gt; line below --&gt;\n    &lt;clear /&gt;\n    &lt;add key=&quot;dotnet-public&quot; value=&quot;https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json&quot; /&gt;\n    &lt;add key=&quot;dotnet8&quot; value=&quot;https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json&quot; /&gt;\n &lt;/packageSources&gt;\n&lt;/configuration&gt;\n</code></pre>\n<p>You can target .NET 6 apps, and with ILTrim improvements in .NET 6 the more and more code would be ready for native compilation.</p>\n<p>For simple applications you can try use <a href="https://github.com/MichalStrehovsky/bflat" rel="nofollow noreferrer">BFlat</a> which may give you even better results.</p>\n'}]
['https://Stackoverflow.com/questions/45702', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/184/']