0

Can someone help me with this issue?

I currently working on my project for final year of my honors degree. And we are developing a application to evaluate programming assignments of student ( for 1st year student level)

I just want to know how to integrate C++ compiler using C# code to compile C++ code.

In our case we are loading a student C++ code into text area, then with a click on button we want to compile the code. And if there any compilation errors it will be displayed on text area nearby. (Interface is attached herewith.)

And finally it able to execute the code if there aren't any compilation errors. And results will be displayed in console.

We were able to do this with a C#(C# code will be loaded to text area intead of C++ code) code using inbuilt compiler. But still not able to do for C# code.

Can anyone suggest a method to do this? It is possible to integrate external compiler to VS C# code? If possible how to achieve it?

Very grateful if anyone will contributing to solve this matter?

This is code for Build button which we proceed with C# code compiling

CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("csharp"); string Output = "Out.exe"; Button ButtonObject = (Button)sender;

        rtbresult.Text = "";
        System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
        //Make sure we generate an EXE, not a DLL
        parameters.GenerateExecutable = true;
        parameters.OutputAssembly = Output;
        CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, rtbcode.Text);

        if (results.Errors.Count > 0)
        {

            rtbresult.ForeColor = Color.Red;
            foreach (CompilerError CompErr in results.Errors)
            {
                rtbresult.Text = rtbresult.Text +
                            "Line number " + CompErr.Line +
                            ", Error Number: " + CompErr.ErrorNumber +
                            ", '" + CompErr.ErrorText + ";" +
                            Environment.NewLine + Environment.NewLine;
            }
        }
        else
        {
            //Successful Compile
            rtbresult.ForeColor = Color.Blue;
            rtbresult.Text = "Success!";
            //If we clicked run then launch our EXE
            if (ButtonObject.Text == "Run") Process.Start(Output); // Run button
        }
Lucas
  • 167
  • 3
  • 15

1 Answers1

2

there is unfortunately no default implementation for CodeDom for C++, you can always define your own if you want to use the same code as the above to compile C++.

Or you can call cl.exe directly, In both cases you would have to manually invoke cl.exe

http://msdn.microsoft.com/en-us/library/19z1t1wy(v=VS.71).aspx

It shouldn't that hard. write the code to a temporary file, call cl.exe pipe any output to a window you want (or not) and the end, check if a exe has been produced, if it has compilation succeeded and you can run the exe, if not it failed and the error should be in the log you created earlier.

It's less structured than above but it's by far the easiest way.

-- more detailed

the following code assumes your environment vars are properly set. http://msdn.microsoft.com/en-us/library/f2ccy3wt(VS.80).aspx

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;
    }
}

this will compile the code given to it, but it's just a sample, everytime compilation succeeds there will be a file "Test.exe" which you can run. when it fails the "errors" variable will contain the error message.

hope this helps, for more information on running processes, take a look at http://www.codeproject.com/KB/cs/ProcessStartDemo.aspx

Phyx
  • 2,697
  • 1
  • 20
  • 35
  • Thanks for the help. But can u provide little example code so i can understand it very well. – Lucas Jun 14 '10 at 11:26
  • Wow.... You done a great job. Thanx for the help. But one small thing. I beginner for programming. So can you please show me how to pass values for your compile() methode. CL k = new CL () k .Compile (); – Lucas Jun 15 '10 at 04:12
  • Ok. I done it this way.. CL k = new CL(new string[] { }); if (k.Compile(content, ref errors)) MessageBox.Show("Success"); else MessageBox.Show("Errors are : ", errors); But even i pass the correct C++ file it still returns false – Lucas Jun 15 '10 at 08:04
  • ah, sorry, stackoverflow never send me any notifications about your comments. the way you use it is correct when you don't have any parameters to pass it. I think you're still getting false because you're paths are mostlikely not set correctly and it's failing to either run cl or to find cl's dependencies. one way to check this is to do on a command line cl.exe /FeTest.exe and see what that returns, the error you get there is the reason it's failing. to set your paths correctly check out the http://msdn.microsoft.com/en-us/library/f2ccy3wt(VS.80).aspx link, after you run it – Phyx Jun 17 '10 at 09:05
  • you have to copy the values from CL, INCLUDE , LINK, LIB, PATH, and TMP. into your user env. I have to get going now, but i'll post later with a version that'll set these automatically. – Phyx Jun 17 '10 at 09:06