GTK+ 3 C Program using Glade 3

This article shows how to create a C program that uses the GIMP Toolkit (GTK+)  version 3 to display a window that is created using the Glade 3 user interface designer.

A window is designed using Glade 3 which is saved as a .glade XML file. A C program then opens the glade file when the compiled C program is run and uses it to display the designed window. This avoids having to lay out the window programmatically in the C code itself.

The GTK+ 3 C program using Glade 3 example shows the minimum code needed to display a GUI window with Glade and GTK+.

Part 3 of GTK 3 Programming with C and Glade Tutorial

See the full GTK3 tutorial index

Update October 2019 – see the video which demonstrates this tutorial using the latest version of Glade which looks different from the one in the text that follows.

Creating a Window in Glade

Glade can be installed on a Debian based system such as Ubuntu or Linux Mint with the following command:

sudo apt-get install glade

To create a window in Glade, start the Glade application and follow the steps below.

1. Create a Top Level Window

Click the Window icon in the Toplevels section of the left palette.

Creating a Top Level Window in Glade for the GTK+ 3 C Program using Glade 3
Creating a Top Level Window in Glade

A new window will appear in Glade with its properties displayed in a pane at the right of the Glade editor window.

2. Change the Window ID

We can now optionally change the window ID. This is the name that the window will be referred to in the C code. In the image below it is changed from the default name of window1 to window_main. The change is made in the Window Properties pane at the right of the Glade window.

Change Glade Window ID
Change Glade Window ID

3. Change the Default Window Size

The default size of the window can optionally be changed. This will be the size of the window when it is created by the C program.

The default window size is changed by scrolling down the General tab of the Window Properties pane and then checking the two checkboxes Default Width and Default Height. The width and height values next to the checkboxes can then be changed. The image below shows the window size being changed to 640 pixels wide by 480 pixels high.

Changing the Default Window Size in Glade
Changing the Default Window Size in Glade

 4. Connect a Callback Function to the destroy Signal

When the window that is currently being designed in Glade is displayed by the C program, it will emit a destroy signal when the window is closed. A callback function needs to be connected to the destroy signal that will run when the window is closed.

To add a callback function, click the Signals tab in the Window Properties pane of Glade. Click the arrow next to GtkWidget to expand the list and then scroll down to destroy. Click the text that says Type here twice under the Handler heading. Text can now be typed into this field. Start by typing on — Glade will now suggest a name for the callback function that can be selected by pressing the keyboard down arrow and then the Enter key twice. The suggested name is on_window_main_destroy as shown in the image below.

Connecting a Callback Function to the destroy Signal
Connecting a Callback Function to the destroy Signal

5. Change the Window Title

To change the title that will appear at the top of the window, click the General tab in the Window Properties pane and scroll down to find the box labelled Title under Appearance. The image below shows the title of the window changed to Template Window.

Changing the Window Title in Glade
Changing the Window Title in Glade

6. Save the Glade File

After making changes to the Glade file, save it by clicking the Save icon or File and then Save from the top menu. Save the file as which gives the file name the same name as the window ID.

Now that the Glade file has been created, the C application program can be created that will use the Glade file to display the window.

The C Program Code

The code below is based on code from a tutorial by Micah Carrick which has since disappeared along with his website.

The main.c code listing:

#include <gtk/gtk.h>

int main(int argc, char *argv[])
    GtkBuilder      *builder; 
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file (builder, "", NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
    gtk_builder_connect_signals(builder, NULL);



    return 0;

// called when window is closed
void on_window_main_destroy()

The code creates the window from the file and connects the on_window_main_destroy() callback function to the destroy signal as specified in the Glade file. The on_window_main_destroy() callback function calls gtk_main_quit() which will close the window.

Create a file called main.c and copy the above code to it. Save this file to the same directory or folder that contains the glade file that you already created.

Compiling the Code

Before compiling the code, make sure that the GTK+3 development library is installed. It can be installed on a Debian based system such as Ubuntu or Linux Mint with the following command:

sudo apt-get install libgtk-3-dev

The following line will compile the above code.

gcc -o gladewin main.c -Wall `pkg-config --cflags --libs gtk+-3.0` -export-dynamic

Where main.c is the name of the file that the above C code is saved to and gladewin is the name of the executable output file.

Make sure that the file is in the same directory as the gladewin application when running the application.

When the application is run it displays the window that was created in Glade, as shown in the image below. The window can be resized and closed.

Window Application Created with GTK+ 3 and Glade 3
Window Application Created with GTK+ 3 and Glade 3

GTK+ 3 C Program using Glade 3 Development System Details

The code and Glade file for this GTK+ 3 C Program using Glade 3 example was developed on Linux Mint 17 with:

  • Glade version 3.16.1
  • GTK library (libgtk-3-dev) version 3.10.8

Update 25 October 2016
This tutorial was tested on a Linux Mint 18 computer with:

  • Glade version 3.18.3
  • GTK library (libgtk-3-dev) version 3.18.9-1ubuntu3.1

61 thoughts on “GTK+ 3 C Program using Glade 3”

  1. thats kinda useful for keeping things fast running; wish they had something like this on their project page

  2. Thank you. This was exactly what is needed to get started. Many other tutorial on the net have lots of information but it gets outdated quickly with all of the glade updates.

  3. Thank you ! I have spent the last 2 days creating a working gtk development environment; there have been so many glade / gtk changes and versions, that I kept finding out-of-date tutorials. Good work, well done.

  4. Howdy,
    Thanks for, info given. Simplicity sees picture, tell more that 10^2 words.


  5. Wow, thank you. I had spent hours on a tutorial that was so complex with so many steps that I gave up several times because I kept making tiny insignificant mistakes and couldn’t seem to fix them. It was all pointless busywork, trying to demonstrate too many things at once. This is exactly what I need, short and to the point.

  6. hello when i am compiling after all of them, it gave me this error;
    gcc: error: pkg-config –cflags –libs gtk+-3.0: No such file or directory

    1. Which Linux distribution are you using? Is pkg-config installed? Did you install the GTK+ 3 development library?

        1. just a thought as i had something similar: did you notice the backquotes before pkg and after 3.0? they are critical in making sure pkg-config … sets up paths etc correctly ?

  7. Worked Good. Needed these changes for FreeBSD

    main.c GTK Header file needs full path

    cc `pkgconf –cflags gtk+-3.0` main.c -o gladewin `pkgconf –libs gtk+-3.0` -rdynamic -Wall

    Thanks for the help.

  8. Great tutorial tnx, by the way do planning to do more on gtk/glade tutorials, cheers πŸ™‚

  9. Copy/pasted code from here into main.c … compiled without one squeak. Very good.

    I’m using Ubuntu 16.04 . Left-clicking on the ‘x’ closes the window… but the command prompt does not appear. Typing ^C terminates the program.

    I’ve used gdb and set a breakpoint on the callback on_window_main_destroy(). Closing the window does not call the callback.

    Looking forward to working more tutorials… Thanks for creating this one.

  10. OOOPS!

    Never mind. Typo on my part. The handler didn’t get entered into the signals section.

    Mea Culpa.



  11. And how to compile it to create single binary with embeded glade xml code inside this binary? So the binary is standalone and doesnt require any extarnal glade file.

    1. The idea of using an external Glade file is so that changes to the application’s GUI can be made without having to recompile the application code, so I don’t know if including the Glade file in the C source code is possible. There is the gtk_builder_new_from_string() function that allows XML to be included in an application, but I am not sure if you can include the Glade file as-is. You would have to test to see if it will work with a Glade file. I have not done this myself.

  12. This was exactly what I needed to get jump started. It answered a number of questions all at once. I am new to GTK and Glade, but not to programming. This was great!!

    I see you have new entries covering input/output. I am headed there now. Thanks.

  13. Hello CodeNerd,
    I did everything like you show it. But when i run the code it says
    “(gtk-test:23026): Gtk-CRITICAL **: gtk_widget_show: assertion ‘GTK_IS_WIDGET(widget)’ failed”

    I searched a bit on the internet but I can`t find anything helpfull. I hope you can help me.

    1. The problem was that when I compiled it with eclipse, the glade file has a different location than the compiler thought.
      But I’m still working on how to fix it.
      (Now I’m using gnome-builder, so if you have the solution there for, please tell me.)

  14. Very top-level question for you, as I’m researching easy GUI creation for Linux and serial libraries…

    How does one select the “target” language to use with Glade/GTK? In the Arduino world, a lot of folks use Glade with a Python backend. All well and good, but I’m a C guy.

    I see that you’re using C in this tutorial, but I’m confused about how one switches from one language to the other.


  15. Thanks for this example, it helped greatly in getting started with Glade/GTK3 on MSYS in Windows 10.

    Initially, I couldn’t get signals to work correctly until adding G_MODULE_EXPORT to “void on_window_main_destroy(){…}” – the sympton being that when closing the window, I had to CTRL-C on the terminal to regain the prompt.

    Also, I was getting a warning message on the compile/link –
    “ld.exe: warning: –export-dynamic is not supported for PE+ targets, did you mean –export-all-symbols?” – and am not experienced enough to understand what it means, although it didn’t seem to affect the result.

    In order to get rid of it, I reverted to the basic method described in a different GTK tutorial, thus :

    gcc `pkg-config –cflags gtk+-3.0` -o gladewin main.c `pkg-config –libs gtk+-3.0`

    which possibly indicates your options are not relevant to Windows? – would be interested to hear your opinion.

    Again, thanks for the excellent kick start.

  16. after compiling my own programme of gtk+ and c
    it rises to error as follow?
    please help me?

    /usr/bin/ld: warning: cannot find entry symbol xport-dynami; defaulting to 0000000000000880

  17. I’m used this line for the compilation:
    gcc -Wall –pedantic -o ejemplo1 ejemplo1.c `pkg-config –cflags –libs gtk+-3.0` -export-dynamic
    It works perfectly -> of course, before I got shure to have
    pkg-config installed and libgtk-3-dev also with the next couple of instructions in the ‘shell’ 1) pkg-config –version 2)sudo apt-get install libgtk-3-dev .

  18. First off, thaks for the great tutorial.But, when try to compile by pasting this command to the terminal window:
    /gcc -o gladewin main.c -Wall `pkg-config –cflags –libs gtk+-3.0` -export-dynamic/
    It just gets this error:
    /gcc: error: main.c: No such file or directory/
    main. glade and main.c are in the same folder and pkg-config is already newest version.I`m using raspbian on rpi 2.Any ideas?It`s so frustrating

  19. Thank you for this. Very well put together and to the point. Before I really dive deep into glade/gtk, I wanted to see it work and this proved to me in less than 5 minutes that it does.

    Thank you!

  20. Thanks from me, too! This tutorial is a perfect starting point. I am working on a Lattepanda 4G/64G running Opensuse Leap 15.

    Thank you again!

  21. I do not have the on_window_main_destroy function in glade
    Instead i have on_window_main_destroy_event in glade.
    I tried changing the function name to on_window_main_destroy_event in main.c ……..still it is giving the exception that particular signal handler.

  22. Awesome site – thanks for making this! I have been struggling to find a good GTK tutorial (either online or in printed form) but almost everything I find is outdated. Everything here is clearly explained to get up and running!

  23. I’m stuck at step 1 – when I start up glade, I just get a blank screen with a menu. How do you get the tool box to display so I can select my top level window so I can get started?

  24. Tried this, didn’t work:

    (gladewin:4660): Gtk-WARNING **: Theme parsing error: gtk-contained.css:2134:2: Expected semicolon

    (gladewin:4660): Gtk-CRITICAL **: gtk_widget_show: assertion ‘GTK_IS_WIDGET (widget)’ failed

  25. Hi, I want to execute a external command (like date or time) from gtk script (c). Can you explain how to do?

  26. I got this working in CentOS 7. The widget window doesn’t show up by default in the current version. However, I was able to figure out the tutorial and got the program running within 15 minutes.

  27. Make another window which is opened via button click.
    Open task manager and look that memory isn’t freed after g_object_unref() calling. Open this window many times and get huge memory leak.

  28. Thanks! Got my (Linux Mint) GIU, Hello World ‘application’ running, with only minor hiccups, which could’ve been avoided with careful reading of the tutorial and not skipping ahead. Do you think the Qt environment is better than Glade?

  29. I’m trying to learn how to use GTK, and it get’s so disheartening – few of the examples and tutorials out there compile, let alone work. Your site is remarkable. Thank you so much – you renew my faith that this is a worthwhile pursuit.

    1. Much of what is out there is well out of date and the Gtk 3 API changed much during what many thought should have been a stable API period. Regardless, with GTK 3.24 the API is now considered stable and this should be the time that we see some consistent documentation but it seems to be slow in coming.

      My thanks to CodeNerd for writing and making the videos. I have had plenty of false starts and I think I learned more from his GTK Tutorial 3 video than anything else I spent time on through the years. All of the parts shown are the most needed basic items. I’d no idea of how to handle signals with Glade/GtkBuilder until working with this video and the code a while ago.

      Many thanks.

  30. I have a error on msys2 , and my operating system is win7
    # gcc -o gladewin ug_gtk.c -Wall `pkg-config –cflags –libs gtk+-3.0` -export-dynamic
    ug_gtk.c:3:10: fatal error: glade/glade.h: No such file or directory
    3 | #include
    | ^~~~~~~~~~~~~~~
    compilation terminated.

  31. Hi,
    Thnx great tutorial.
    Problem: I get this message: there is no application for a shared library. So it does not run. Strange thing is; I can run it as an administrator.
    What can be wrong en how do I fix it?

  32. This tutorioal worked on Linux Ubuntu Mate 20.04 with snap installed Glade version 3.36.0 and libgtk-3-dev version 3.24.20-0ubuntu1.

    I’m encouraged to go further with learning Glade.

  33. Add information:

    `-export-dynamic` for compiling is needed to prevent the compiler to remove `on_window_main_destroy` from the executable. Compilers usually do that to optimize the size, as they think the function is not used.

    Glade file content

    Here’s a glade file content, that works with the code:

Leave a Reply

Your email address will not be published. Required fields are marked *