1

I beginner for programming. So can you please show me how to pass values for your compile() method.

class CL
{

    private const string clexe = @"cl.exe";
    private const string exe = "Test.exe", file = "test.cpp";
    private string args;
    public CL(String[] args)
    {
        this.args = String.Join(" ", args);
        this.args += (args.Length > 0 ? " " : "") + "/Fe" + exe + " " + file;
    }   

    public Boolean Compile(String content, ref string errors)
    {
        //remove any old copies
        if (File.Exists(exe))
            File.Delete(exe);
        if (File.Exists(file))
            File.Delete(file);

        File.WriteAllText(file, content);

        Process proc = new Process();
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.FileName = clexe;
        proc.StartInfo.Arguments = this.args;
        proc.StartInfo.CreateNoWindow = true;

        proc.Start();
        //errors += proc.StandardError.ReadToEnd();
        errors += proc.StandardOutput.ReadToEnd();

        proc.WaitForExit();

        bool success = File.Exists(exe);

        return success;
    }
}
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
Lucas
  • 167
  • 3
  • 15
  • For more information..... http://stackoverflow.com/questions/3036238/how-to-integrate-c-compiler-in-visual-studio-2008 – Lucas Jun 15 '10 at 06:01

2 Answers2

1
public Boolean Compile(String content, ref string errors) 

Do you want to know how to call this? Try . . .

string content = "#include <stdio.h>\nmain(){\nprintf(\"Hello world\");\n}\n";
string errors = "";

CL k = new CL(new string[2] {"/Od", "/C"});
if(k.Compile(content, ref errors))
   Console.WriteLine("Success!");
else
   Console.WriteLine("Failure: {0}", errors);

Hope this helps

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • Thanks for helping me.... :). But it throws this error... Error 1 No overload for method 'CL' takes '0' arguments – Lucas Jun 15 '10 at 06:20
  • Hi, Still i unable to compile it. Following error occured............... No overload for method 'CL' takes '0' arguments – Lucas Jun 15 '10 at 06:42
  • Apologies, I missed the args on the constructor for CL. I added two spurious ones (/Od = disable optimizations, /C = don't strip comments). – Binary Worrier Jun 15 '10 at 07:32
  • I done it as you mentioned. But still cannot compile. Following error occured : Error 1 No overload for method 'CL' takes '2' arguments – Lucas Jun 15 '10 at 07:41
  • 2
    The constructor takes an array of strings (i.e. 1 parameter). Either change the constructor signature to be public CL(params String[] args) or change the call to be new CL(new string[] { }) – bruceboughton Jun 15 '10 at 07:55
1

Create a form using the designer, add a text box name it txtCplusplus and a button. Add a click event for the button.

Paste your CL class into the same file as event handler (form.cs or whatever you call it), NOT inside a method or property.

In the buttons click event handler put this code:

      CL cmp = New CL();
       string errs;   
       if (cmp.Compile(txtCplusplus.Text, ref errs) {
        MessageBox.Show("Success");
       } else {
         MessageBox.Show(errs);
       }
jmoreno
  • 12,752
  • 4
  • 60
  • 91
  • Thanks for helping me.... :). But it throws this error... Error 1 No overload for method 'CL' takes '0' arguments – Lucas Jun 15 '10 at 06:29
  • CL isn't a method, it's a class. Which means that you don't have the class properly defined. Hmn, looking at what you posted, I bet you have the lines "CL k = new CL () k .Compile ();" included. If so those two lines are the problem. Get rid of them. – jmoreno Jun 15 '10 at 06:47
  • OK, I think I see the problem. Change: CL cmp = new CL(); to CL cmp = new CL([]); – jmoreno Jun 15 '10 at 06:54
  • If the above doesn't work, then try: string[] noargs; CL cmp = new CL(noargs); – jmoreno Jun 15 '10 at 07:00
  • Both Didn't work, in your second answer it throws flowing error Error 1 Use of unassigned local variable 'noargs' – Lucas Jun 15 '10 at 07:11
  • 1
    The constructor takes an array of strings (i.e. 1 parameter). Either change the constructor signature to be public CL(params String[] args) or change the call to be new CL(new string[] { }) – bruceboughton Jun 15 '10 at 07:58
  • Sorry, meant: string[] noargs =new string[] {}; – jmoreno Jun 15 '10 at 15:37