I've been building some classes and functions in a static library which has the familiar printing of text messages to stdout:

cout << "Hello World!" << endl;

I've been developing this library for use in a console application but I'm considering also trying to make a GUI version, because it will be both useful and highly educational (for me).

I wonder how is it possible to make output in a library sent to both stdout and GUI controls (like a status bar or a popup window). Since such messages are all sent (in my case anyway) to one destination (cout) could some sort of redirection be possible?

I've always presumed something like this may be possible as people often release command line and GUI versions of their programs.

  • Did you try to google "cout GUI programs"? Getting plenty of answers there. Though I think the question is right on-topic for this site.
    – Doc Brown
    Commented Jun 19, 2014 at 9:06

2 Answers 2


cout always writes to the standard output stream (stdout). If you want the output to be redirected elsewhere (e.g. a graphical window) you will have to pipe the output to another program or thread responsible for handling the graphical interface. There are many ways this can be done but it depends on the operating system somewhat.

By default, on Linux you can observe the stdout of both console and graphical programs just by running it from a terminal. In fact, there's really no clear-cut distinction between "console" and "graphical" programs in Linux.

On Windows it's rather different: console and graphical programs are distinct*. Graphical programs do not have a terminal allocated by default, so stdout messages usually end up in oblivion. There is a little bit of trickery required to allocate a terminal for graphical programs (so that they behave similar to Linux).

(I can't comment on Macs as I don't use them.)

You can do redirections externally via the shell. Here I assume a Bourne-like shell.** Say you want to run a program my-simulation and feed its output to another program my-display, which presumably reads from the standard input (stdin). If you run the program as is, the output will be dumped into the terminal as usual:


You can establish a pipe by running the following in the shell:

./my-simulation | ./my-display

Or if you want to write to a file instead,

./my-simulation >my-output-file.txt

Of course, these can all be done in C/C++, but with more work. If you want stdout to be redirected to a file, you can use freopen from the standard library. Redirecting stdout to another process is slightly more difficult as you need to use platform-specific API to deal with processes and piping file descriptors.

[*] In Windows lingo, graphical programs are called Windows applications. You can find more about the various differences between these two kinds of programs on the MSDN.

[**] You can also do something similar with Command Prompt on Windows, but with a slightly different syntax.


What I would do is output what you want into an osstream and with this osstream i can use it to display the output on whatever front end i want, be it console or window message, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.