无法使用 QProcess 运行简单的控制台程序

Cannot run a simple console program using QProcess

本文关键字:简单 控制台 程序 运行 QProcess      更新时间:2023-10-16

我创建了一个简单的Qt应用程序来编译一个C++文件。现在,当我尝试使用 QProcess 从应用程序运行 exe 文件时,它无法运行。当我尝试手动打开文件时,我收到一个错误,说"libwinpthread-1.dll丢失"。

这是我编译文件的方式-

QFileInfo finfo(fileName);
exeFileName = QFileInfo( QDir(finfo.path()), finfo.baseName() + ".exe").filePath();
QStringList arguments;
arguments << fileName << "-o" << exeFileName;
process->start(QString("g++"), arguments);

而且,这是运行它的代码 -

QProcess *runProcess = new QProcess(this);
runProcess->setStandardInputFile(inputFilename);
runProcess->setStandardOutputFile(QFileInfo(exeFileName).path() + "/output.txt");
connect(runProcess, SIGNAL(finished(int)), this, SLOT(runComplete(int)));
runProcess->start(exeFileName);

基本上,我想编译并运行一个C++文件,为其提供示例输入文件并将标准输出存储在新文件中。这段代码有什么问题?还是其他方法?我正在Windows 7上工作。另外,我不明白为什么编译的程序在从Qt应用程序编译时需要该dll文件,并且在手动编译时运行良好。

以防万一,这是我正在尝试编译的文件

//file.cpp
#include <iostream>
int main() {
    std::string s;
    std::cin >> s;
    std::cout << s;
    return 0;
}

您正在以意想不到的方式使用 QProcess 的 API。您需要创建参数的 QString列表:

QStringList args;
args << fileName;
args << exeFileName;
...
compileProcess->start("g++", args);

我似乎通过在编译文件时提供-static选项来让它工作,生成的exe文件运行良好,不需要任何外部.dll文件。但是,它仍然不会从QProcess运行。