如何整合C ++编译器在Visual Studio 2008 [英] How to Integrate C++ compiler in Visual Studio 2008

查看:167
本文介绍了如何整合C ++编译器在Visual Studio 2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我这个问题?



我目前正在对我的项目我的荣誉学位的最后一年。我们正在开发一个应用程序,以评估编程的学生分配(一年级学生的水平)



我只是想知道如何使用C#代码编译C集成C ++编译器++代码。



在我们的例子中,我们是要装入学生的C ++代码转换成文本区域,然后用按钮点击我们要编译代码。如果有任何编译错误,将在文本区域附近的显示。 (接口附后)。



最后,如果没有任何编译错误,能够执行代码。而结果将显示在控制台中。



我们能够用C#做到这一点使用内置的编译器(C#代码将被载入到这一翻译的C ++代码文本区)代码。但仍然无法为C#代码做的。



任何人都可以提出来做到这一点的方法?是可能的外部的编译器集成到VS C#代码?如果可能的话如何实现呢?



非常感谢,如果有人将有助于解决这个问题?



这是为Build按钮的代码,我们用C#代码进行编译



CodeDomProvider codeProvider = CodeDomProvider.CreateProvider(CSHARP);
字符串输出=Out.exe;
键ButtonObject =(按钮)发送;

  rtbresult.Text =; 
System.CodeDom.Compiler.CompilerParameters参数=新CompilerParameters();
//确保我们生成一个EXE,而不是一个DLL
parameters.GenerateExecutable = TRUE;
parameters.OutputAssembly =输出;
CompilerResults结果= codeProvider.CompileAssemblyFromSource(参数,rtbcode.Text);

如果(results.Errors.Count大于0)
{

rtbresult.ForeColor = Color.Red;
的foreach(CompilerError CompErr在results.Errors)
{
rtbresult.Text = rtbresult.Text +
行号+ CompErr.Line +
,错误编号:+ CompErr.ErrorNumber +
,'+ CompErr.ErrorText +; +
Environment.NewLine + Environment.NewLine;
}
}
,否则
{
//编译成功
rtbresult.ForeColor = Color.Blue;
rtbresult.Text =成功了!
//如果我们点击运行,然后启动我们的EXE
如果(ButtonObject.Text ==运行)的Process.Start(输出); //运行按钮
}


解决方案

有不幸的是CodeDom中没有默认实现了C ++,你可以随时,如果你想使用相同的代码上面编译C ++定义自己。



或者你也可以拨打CL直接的.exe,在这两种情况下,你必须手动调用cl.exe时



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



应该不会那么难。代码写入到一个临时文件,调用cl.exe时管道的任何输出到你想要的(或没有)一个窗口,结束后,检查是否一个exe已经产生,如果有编译成功了,就可以运行exe文件,如果没有它失败,错误应该在日志中,您先前创建。



这是不太结构比上面,但它是迄今为止最简单的方法。



- 更详细



下面的代码假定您的环境瓦尔设置正确。 http://msdn.microsoft.com/en-us/库/ f2ccy3wt(VS.80)的.aspx

 类CL 
{
私人常量字符串clexe = @cl.exe时;
私人常量字符串exe文件=将Test.exe,文件=TEST.CPP;
私人字符串ARGS;
公共CL(字串[] args)
{
this.args =的string.join(,参数);
this.args + =(args.Length大于0?:)+/ FE+ EXE ++文件;
}

公共布尔编译(字符串的内容,参考字符串错误)
{
//删除任何旧的副本
如果(File.Exists(EXE ))
File.Delete(EXE);
如果(File.Exists(文件))
File.Delete(文件);

File.WriteAllText(文件,内容);

过程PROC =新工艺();
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();
//错误+ = proc.StandardError.ReadToEnd();
错误+ = proc.StandardOutput.ReadToEnd();

proc.WaitForExit();

BOOL成功= File.Exists(EXE);

返回成功;
}
}

这将编译给它的代码,但它的只是一个样本,每次编译成功会有一个文件将Test.exe,它可以运行。当它失败的错误变量将包含错误信息。



希望这可以帮助,
的详细信息,正在运行的进程,看看<一个HREF =htt​​p://www.codeproject.com/KB/cs/ProcessStartDemo.aspx相对=nofollow> http://www.codeproject.com/KB/cs/ProcessStartDemo.aspx


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
        }

解决方案

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

这篇关于如何整合C ++编译器在Visual Studio 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆