Ejemplo simple

To begin our introduction to gtkmm, we'll start with the simplest program possible. This program will create an empty 200 x 200 pixel window.

Código fuente

File: base.cc (For use with gtkmm 3, not gtkmm 2)

#include <gtkmm.h>

int main(int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);

  Gtk::Window window;

  Gtk::Main::run(window);

  return EXIT_SUCCESS;
}

Ahora se explicará cada línea del ejemplo

#include <gtkmm.h>

All gtkmm programs must include certain gtkmm headers; gtkmm.h includes the entire gtkmm kit. This is usually not a good idea, because it includes a megabyte or so of headers, but for simple programs, it suffices.

The next line:

Gtk::Main kit(argc, argv);
creates a Gtk::Main object. This is needed in all gtkmm applications. The constructor for this object initializes gtkmm, and checks the arguments passed to your application on the command line, looking for standard options such as -display. It takes these from the argument list, leaving anything it does not recognize for your application to parse or ignore. This ensures that all gtkmm applications accept the same set of standard arguments.

Las dos siguientes líneas de código crean y muestran una ventana:

Gtk::Window window;

La última línea muestra la ventana y entra al bucle principal de gtkmm, que terminará cuando la ventana se cierre.

Gtk::Main::run(window);

After putting the source code in simple.cc you can compile the above program with gcc using:

g++ simple.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs`
Note that you must surround the pkg-config invocation with backquotes. Backquotes cause the shell to execute the command inside them, and to use the command's output as part of the command line.