简体   繁体   中英

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

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.

Try

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM