开发者

How to Integrate C++ compiler in Visual Studio 2008

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 integ开发者_StackOverflow中文版rate 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
        }


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜