Re: [Mingw-users] How to redirect the printf in a GUI program under
GDB

> Is there any method to let the printf output *redirect* to GDB console?
There are two ways:
- Change your executable into a console executable. Either relink it
without the -mwindows switch or run some suitable tool on the .exe
file that changes the header field in question. (Yes, it is just a
single header field that says whether an .exe is "console" or "gui".)
The only such tool I know off-hand is editbin.exe (part of MSVC,
presumably also present in the no-cost "express" edition), but as such
it is a relatively trivial task and would require not much code at
all.
- Add code to attach the parent process console:
AttachConsole(ATTACH_PARENT_PROCESS). After this you probably need to
do freopen("CONOUT$", "w", stdout). (I am too lazy to actually test
right now.) And you probably should first check if stdout currently
actually is attached to an invalid handle before redirecting it with
freopen, so that your program won't keep printing to the console even
if run with stdout redirected with ">" or a pipe.
--tml

Thread view

Hi, I'm debugging a wxWidgets program, under GDB( in codeblocks).
Some of my code were copied from a console program, so there are some
code like printf output.
My question is:
Is there any method to let the printf output *redirect* to GDB console?
I'm searching the whole mingw maillists and found no answers.
By the way, I have tried using *trace()* , but it was failed in
compiling with "undefined identifier".
Any suggestions?
Thanks.

> Is there any method to let the printf output *redirect* to GDB console?
There are two ways:
- Change your executable into a console executable. Either relink it
without the -mwindows switch or run some suitable tool on the .exe
file that changes the header field in question. (Yes, it is just a
single header field that says whether an .exe is "console" or "gui".)
The only such tool I know off-hand is editbin.exe (part of MSVC,
presumably also present in the no-cost "express" edition), but as such
it is a relatively trivial task and would require not much code at
all.
- Add code to attach the parent process console:
AttachConsole(ATTACH_PARENT_PROCESS). After this you probably need to
do freopen("CONOUT$", "w", stdout). (I am too lazy to actually test
right now.) And you probably should first check if stdout currently
actually is attached to an invalid handle before redirecting it with
freopen, so that your program won't keep printing to the console even
if run with stdout redirected with ">" or a pipe.
--tml

Tor Lillqvist wrote:
>> Is there any method to let the printf output *redirect* to GDB console?
>
> There are two ways:
>
> - Change your executable into a console executable. Either relink it
> without the -mwindows switch or run some suitable tool on the .exe
> file that changes the header field in question. (Yes, it is just a
> single header field that says whether an .exe is "console" or "gui".)
> The only such tool I know off-hand is editbin.exe (part of MSVC,
> presumably also present in the no-cost "express" edition), but as such
> it is a relatively trivial task and would require not much code at
> all.
>
> - Add code to attach the parent process console:
> AttachConsole(ATTACH_PARENT_PROCESS). After this you probably need to
> do freopen("CONOUT$", "w", stdout). (I am too lazy to actually test
> right now.) And you probably should first check if stdout currently
> actually is attached to an invalid handle before redirecting it with
> freopen, so that your program won't keep printing to the console even
> if run with stdout redirected with ">" or a pipe.
>
> --tml
Thanks Tor for your help.
I follow your first method. Finally, I found it is quite easy task to do
this in codeblocks and MinGW.
For a record, Here is the steps:
1,Assume your project was generated by a wxWidgets wizard.
2,Go to the Menu->project->properties dialog.
3, select the build target tab
4, Change the Target type from "Gui applications" to "console applications".
5, Check on the "Pause when execution ends"
6, rebuild your program.
7, All the "printf" can be viewed from a console window behind your
wxWigets GUIs.
Thanks!