[英]Compile C++ code via C# class in Visual Studio
I use this method to compile C++ file in VS. 我使用这种方法在VS中编译C ++文件。 But even i provide the correct file it returns false.
但是,即使我提供了正确的文件,它也会返回false。 Can any one help me... This is class called CL
谁能帮助我...这是一个叫做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;
}
}
This is my button click event 这是我的按钮点击事件
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);
}
In your Visual Studio installation folder there should be the following path: 在Visual Studio安装文件夹中,应具有以下路径:
VC\bin\x86_amd64\1033\1033
There should be a clui.dll
in this path. 此路径中应该有一个
clui.dll
。 Copy it to the parent folder ( VC\\bin\\x86_amd64\\1033
). 将其复制到父文件夹(
VC\\bin\\x86_amd64\\1033
)。 This should solve your problem. 这应该可以解决您的问题。
I took the solution from http://connect.microsoft.com/VisualStudio/feedback/details/108528/command-line-issue-when-building-for-64bit-in-32bit-with-cl-exe : 我从http://connect.microsoft.com/VisualStudio/feedback/details/108528/command-line-issue-when-building-for-64bit-in-32bit-with-cl-exe中获取了该解决方案:
Maybe it is not relevant, but I think you miss a space in your command line... 也许无关紧要,但我认为您在命令行中错过了一个空格...
this.args += (args.Length > 0 ? " " : "") + "/Fe" + exe + " " + file;
right after the "/Fe"
在
"/Fe"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.