Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically compile a c code from a c# code using mingw32-gcc compiler

Tags:

c#

c#-4.0

I want to compile a C code from c# programmatically. I am trying but i have not found any solution yet. Here is my code.

try {
    var info = new ProcessStartInfo {
        FileName = "cmd.exe",
        Arguments = "mingw32-gcc -o a Test.c"
    };
    var process = new Process { StartInfo = info };
    bool start = process.Start();

    process.WaitForExit();
    if (start) {
        Console.WriteLine("done");
    }
} catch (Exception) {
    Console.WriteLine("Not done");
}

I am using VS2010 in windows 7 and I have installed mingw32-gcc and my environment variable for mingw32-gcc is C:\Program Files\CodeBlocks\MinGW\bin Any Help will be appreciated. Thanks in advance.

like image 826
SM Farhad Ali Avatar asked Mar 25 '12 21:03

SM Farhad Ali


People also ask

How C# code is compiled?

C# code is compiled into IL when you build your project. The IL is saved in a file on disk. When you run your program, the IL is compiled again, using the Just In Time (JIT) compiler (a process often called JIT'ing). The result is machine code, executed by the machine's processor.

What format does net C# bot code need to be compiled as?

Your source code is compiled into a byte code known as the common intermediate language (CIL) or MSIL (Microsoft Intermediate Language).

Which compiler is used in C#?

NET Compiler Platform, also known by its codename Roslyn, is a set of open-source compilers and code analysis APIs for C# and Visual Basic (VB.NET) languages from Microsoft.

How does GCC compiler work?

After the file is preprocessed, gcc moves it to the compiler. The compiler turns each line in the preprocessed file into assembly language, which are instructions in English mnemonics that have strong one-to-one correspondence to the machine code that computers can read.


2 Answers

Try

Process process = Process.Start(
         @"C:\Program Files\CodeBlocks\MinGW\bin\mingw32-gcc.exe", "-o a Test.c");
like image 167
Olivier Jacot-Descombes Avatar answered Nov 14 '22 23:11

Olivier Jacot-Descombes


Calling the cmd.exe program is not necessary. You can directly call the mingw32-gcc.exe program with arguments.

Edit:

string szMgwGCCPath = "C:\\mingw32\\bin\\mingw32-gcc.exe"; // Example of location
string szArguments = " -c main.c -o main.exe"; // Example of arguments
ProcessStartInfo gccStartInfo = new ProcessStartInfo(szMgwGCCPath , szArguments );
gccStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(gccStartInfo );

Regards

like image 22
grifos Avatar answered Nov 14 '22 22:11

grifos