Chapter 3. Basics

# 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 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 Code

File: `base.cc` (For use with gtkmm 4)

    #include <gtkmm.h>

    class MyWindow : public Gtk::Window
    {
    public:
      MyWindow();
    };

    MyWindow::MyWindow()
    {
      set_title("Basic application");
      set_default_size(200, 200);
    }

    int main(int argc, char* argv[])
    {
      auto app = Gtk::Application::create("org.gtkmm.examples.base");

      return app->make_window_and_run<MyWindow>(argc, argv);
    }

We will now explain each part of the example

    #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 part of the program:

    class MyWindow : public Gtk::Window
    {
    public:
      MyWindow();
    };

    MyWindow::MyWindow()
    {
      set_title("Basic application");
      set_default_size(200, 200);
    }

defines the `MyWindow` class. Its default constructor sets the window's title and default (initial) size.

The `main()` function's first statement:

    auto app = Gtk::Application::create("org.gtkmm.examples.base");

creates a `Gtk::Application` object, stored in a `Glib::RefPtr` smartpointer. This is needed in all gtkmm applications. The `create()` method for this object initializes gtkmm.

The last line creates and shows a window and enters the gtkmm main processing loop, which will finish when the window is closed. Your `main()` function will then return with an appropriate success or error code. The `argc` and `argv` arguments, passed to your application on the command line, can be checked when `make_window_and_run()` is called, but this simple application does not use those arguments.

    return app->make_window_and_run<MyWindow>(argc, argv);

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

    g++ base.cc -o base `pkg-config --cflags --libs gtkmm-4.0` -std=c++17

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. Note also that `base.cc` must come before the pkg-config invocation on the command line. `-std=c++17` is necessary only if your compiler is not C++17 compliant by default.

Microsoft Windows |  | Headers and Linking