c++ - 无法使用 QProcess 启动 g++

标签 c++ qt qprocess

我想使用 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();

关于c++ - 无法使用 QProcess 启动 g++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21722711/

相关文章:

c++ - 使用 qprocess 运行 .exe

c++ - Qt5.4.1升级到5.12.1,现在QCodec报异常

c++测试2组是否不相交

c++ - 将 QList 转换为 QVariant

c++ - 从 xml 文件输入并使用 rapidxml 解析

qt - 如何制作非阻塞、非模态的 QMessageBox?

c++ - 在带有 msvc2008 编译器的 Qt 中使用 afx_msg

c++ - QML listview读取当前和触摸区域

c++ - Qt 与 QAMQP/RabbitMQ