简体   繁体   English

如何在Visual Studio 2008中集成C ++编译器

[英]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 integrate C++ compiler using C# code to compile C++ code. 我只想知道如何使用C#代码集成C ++编译器来编译C ++代码。

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. 在我们的例子中,我们将一个学生C ++代码加载到文本区域中,然后单击按钮以编译该代码。 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. 我们能够使用内置编译器使用C#(将C#代码加载到C ++代码的文本区域)代码做到这一点。 But still not able to do for C# code. 但是仍然不能为C#代码做。

Can anyone suggest a method to do this? 有人可以建议一种方法吗? It is possible to integrate external compiler to VS C# code? 是否可以将外部编译器集成到VS C#代码? 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 这是“构建”按钮的代码,我们继续进行C#代码编译

CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("csharp"); CodeDomProvider codeProvider = CodeDomProvider.CreateProvider(“ csharp”); string Output = "Out.exe"; 字符串输出=“ Out.exe”; Button ButtonObject = (Button)sender; Button ButtonObject =(按钮)发送器;

        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++. 遗憾的是,CodeDom for C ++没有默认实现,如果您想使用与上述相同的代码来编译C ++,则可以始终定义自己的实现。

Or you can call cl.exe directly, In both cases you would have to manually invoke cl.exe 或者,您可以直接调用cl.exe,在这两种情况下,您都必须手动调用cl.exe

http://msdn.microsoft.com/en-us/library/19z1t1wy(v=VS.71).aspx 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. 将代码编写到临时文件中,调用cl.exe将所有输出通过管道传递到想要(或不希望)的窗口并结束,检查是否已生成exe,是否编译成功,并且可以运行exe。失败,该错误应该在您先前创建的日志中。

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 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. 这将编译提供给它的代码,但这只是一个示例,每次编译成功都会有一个文件“ Test.exe”可以运行。 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 希望这对更多有关正在运行的进程的信息有所帮助,请访问http://www.codeproject.com/KB/cs/ProcessStartDemo.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM