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.
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.
Your source code is compiled into a byte code known as the common intermediate language (CIL) or MSIL (Microsoft Intermediate Language).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With