Boyan
Boyan

Reputation: 456

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.

Upvotes: 4

Views: 1140

Answers (5)

Ciprian Khlud
Ciprian Khlud

Reputation: 444

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

Upvotes: 2

DanielCuadra
DanielCuadra

Reputation: 1030

I have been using the Portable Class Libraries (PCL) and they work great. Basically, it's subset of .Net that can run on multiple devices (it was specifically designed for this very purpose).

You can use Linq, async/await, lambdas, etc., so you have all the syntax power of C# available.

The only caveat is that many of the .Net libraries are not available (like Cryptography); the workaround is usually finding a PCL-ported version of the library on Nuget, or replicate some of the .Net functionality by yourself. - This limitation is actually encouraged by Microsoft, as they claim your PCL should use dependency injection from your other libraries in order to enable the missing features. For example, your XBox project might implement the encryption libraries that are missing on the PCL, and your PCL project should be injected to use XBox's classes on runtime (through interfaces, etc); later you could do something similar for Android, and your Android library would inject the encryption library into the PLC, the but PLC logic itself wouldn't need to change and would be the same both for the XBox and Android projects.

Here's more info about it http://msdn.microsoft.com/en-us/library/vstudio/gg597391(v=vs.110).aspx

Upvotes: 0

Mr. Smith
Mr. Smith

Reputation: 4516

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.

Upvotes: 3

Alecu
Alecu

Reputation: 2708

Actually some tools to convert the c# code into c++ code exists, actually the il instructions into c++ code. Then you can compile the c++ code directly. But it is currently in development and it might take a while for it to be usable.

It is called Code Refractor and it is available from here:http://coderefractor.blogspot.ro/search?updated-min=2014-01-01T00:00:00-08:00&updated-max=2015-01-01T00:00:00-08:00&max-results=10

Upvotes: -1

BradleyDotNET
BradleyDotNET

Reputation: 61369

Its a .NET app, its going to be dependent on the .NET runtime. Just a few things it provides:

  1. A HUGE number of assemblies in the GAC that every .NET application uses (think the System namespaces, among many others)
  2. The JIT compiler to turn IL into executable code.
  3. Registry keys and other required system modifications.

Compiling all that into a single application isn't practical, desirable, or really possible. You might as well package the .NET framework with your app and just install it (the recommended approach). I believe you even need to install with mono (for much the same reason I'm sure).

I know you were looking for a different solution, but the real answer is that .NET does not support this type of compilation.

Upvotes: -1

Related Questions