2010-06-14 103 views
0

有人可以帮我解决这个问题吗?如何在Visual Studio 2008中集成C++编译器

我目前正在致力于我的项目,获得我的荣誉学位的最后一年。我们正在开发一个应用程序来评估学生的编程作业(第一年级学生)

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

在我们的例子中,我们将一个学生的C++代码加载到文本区域,然后点击按钮我们想编译代码。如果有任何编译错误,它将显示在附近的文本区域。 (接口附带)

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

使用内置编译器,我们可以用C#(C#代码将加载到C++代码的文本区域intead)代码执行此操作。但仍然无法为C#代码做。

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

非常感谢,如果有人会为解决此事做出贡献?

这是为生成按钮的代码,我们用C#代码编译

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 
     } 

回答

2

有不幸的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/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; 
    } 
} 

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

希望这有助于为 上运行的进程的详细信息,看看http://www.codeproject.com/KB/cs/ProcessStartDemo.aspx

+0

感谢您的帮助。但是你能提供一些示例代码,所以我可以很好地理解它。 – Kasun 2010-06-14 11:26:36

+0

增加了一些示例代码 – Phyx 2010-06-14 13:12:20

+0

哇....你做得很好。感谢Thanx的帮助。但一件小事。我是编程初学者。那么你能告诉我如何为你的compile()方法传递值。 CL k = new CL() k .Compile(); – Kasun 2010-06-15 04:12:15

相关问题