0

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;
}
Shubham
  • 935
  • 1
  • 7
  • 25
  • 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 Feb 13 '14 at 11:03
  • `readAllStandardError` doesn't return any information. And I verified that `exeFileName` points to the correct file. – Shubham Feb 13 '14 at 11:12
  • 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 Feb 13 '14 at 11:16
  • There's a working directory method: http://qt-project.org/doc/qt-5.0/qtcore/qprocess.html#setWorkingDirectory basically QProcess sets it to the directory your application is running from, but occasionally it can cause problems. – Nicholas Smith Feb 13 '14 at 11:24
  • Okay, but `exeFileName` has the absolute path to the file, even then QProcess can't find the file? – Shubham Feb 13 '14 at 11:27
  • If it has the absolute path it *should* be fine. You might need to do `QDir::toNativeSeparators(exeFileName)` to convert to Windows formatting thinking about it. – Nicholas Smith Feb 13 '14 at 11:49

1 Answers1

0

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 + "\"");
Shubham
  • 935
  • 1
  • 7
  • 25