Problems with providing arguments to QProcess



  • I am trying to execute a c program from a QT application. I use the following code
    for the same but it does not work.

    QProcess *process = new QProcess();
    QString program = "sh";
    QString str1 = "gcc";
    QString str2 = "/home/Desktop/QtTest/basic.c";
    QString res = str1+ " " + str2;
    QStringList arguments;
    arguments << "-c" << res;
    process->start(program,arguments);
    
    if (!process->waitForStarted())
    {
    	qDebug("error in start");	
        return false;
    }
    process->closeWriteChannel();
    if (!process->waitForFinished())
    {
    	qDebug("error in finnished");	
        return false;
    } 
    

    But if I use --help as argument instead of filepath the command is executed successfully.

    QProcess *process = new QProcess();
    QString program = "sh";
    QString str1 = "gcc";
    QString str2 = "--help";
    QString res = str1+ " " + str2;
    QStringList arguments;
    arguments << "-c" << res;
    process->start(program,arguments);
    
    if (!process->waitForStarted())
    {
    	qDebug("error in start");	
        return false;
    }
    process->closeWriteChannel();
    if (!process->waitForFinished())
    {
    	qDebug("error in finnished");	
        return false;
    } 
    

    I am not able to understand why such behaviour and what is wrong on my side.
    As I know, I am missing something here.


  • Moderators

    First - why do you execute sh instead gcc directly?
    Second - the arguments should be passed as separate arguments instead manually concatenated - otherwise they are not recognized as two separate arguments.



  • I tried executing gcc directly, but I am facing the same problem as asked previously.

       QProcess process = new QProcess(); 
       QString program = "gcc";
       QStringList arguments;
       arguments << "--help";// "/home/sanket/Desktop/QtTest/basic.c";
       process->start(program,arguments);
    

    the gcc --help command is executed, but gcc along with the commented line as argument returns an empty string for readAllStandardOutput and readAllStandardError functions.

    1. How can I pass separate arguments to start function as it takes two arguments one as QString and other as QStringList

  • Qt Champions 2017

    Hi
    First of all. if gcc can just compile the file. nothing is shown. no output at all.
    Fooled me at first.

    This code works for me and show any syntax errors if i introduce one.

    #include <QMessageBox>
    #include <QProcess>
    int main(int argc, char* argv[]) {
      QApplication a(argc, argv);
      QProcess process;
      QString program = "gcc";
      QStringList arguments;
      process.setProcessChannelMode(QProcess::MergedChannels);
      arguments << "-Wall" << "/home/master/test.cpp" << "-o" << "hello";
      process.start(program, arguments);
      process.waitForFinished(); // sets current thread to sleep and waits for process end ( lacks check)
      QString output(process.readAllStandardOutput());
      QMessageBox::warning(0, "output", output );
    
      MainWindow w;
      w.show();
    
      return a.exec();
    }
    
    

    test.cpp is

    #include <stdio.h>
    ddd
    int
    main (void)
    {
      printf ("Hello, world!\n");
      return 0;
    }
    

    and this is what sample says:
    alt text



  • Hi,

    Sorry for not mentioning it correctly.
    I was trying to say, that there is no output file (.out file ) generated.
    But now everything seems to work fine.

    Thank you for the hep


  • Qt Champions 2017

    @Thanos

    thanks for your feedback.

    So please mark this topic as SOLVED now.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.