0

I have a C# app that generates a .cpp file. I try to compile programatically that file, using the gcc compiler like that:

    private void button2_Click(object sender, EventArgs e)
    {
        Thread.Sleep(2000);
        Process process = Process.Start(@"C:\Program Files (x86)\CodeBlocks\MinGW\bin\mingw32-gcc.exe", "-o a x.cpp");
        Thread.Sleep(2000);
    } 

When I press the button2, after 2 seconds, appears a console (the gcc compiler) but it dissapears immediately so that I can't see it's contents.

I want to see if the code contained in x.cpp file was compiled succesfully or, if not, what are the errors generated by the gcc compiler.

Thank you respectfully.

Cosmin Ioniță
  • 3,598
  • 4
  • 23
  • 48
  • 1
    You might want to look into the documentation of the [ProcessStartInfo](http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx) class about redirecting the standard and error output channels, or just look here: [Capturing console output from a .NET application (C#)](http://stackoverflow.com/questions/186822/capturing-console-output-from-a-net-application-c) –  Apr 21 '14 at 17:10

1 Answers1

0

If you just need success/failure - check [exit code]http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode%28v=vs.110%29.aspx). Zero is generally success, non-zero is failure.

Redirection of output streams from console app is usual appoach to collect output.

Possibly easier to debug/validate approach would be to dump compiler to log to a file instead of console. Since GCC writes logs to standard output you can create CMD/BAT file to redirect logs to a file and run it instead of GCC

...gcc.exe %1 1>%2 2>%3

And call it with Process.Start. Note that you will need to deal with cleaning up log files. On other hand you know what in the log files and don't need to worry if you missed/incorrectly interpreted console output in C#.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • The same behaviour. The console blinks when I press the button. I don't know if those two lines are correct: process.StartInfo.FileName = "C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\mingw32-gcc.exe"; process.StartInfo.Arguments = "/r:System.dll /out:sample.exe x.cpp"; – Cosmin Ioniță Apr 21 '14 at 19:15
  • @IoniţăCosmin Edit your question with something like "also tried....". It is very hard to what you did if you just provide random part of code in comment... (Note that usually Unix tools don't use "/" and ":" in command line, "--" is more common... so check if your arguments are ok). – Alexei Levenkov Apr 21 '14 at 19:28