2010-06-15 73 views
1

我是编程的初学者。那么你能告诉我如何为你的compile()方法传递值。传递方法的值

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

欲了解更多信息..... http://stackoverflow.com/questions/3036238/how-to-integrate-c- compiler-in-visual-studio-2008 – Kasun 2010-06-15 06:01:09

回答

1
public Boolean Compile(String content, ref string errors) 

你想知道如何称呼呢?尝试。 。 。

string content = "#include <stdio.h>\nmain(){\nprintf(\"Hello world\");\n}\n"; 
string errors = ""; 

CL k = new CL(new string[2] {"/Od", "/C"}); 
if(k.Compile(content, ref errors)) 
    Console.WriteLine("Success!"); 
else 
    Console.WriteLine("Failure: {0}", errors); 

希望这有助于

+0

感谢您的帮助.... :)。但它会引发此错误... 错误方法'CL'没有重载'0'参数 – Kasun 2010-06-15 06:20:39

+0

嗨,仍然无法编译它。发生以下错误............... 方法'CL'没有超载需要'0'参数 – Kasun 2010-06-15 06:42:25

+0

道歉,我错过了CL的构造函数的参数。我添加了两个伪造的(/ Od =禁用优化,/ C =不删除注释)。 – 2010-06-15 07:32:28

1

使用设计器创建一个表单,添加一个文本框将其命名为txtCplusplus和一个按钮。为该按钮添加一个点击事件。

将您的CL类粘贴到与事件处理程序(form.cs或其他任何您称之为的内容)相同的文件中,而不是在方法或属性中。

中的按钮单击事件处理程序把这个代码:

 
     CL cmp = New CL(); 
     string errs; 
     if (cmp.Compile(txtCplusplus.Text, ref errs) { 
     MessageBox.Show("Success"); 
     } else { 
     MessageBox.Show(errs); 
     } 
+0

感谢您的帮助.... :)。但它会抛出这个错误...错误1方法'CL'没有超载需要'0'参数 – Kasun 2010-06-15 06:29:46

+0

CL不是一种方法,它是一个类。这意味着你没有正确定义类。 Hmn,看看你发布的内容,我敢打赌,你有行“CL k = new CL() k .Compile();”包括在内。如果是这样,那两条线就是问题所在。摆脱他们。 – jmoreno 2010-06-15 06:47:09

+0

我按你所说的完成了。但仍然是同样的问题.. – Kasun 2010-06-15 06:50:04