Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling a .NET assembly into given target platform binary

I've been looking for a while for a way to compile a .NET assembly for a specified target platform. The goal is to have the IL and the whole assembly compiled into an independent of the .NET runtime, standalone executable.

I've read a lot of articles and comments on why it can't be done but I'm curious - can anyone come up with some idea?

UPDATE: Microsoft announced the ".NET Native" preview available. See here. According to the FAQ:

Q: How does linking work? Is framework code compiled into the application?

A: Yes, framework code will be compiled into the application

Now that sounds exciting. I wonder about BuildDefinitions and custom optimisations (currently: VC++ opt. is used) though.

like image 677
Boyan Avatar asked Oct 17 '25 16:10

Boyan


2 Answers

If your .NET assembly is able to run on Mono, it is possible to use Mono to produce an executable that runs without requiring the end-user to have either the .NET Framework or Mono. And infact, there are several developers I know of (including myself) who are doing this (primarily for the *nix target platforms, but it can be done for Windows too).

First thing to note though, the .NET Framework 2.0 has been included as part of the Windows installation since Windows XP. If you can target that framework, very few Windows users will need to install the .NET Framework to run your application. I would pursue this option, if at all possible.

If that's not possible, then I would use Mono's mkbundle tool. mkbundle produces a native executable on the platform it's ran on. Unfortunately, I don't have exact steps for you for running it on Windows; I've only used it on Linux and Mac.

like image 176
Mr. Smith Avatar answered Oct 20 '25 06:10

Mr. Smith


I am not allowed to comment (I have no 50 points of reputation), but I'm the creator of CodeRefractor so I want to clarify about this project:

  • I think that Mono and --full-aot flag is a better target than CodeRefractor will be as Mono is commercially supported and it has on the face value more resources (read: developers that work on all components of the runtime) and a longer history

  • CR is a free/hobby project and it will remain as such as I don't see many contributors outside of myself (in fact none, excluding comments: please add this, or does CR handle that). For this reason alone, I was interested mostly about how to optimize CIL opcodes, and less to be a solid implementation of .Net. Also, I will use the project design as a bachelor paper

  • for a very math intensive code and if you want to profile C/C++ generated code, CodeRefractor can help you here, but the actual runtime library is very limited. You may need to write your own PInvoke libraries to stream in and out the data. Which is to be honest, a bit hard!

  • CR has some good performance defaults: if the code follow some simple rules, will likely have performance in the range of Java's Server compiler, or a hand-optimized C++. One of the reasons is also because CR doesn't check the range of loops and has no exceptions. So, if you are lucky, you can get really good performance!

  • CodeRefractor has interesting optimizations, at least on theoretical side. This means that you can get really good performance if you really try to understand which optimizations are performed. It works very similar with LTO in GCC compiler and are multi tiered optimizations that run especially well if you use constants, they will be optimized out including the dead code!

  • CodeRefractor as target is to remove the .Net dependency by offering a (somewhat) C++ readable code that can be at least copy/pasted to your C++ code or to tweak it if needed. This also means that there will be always some edge cases that C++ will not map correctly the CIL code, like in area of multi-threading (with volatiles)

So as the final answer: look into Mono and --full-aot flag. It would be great if CodeRefractor will work for you, but I would be really impressed if it does as for now! Maybe in 3-4 years from now if people will back it (or a company, who knows), otherwise just read it for blog, as most people do.

If you are interested about design of CR, you can read it from documentation folder: https://github.com/ciplogic/CodeRefractor/blob/master/Documentation/BarchelorPaper2014.docx

like image 23
Ciprian Khlud Avatar answered Oct 20 '25 05:10

Ciprian Khlud