2

I created a simple Qt app to compile a C++ file using QProcess. Now when I try to run the exe file from the app using QProcess, it doesn't run. When I tried to open the file manually, I got an error saying "libwinpthread-1.dll is missing".

Here's how I compiled the file-

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

And, this is the code for running it -

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);

Basically, I want to compile and run a C++ file, provide it sample input file and store the standard output in a new file. What is wrong with this code? Or any other way of doing it? I am working on windows 7. Also, I cannot understand why does the compiled program need that dll file when compiled from the Qt app and runs fine when compiled manually.

Just in case, this is the file I am trying to compile

//file.cpp
#include <iostream>

int main() {
    std::string s;
    std::cin >> s;
    std::cout << s;
    return 0;
}
Shubham
  • 935
  • 1
  • 7
  • 25

2 Answers2

3

You are using API of QProcess in unexpected way. You need to create QStringList of args:

QStringList args;
args << fileName;
args << exeFileName;
...
compileProcess->start("g++", args);
yshurik
  • 772
  • 5
  • 12
  • I tried doing it that way but it didn't work due to spaces in file path. – Shubham Feb 12 '14 at 14:41
  • This way is actually better in handling spaces in arguments - no quotes needed, so you may have some another especiality with QProcess, try to check what is the PATH environment variable - then you may have some dlls not found – yshurik Feb 12 '14 at 14:45
  • @Shubham Are you absolutely sure that that's the case? Because it works for me. – Kuba hasn't forgotten Monica Feb 12 '14 at 14:45
  • @KubaOber Yes. My file was in a folder that had a space in its name. Didn't work – Shubham Feb 12 '14 at 14:47
  • @Shubham Again - it works for me on both Qt 4.8.5 and 5.2.1 on OS X. What version do you use, and on what platform? – Kuba hasn't forgotten Monica Feb 12 '14 at 14:50
  • @KubaOber I am using Qt 5.0.2 on windows 7. To specify the filename in arguments, i used `fileName` and `QFileInfo(fileName).path() + "/out.exe"` – Shubham Feb 12 '14 at 14:53
  • 1
    @KubaOber see this http://stackoverflow.com/questions/21722711/unable-to-start-g-using-qprocess?noredirect=1#comment32857165_21722711 – Shubham Feb 12 '14 at 14:57
  • @yshurik I still have the same problem on using `args`, the program compiles but doesn't run. I edited my question in response to that. – Shubham Feb 13 '14 at 06:36
0

I seem to get it working by providing -static option while compiling the file, the exe file produced runs fine without demanding any external .dll file. But still, It doesn't run from QProcess.

Shubham
  • 935
  • 1
  • 7
  • 25