Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tjw09003

macrumors newbie
Original poster
Feb 11, 2010
13
0
(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();
 
*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()
 
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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.