我用这个方法在VS中编译C++文件。但即使我提供了正确的文件,它也会返回 false。谁能帮我... 这是名为 CL 的类
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)
{
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;
}
}
这是我的按钮点击事件
private void button1_Click(object sender, EventArgs e)
{
string content = "#include <stdio.h>\nmain(){\nprintf(\"Hello world\");\n}\n";
string errors = "";
CL k = new CL(new string[] { });
if (k.Compile(content, ref errors))
Console.WriteLine("Success!");
else
MessageBox.Show("Errors are : ", errors);
}
最佳答案
在您的 Visual Studio 安装文件夹中应该有以下路径:
VC\bin\x86_amd64\1033\1033
该路径下应该有一个clui.dll
。将其复制到父文件夹 (VC\bin\x86_amd64\1033
)。这应该可以解决您的问题。
关于c# - 在 Visual Studio 中通过 C# 类编译 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3043487/