(qt c++) Program code hangs when executed from terminal, works fine from Finder. why?

Discussion in 'Mac Programming' started by tjw09003, Feb 26, 2010.

  1. tjw09003 macrumors newbie

    Joined:
    Feb 11, 2010
    #1
    (qt c++) Program hangs when executed from terminal, works fine from Finder. why?


    This code doesn't work when executed from the terminal like this. It does nothing if I try to start "ls" in the code, and will hang the program if I try to start "gcc"

    ./helloworld.app/contents/macos/helloworld


    But will work fine when I double click on the app in Finder, why is that?

    Code:
        QString result = "Nothing";
    
        //(TEST)RUN COMMAND
        QProcess *proc = new QProcess(this);
        proc->start("ls /avr");
        proc->waitForFinished();
        result = proc->readAllStandardError();
    
     
  2. tjw09003 thread starter macrumors newbie

    Joined:
    Feb 11, 2010
    #2
    *facepalm*

    The ls does nothing ever because I'm only reading from stdERR. I feel stupid lol.


    But still, why would executing gcc hang?? Only when the app is started from terminal. But works fine from finder. It won't hang from terminal is I comment out the line waitforFinnished()
     
  3. mslide macrumors 6502a

    Joined:
    Sep 17, 2007
    #3
    We need more to go on. Can you give us a (small) complete example (that will compile as is... not just a small snippet from your program) that duplicates the problem?

    You say it hangs when you execute gcc. I'm assuming you mean you are using a QProcess to invoke gcc, right? You also said that it works when executed from finder. Are you sure it worked? Are you sure the gcc command you are invoking is good? Are you assuming you are in a certain directory when you start your QProcess? What *exactly* are you trying to execute that causes it to hang?

    Also, waitForFinished has a default timeout of 30 seconds. Are you sure it is really hanging and that the command you are trying to execute doesn't take longer than that (and that you didn't wait 30 seconds to see if it returns)? What I mean is perhaps it is working properly in terminal (maybe you're assuming you are in a certain directory) and it is not working when executed from finder (maybe it errors out right away).

    There's certainly nothing in Qt that stops you from using QProcess to build a c file with gcc. I was able to make a simple example that does just that. I used your exact code, stuck it in main() and did something like proc->start("g++ <my home dir>/foo.cpp -o <my home dir>/foo"). It worked when executed from the terminal and from finder.

    PS I'm assuming you were using QProcess to execute ls just to see if your program worked. If you really wanted to get a directory listing, there are better ways than having QProcess execute 'ls' (e.g. use QDir).
     

Share This Page