C#.net Process调用g++.exe执行.\g++.exe file.cpp -o file
时间: 2023-12-06 08:03:01 浏览: 98
可以使用Process类来调用g++编译器来编译一个C++源文件。可以参考下面的代码:
```
string compilerPath = "g++.exe";
string sourceFile = "file.cpp";
string outputFile = "file.exe";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = compilerPath;
startInfo.Arguments = $"{sourceFile} -o {outputFile}";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (process.ExitCode == 0)
{
Console.WriteLine("Compilation succeeded.");
}
else
{
Console.WriteLine("Compilation failed. Output:");
Console.WriteLine(output);
}
```
上述代码将调用g++编译器来编译file.cpp源文件,并生成名为file.exe的可执行文件。如果编译成功,将输出"Compilation succeeded.",否则将输出"Compilation failed. Output:"和编译器的输出信息。
阅读全文