dirk/C++/gtkmm/close-con

Weingarten, 10-01-13

close-con – Close Console

An X11 application on a *ix system always has the opportunity to drop messages to the console. This can be, for example, the shell where the application has started from. This is totally unusual for MS Windows users.

GTK+ applications compiled in MS Visual C++ open a console beside of the actual window. This is caused by the kind how GTK+/gtkmm applications are built in MS Visual C++ – as Win32 Console Application. That's useful for debugging purposes but may confuse end users. I tried the Win32 Windows Application instead but it didn't work.

I searched the W3 for a solution. All I found, was a hint to close the console.

Source Code

Text Fileclose-con.cc
/** @file sample for a graphical application closing its console in
 * MS Windows
 *
 * Last CVS checkin:
 * $Date: $
 * $Author: scheff $
 */

/**************************************************************************/

// Gnome header:
#include <gtkmm/main.h>
#include <gtkmm/window.h>

#ifdef WIN32
// Win32 header:
#include <windows.h>
#endif // WIN32

/**************************************************************************/

/** provides the main function of application.
 *
 * @param argc number of command line arguments
 * @param argv pointer to array of pointers to strings with command line
 *        arguments
 * @return 0 ... application exited regularly\n
 *         else ... execution of application aborted
 */
int main(int argc, char *argv[])
{
#ifdef WIN32
  FreeConsole();
#endif // WIN32
  Gtk::Main(argc, argv).run(Gtk::Window());
  // done
  return 0;
}

Conclusion

Later, I realized that (even on MS Windows) it is quite useful to have a console available for debug output. Thus, I added checks for the NDEBUG macro to close the console in the Release built and keep it open in the Debug built.

Files

Text Fileclose-con.ccC++ source code
MS Visual C++ 2008 Project Fileclose-con.vcprojMS Visual C++ 2008 project file

Last modified: Sun Feb 21 22:00:10 2010