Chapter 3. Basics
Prev |  | Next

## Chapter 3. Basics

This chapter will introduce some of the most important aspects of gtkmm coding. These will be demonstrated with simple working example code. However, this is just a taster, so you need to look at the other chapters for more substantial information.

Your existing knowledge of C++ will be help you with gtkmm as it would with any library. Unless we state otherwise, you can expect gtkmm classes to behave like any other C++ class, and you can expect to use your existing C++ techniques with gtkmm classes.

## Simple Example

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.

Source location: examples/base/base.cc

    #include <gtkmm.h>

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

        Gtk::Window window;

        Gtk::Main::run(window);

        return 0;
    }

We will now explain each line of the example

    #include <gtkmm.h>

All gtkmm programs must include certain gtkmm headers; `gtkmm.h` includes the entire gtkmm kit, which is usually not a good idea, since 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.

The next two lines of code create and display a window:

      Gtk::Window window;

The last line shows the window and enters the gtkmm main processing loop, which will finish when the window is closed.

      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-2.4 --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.

Although we have shown the compilation command for this simple example, you really should use the automake and autoconf tools, as described in "Autoconf, Automake, Libtool", by G. V. Vaughan et al. The examples used in this book are included in the gtkmm package, with appropriate build files, so we won't show the build commands in future. You'll just need to find the appropriate directory and type `make`.

To simplify compilation, we use `pkg-config`, which is present in all (properly installed) gtkmm installations. This program 'knows' what compiler switches are needed to compile programs that use gtkmm. The `--cflags` option causes `pkg-config` to output a list of include directories for the compiler to look in; the `--libs` option requests the list of libraries for the compiler to link with and the directories to find them in. Try running it from your shell-prompt to see the results on your system.

Prev | Up | Next
Microsoft Windows | Home | Widgets