无法使用 QProcess 启动 g++

Unable to start g++ using QProcess

本文关键字:启动 g++ QProcess      更新时间:2023-10-16

我想使用 QProcess 从 Qt 应用程序编译一个 c++ 文件。但它不起作用,我没有看到编译器生成的任何 .o 或 .exe 文件。

这就是我正在做的——

QProcess *process = new QProcess(this);
QString program = "g++";
QStringList arguments;
//fileName is fetched from QFileDialog
arguments << fileName << "-o" << QFileInfo(fileName).path() + QFileInfo(fileName).baseName() + ".exe";
errorFilename = QFileInfo(fileName).baseName() + "_error.txt";
process->setStandardOutputFile(errorFilename);
connect(process, SIGNAL(finished(int)), this, SLOT(compiled()));
process->start(program, arguments);

请告诉我这段代码有什么问题。我正在Windows 7上工作。

请记住

,错误不会stdout,而是stderr。尝试使用:

process->setStandardErrorFile(errorFilename);

此外,QFileInfo::path()末尾没有路径分隔符,因此在将路径与基本文件名连接时需要添加一个:

QFileInfo finfo(fileName);
arguments << fileName << "-o" << QFileInfo( QDir(finfo.path()), finfo.baseName() + ".exe").filePath();