C++ Gtkmm Tutorial 1

C++ Gtkmm Tutorial 1

5 Comments on C++ Gtkmm Tutorial 1

This will be the first in a series of C++ Gtkmm tutorials leading up to a simple game. This tutorial will explain how to set up the development environment, create a Gtkmm application window, resize the window and rename it.

IDE Configuration
The IDE (integrated development environment) that this tutorial series will be using is Code::Blocks. It has code completion, is very easy to use and is available in the Ubuntu repositories.

sudo apt-get install codeblocks

Other IDE’s can be used and I know there will be debate on which IDE is the best but this tutorial is focused on getting started with Gtkmm not IDE choice. To understand how the IDE works we will start with an empty project.

Select from the drop down menu File, New then Project. Select the empty project template.

Skip the wizard welcome page. You will be now be able to name your project.

Make sure the GNU GCC Compiler is selected for Linux applications.

When the wizard ends, select New then Empty File from the drop down menu. A dialog will ask you if you want to add the file to the active project, select yes. When you name the file, make sure it has the extension cpp. example: “main.cpp”.

Right click the project and select build options. Below I have called it “tutorial1″ (highlighted in yellow).

We now have to add all of the compiler and linker flags. This can be done easily using pkg-config. Add the following to both debug and release.

In Compiler settings, Other options. Add the following line:

`pkg-config gtkmm-3.0 --cflags`


In Linker settings, Other linker options. Add the following line:

`pkg-config gtkmm-3.0 --libs`


When the compiler and linker settings have been completed for both release and debug, close the dialog. The IDE is now set up to program Gtkmm programs.

The Code
Copy or type the following code into the empty cpp file.

#include <gtkmm.h>

int main(int argc, char *argv[])
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.gtkmm.tutorial1.base");

    Gtk::Window window;

    window.set_default_size(400, 200);
    window.set_title("Tutorial 1");

    return app->run(window);

This code will create a Gtkmm application, create a GTK window, resize the window and name it.

Explaining The Code

#include <gtkmm.h>

This will load the definitions of the Gtkmm functions so they can be used in the application.

int main(int argc, char *argv[])

This is a function and because it has the name main, it is the entry point of the application or in other words the first function run when the application is run. Everything in this function will be between the braces (curly brackets {}).

Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.gtkmm.tutorial1.base");

This will create a Gtkmm application and store it in the pointer app. The arguments argc and argv are also given to the Gtkmm application.

Gtk::Window window;

Here we define the window and call it window.

window.set_default_size(400, 200);

This is where the window is given the width of 400 and the height of 200.

window.set_title("Tutorial 1");

The window is given a name of “Tutorial 1″.

return app->run(window);

This is the main Gtkmm loop. The function is given the window so it can be created and handle all of the control buttons such as minimize, maximize and close. The run function will also return a result which is then returned by the main function.

To Come
Tutorial 2 will explain how to use containers and create widget objects that are managed by Gtkmm.

About the author:

Bill Payne started working as a paid professional software developer at the young age of 12 years old developing simple games and other applications for pre-packaged computers. Bill has since developed software for many industries such as direct sales and the the stock market. Bill has now started sharing his many years of software development experience through a blog on the MPSHouse website and one on one lessons.


  1. Vasya  - 21 February , 2014 - 2:55 am
    Reply /

    Very useful tutorial, where is second one?

  2. Vasya  - 21 February , 2014 - 2:59 am
    Reply /

    Sorry. Just second tutorial without tag `gtkmm`

  3. Joe W.  - 19 June , 2014 - 9:13 am
    Reply /

    1st of all, I would like to thank you for writing up these 5 tutorials. These are my 1st step learned the gtkmm programming and very help to me. I write to you ask if you can add another program that includes a child window. I google and couldn’t find related to gtkmm 3.0 also looked most of examples on https://developer.gnome.org/gtkmm-tutorial/stable/ and couldn’t find neither.
    My 2nd question is that I got building error while looking for a child window example.
    error: no matching function for call to ‘Gio::SimpleActionGroup::add_action(const char [12], sigc::bound_mem_functor0)’|
    , which is for 12. Menus and Toolbars of above linker. Do you have idea how to fix it on Code::BLOCK.
    Best Regards,

    • Bill  - 20 November , 2014 - 8:46 pm
      Reply /

      (GTKmm Tutorial 6 created for Joe w)

  4. MINHTAM  - 20 November , 2014 - 2:14 pm
    Reply /

    Thanks so much!

Leave a comment

Back to Top