Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any C# to C converter tools? [closed]

Tags:

c

c#

I know C# is different from .NET Framework, C# is a programming language that standard by ECMA (ECMA-334) and ISO (ISO/IEC 23270).

I don't want a converter that converts ANY C# source code (including .NET Framework) to C, but I want a tool that converts an ECMA standard C# source code to ANSI C source code.

Something like java2c but for ECMA C#.

like image 500
Amir Saniyan Avatar asked Dec 05 '11 12:12

Amir Saniyan


2 Answers

There is not exactly such thing, but the Vala programming language is able to take a source code very similar to C#, and generate C codem or compile it directly.

http://live.gnome.org/Vala

Of course, the only problem are the libraries: C# has a lot of API's that you'll have to provide, or modify your source code to adapt to the Vala standard library.

If you wanted to translate this code to C because you need it compiled, there are other possibilities.

For example, ngen in the microsoft world:

http://msdn.microsoft.com/en-us/library/6t9t5wcf(v=vs.80).aspx

In the mono project, you can create a single exe file with the interpreter and the libraries. Look for mkbundle:

http://www.mono-project.com/Mono:Runtime

Mono is able to compile "ahead of time", i.e., generate the native code even before the program is going to be executed, so it will run faster.

http://www.mono-project.com/Mono:Runtime#Ahead-of-time_compilation

like image 62
Baltasarq Avatar answered Sep 30 '22 17:09

Baltasarq


As a proof-of-concept, I wrote a tool called universal-transpiler that converts a small subset of C# into C and several other languages.

For example, it can translate this function from C# to C:

public static double distance_formula(double x1,double y1,double x2,double y2){
    return Math.Sqrt(Math.Pow(x1-x2,2)+Math.Pow(y2-y1,2));
}

This is the equivalent C code that the translator would generate:

double distance_formula(double x1,double y1,double x2,double y2){
    return sqrt(pow(x1-x2,2)+pow(y2-y1,2));
}

I have not found any other C#-to-C compilers, but there is a C#-to-Lua compiler that could be combined with a Lua-to-C compiler to generate C source code.

It might also be possible to compile C# into WebAssembly using the Blazor compiler and then decompile it into C source code using wasm2c.

like image 20
Anderson Green Avatar answered Sep 30 '22 18:09

Anderson Green