Search code examples
c#c++windowsunity-game-engineil2cpp

Can I use IL2CPP (Intermediate Language To C++) for non-Unity applications?


IL2CPP.exe is a Unity utility for converting C# IL code to C++. My question is: can this executable be used outside of the Unity game-development environment as a general-purpose tool for converting any .NET application (not just games) to a high-performance native executable?

Although I do know some C++, It would certainly be nice to be able write all kinds of programs in a language I am comfortable and fluent with (C#)......whether they be audio/video/music-production DAWs or OS-level security/forensics tools or machine-learning platforms or anything else that's resource-intensive.......and know that they will run as efficiently as an app written in straight C++.


Solution

  • Update 2023:

    Based on @StackHola update, it is possible to generate C++ code from C# using IL2CPP Command Line, but you would probably not get any performance benefits doing so, see my Long answer from 2021.

    Short answer: NO

    IL2CPP is tightly connected to the Unity environment and it's not possible to use it outside of Unity. You would need to write your own converter(?) to do such a thing.

    Longer answer

    IL2CPP doesn't do any magic in terms of performance improvement. Comparing C++ with C# with IL2CPP code should give (almost - more below) no performance benefit.

    IL2CPP is performant compared to C# code written for Unity specifically for few reasons that have nothing to do with C++.

    Why Unity is unique and needs IL2CPP:

    • Unity API is very heavily reliant on main thread performance, as the whole Unity API was written almost 10 years ago, where 2 Core CPUs were top-notch and everyone believed that we will have 20-50GHz single-core CPUs by now.
    • Unity makes a lot of assumptions that you will use their API for everything, begging from IO to Threading and GPU access, which is heavily bound to C++ core.
    • Unity needs to be wrapped with Unity objects (MonoBehaviours and GameObjects) to be used for almost anything, you cannot write your own native anything. (This is a simplification)
    • Unity is written in C++, so it needs to do something very similar to Marshalling, and it's not very efficient.

    So why IL2CPP?

    • Unity cannot convert its already very legacy backend (Mono) and its legacy API to be multithreaded since Mono have a lot of assumptions about your code that are not easily convertible to "simple" unity API.
    • Unity core is written in C++, so they are eliminating any form of Marshalling all together by skipping Mono "translator".
    • IL2CPP converts highly inefficient C#, single-threaded code to multithreaded C++, where possible, and it does this by analyzing IL code.

    Is it worth converting other C# to C++?

    No! Compare any arbitrary, optimized C# code that was precompiled by AOT to (modern) C++. You should get the same performance! Identical I would say.

    C# is compiled to IL (Intermediate Language) which as the name suggests is Intermediate. It's converted in runtime to Native Binary code (only when needed), that is what C++ is compiled into. You can force this conversion by skipping IL generation by running Ahead of Time compilation (AOT).

    The ONLY thing that your C# code will be less performant is when you are abusing GC's ability to clean up after you.