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

I want to programmatically compile C code from C #. I am trying, but have not yet found a solution. 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 use VS2010 on Windows 7, and I set mingw32-gcc and the environment variable for mingw32-gcc C: \ Program Files \ CodeBlocks \ MinGW \ bin Any help would be greatly appreciated. Thanks in advance.

+5
source share
2 answers

A call to cmd.exe is not required. 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 );

Hi

+2
source

Try

Process process = Process.Start(
         @"C:\Program Files\CodeBlocks\MinGW\bin\mingw32-gcc.exe", "-o a Test.c");
+8
source

All Articles