0
votes

I compiled a c++ source file from the Qt app I am creating. Now I want to run the exe file generated and also to redirect its input and output to txt files. But when I try to run it from QProcess, it fails to execute with exit code -2.

This is how I compiled the file using QProcess -

arguments << fileName << "-o" << exeFileName << "-static";
connect(compileProcess, SIGNAL(finished(int)), this, SLOT(compiled()));
compileProcess->start(QString("g++"), arguments);

And this is how I run the exe from QProcess in the slot compiled() -

runProcess->setStandardInputFile(inputFilename);
runProcess->setStandardOutputFile(QFileInfo(exeFileName).path() + "/output.txt");
int code = runProcess->execute(exeFileName); //code = -2

The program runs fine when I start it manually. So, why can't it be started from QProcess? I am working with Qt 5.0.2 on Windows 7

This is the source file I am compiling -

#include <iostream>

int main() {
    std::string s;s
    std::cin >> s;
    std::cout << s;
    return 0;
}
1
If you add a readAllStandardError and print the contents, does it have any information? The -2 is usually it fails to either open the executable with the correct permissions, or it can't find the executable so also double check that the exeFileName is pointing to the correct place. - Nicholas Smith
readAllStandardError doesn't return any information. And I verified that exeFileName points to the correct file. - Shubham
the only other thing I can think of is the QProcess is setting to the application working directory rather than looking at the exeFileName path. - Nicholas Smith
There's a working directory method: qt-project.org/doc/qt-5.0/qtcore/… basically QProcess sets it to the directory your application is running from, but occasionally it can cause problems. - Nicholas Smith
Okay, but exeFileName has the absolute path to the file, even then QProcess can't find the file? - Shubham

1 Answers

0
votes

I finally got it to work. The exe file path had spaces in it and Qt did not implicitly add quotes around it. Adding quotes explicitly did the job.

runProcess->start("\"" + exeFileName + "\"");