2

I use qDebug() in eclipse in windows, which doesn't give me any output, it seems that Qt sends it to the debugger from Qt's documents below.

The Qt implementation of these functions prints the text to the stderr output under Unix/X11 and Mac OS X. With Windows, if it is a console application, the text is sent to console; otherwise, it is sent to the debugger.

My purpose is not how to print message in eclipse in windows.

My purpose is to know that why Qt doesn't choose to send debug message to std error stream in windows, while it actually sends debug message to std error stream in Mac Os.

Is there any differences between Windows and Mac Os?

Thanks for help.

jia hilegass
  • 493
  • 2
  • 12
  • 1
    Don't smap tags! This is not related to C! – too honest for this site Aug 24 '16 at 02:17
  • I just need to know some knowledge of output stream in windows. In c language, there are also some output expressions such as "printf", so i give it a tag "c", if there is anything wrong in my opinion, could you please tell me the reason in detail? – jia hilegass Aug 24 '16 at 02:29
  • @jiahilegass Detail: This is not a question about the C language for the same reason that it is not a question about every single language that happens to have a `printf`-style string format function. Let me know if you need any more details. – Jason C Aug 24 '16 at 03:34
  • Probably because `qDebug()` is debug output? ;) – The Compiler Aug 24 '16 at 04:13
  • I have edited my problem. – jia hilegass Aug 24 '16 at 04:37
  • I'd imagine Eclipse can tap into debug log under Windows... If not, switch IDEs! Or use another program to view the qDebug output, see the suggested duplicate. – hyde Aug 24 '16 at 04:56
  • @hyde. As i have said, i know how to figure it out in Eclipse, but it's not my purpose, what i want to know is mentioned above. But your suggest have make me interested, is there any way i can access debug log in Windows? – jia hilegass Aug 24 '16 at 05:04
  • @hyde. Why did you delete your answer? It seems what i want. – jia hilegass Aug 24 '16 at 05:33
  • @jiahilegass Undeleted with a note about possible differences between release and debug builds. – hyde Aug 24 '16 at 05:43

2 Answers2

2

Since question is actually "why" and not "how", the reason is twofold:

  • GUI programs under Windows don't normally have a console, they have to open it themselves (and especially release versions of course won't, that'd annoy users). They don't inherit the console of the process that starts them. Qt GUI programs want to behave like any other GUI programs under Windows, unless you explicitly specify otherwise (for example adding CONFIG+=console for qmake).

  • Debug output in Windows is usually done using the debug output feature of Windows (some practical info here in download page of DebugView tool), for example probably every Windows IDE supports it. Qt just follows this platform convention for software development.

In short, that's how it's done under Windows by default and by convention. If Qt did something different, then it would be necessary to have good reasons why.

The default behavior is different under Unix (and Linux), a child process inherits stdin, stdout and stderr to the TTY of the parent, unless extra measures are taken. And normally no special measures are taken by the programs themselves, it's up to the parent (for example by adding 2>/dev/null when starting the program from a shell/script).

Note: I did not have time to check if debug builds of Qt apps under Windows actually behave a bit differently and by default output to console, so take this into account when reading above.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • Are you a Qt programmer? Your rigorous attitude makes me admire. I am a one year old programmer who wants to do better in programming in China. It's my honor if i can take your email to communicate with some problem. – jia hilegass Aug 24 '16 at 07:42
0

You'd have to ask the Qt devs to find out for sure, but my guess is that they did it that way because under Windows, stdout and stderr are not typically visible while a GUI program is running.

Under Windows, if you want to see the stdout and/or stderr streams of your GUI app, you have to add some explicit code to open a console window and redirect the streams to it:

AllocConsole();
freopen(conOutStr, "w", stdout);
freopen(conOutStr, "w", stderr);

The Qt devs probably assumed that most Windows programmers would not do this, but would instead want to see their qDebug() output in their IDE/debugger window.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • Just like Qt's documents said, when i run my app in console, i can get the output, but if i run it in eclipse, nothing will happen, though i have redirected std error. As documents says, it will send message to the debugger if i don't run it in console, in actually, i do get message in DebugView(which is a software in windows). This is what makes me confused. Why does Qt choose to send it to the DebugView but not std error output in Windows? – jia hilegass Aug 24 '16 at 04:57
  • 1
    @jiahilegass: *"Why does Qt choose to send it to the DebugView but not std error output in Windows?"* - [OutputDebugString](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363362.aspx) doesn't **send** output anywhere. It writes it to a buffer, and any debugger can subscribe to notifications when new data arrives. This works for applications targeting any subsystem (e.g. WINDOWS, CONSOLE, NATIVE), or applications that do not have a GUI at all (e.g. services), and works across sessions. You won't get any of this using *stderr*, which really looks like a crude kludge in comparison. – IInspectable Aug 24 '16 at 13:08
  • @IInspectable:Thanks for help. As you say, any debugger can subscribe to notifications when data arrives. It means Eclipse's debugger can be informed of the “OutputDebugString”. But there is no output message in it. My system is Win7, and this case happeded before i have enabled "Debug Print Filter" in regedit, maybe it's the reason that Eclipse's debugger hasn't been informed. So i will give it a test after enabling "Debug Print Filter" tomorrow. – jia hilegass Aug 24 '16 at 14:30
  • @jiahilegass: Eclipse is not a debugger, it's an IDE. I don't know what debugger Eclipse's CDT plugin (I assume, that's what you have) is using. If it is using the native Windows debugger (or a custom debug engine implemented on top of it), it should be capable of receiving `OutputDebugString` output, as long as the debugger is attached to the application. If in doubt, you can always use [DebugView](https://technet.microsoft.com/en-us/sysinternals/debugview.aspx) to see the output. – IInspectable Aug 24 '16 at 14:51
  • @IInspectable:Thanks for help again. I know how to receive OutputDebugString. What i really want to know is just the concepts that all of you have told me. For example, you make me understand that if PyDev(Eclipse's Python Plugin)'s Debugger implemented native Windows debugger, the OutputDebugString should always be there. – jia hilegass Aug 24 '16 at 15:02