GTK 3 C Code Hello World Tutorial using Glade 3

In this easy tutorial for beginners, a simple “Hello World” program is created using GTK+ 3 and programmed in the C programming language. Glade 3 is used to create the GUI window, button and text label in this GTK 3 C code hello world tutorial.

The image below shows the windowed Hello World application made in this tutorial. When the button in the window is clicked for the first time, the text “Hello, world!” is displayed in the window. Each time the button is clicked, the total number of button clicks is updated and displayed in the window.

Application Created in the GTK 3 C Code Hello World Tutorial
Application Created in the GTK 3 C Code Hello World Tutorial

The tutorial demonstrates the use of a button and text label and how to get a handle or pointer to the text label in order to change its text.

Before continuing with the tutorial, install the GTK+ 3 and Glade 3 development tools on your computer.

You may also want to see the very basics of creating a GTK+ 3 program using Glade 3 and C code.

Part 5 of GTK 3 Programming with C and Glade Tutorial

See the full GTK3 tutorial index

This GTK 3 C Code Hello World Tutorial that uses labels and a button can be seen in the following video, updated to use the new Glade UI designer. Refer to the GTK+ 3 reference manual to find the GTK functions used in this tutorial.

GTK 3 C Code Hello World Tutorial Steps

Follow the steps below of this GTK 3 C Code Hello World tutorial to build the application as shown in the previous image. The steps show how to create a new project from template files, design the GUI window in Glade, write the application C code and build the project.

1. Start a New GTK+ 3 Glade C Project

It is easiest to start with a set of GTK+ 3 Glade C programming template files which add a nice structure to the program by separating the C source code files and Glade files into their own folders. The template files include:

  • A main.c file containing the function main() in the src folder
  • A main glade window called in the glade folder with destory signal callback handler function already connected
  • A make file called makefile in the main folder of the project for building the project
  • Some skeleton source code in the main.c file that creates the main window of the project using the Glade file

1.1 Create a Set of Template Files

Create a set of template files as described in the article on creating a GTK+ 3 Glade C programming template. Keep this set of template files to use as a base for starting new GTK Glade C projects.

1.2 Start a New Project using the Template Files

Make a copy of the template files folder and rename it to hello.

Open the make file and and change the TARGET name at the top of the file to hello. When the project is built the executable file name will now be named hello.

# change application name here (executable output name)

You should now have a project that looks as follows:

GTK+ 3 Project Template Files used to Start the GTK 3 C Code Hello World Tutorial
GTK+ 3 Project Template Files used to Start the GTK 3 C Code Hello World Tutorial

2. Edit the Glade File

Open the glade folder and open in the Glade editor. If the main window does not appear in the Glade editor, click window_main in the right pane.

2.1 Add a Fixed Grid

Although GTK has a method of packing widgets in a window, we will use a fixed grid in this tutorial. Users of MS Visual development tools will be familiar with placing widgets on a grid in a window.

Click the fixed grid icon in the left pane of the Glade window under Containers and then click the main window to place the grid.

Placing a Fixed Grid in Glade
Placing a Fixed Grid in Glade

Without the fixed grid, buttons and text labels could not be placed.

2.2 Place Text Labels

This project uses two text labels — one to display the hello world text and a second label to display the number of times the button is clicked.

2.2.1 Placing a Label

Click the Label item in the left pane of Glade under the Control and Display section. Click the main window on the grid to place the label. Use the drag / resize tool to move the label to the desired position.

Placing a label in Glade
Placing a label in Glade
2.2.2 Sizing a Label

Size the label by changing Height request under the Common tab in the right pane of glade to 30.

Changing Glade Label Height
Changing Glade Label Height
2.2.3 Change Label ID and Text

In the right pane, with the label selected, click the General tab. Change the ID of the label to lbl_hello in the ID box and the text of the label to in the Label box.

Changing Glade Label ID and Text
Changing Glade Label ID and Text
2.2.4 Add a Second Label

Create a second label with the same height and text as the first label, but give it an ID of lbl_count and place it under the first label.

GTK 3 C Code Hello World Tutorial Second Glade Label
Second Glade Label

2.3 Placing and Editing a Button

In the left pane of Glade, click the Button icon under the Control and Display section. Click on the main window grid to place the button. Change the height of the button to 30 in the same place that the height of the text was changed (right pane, Common tab, Height request field).

Place the Button in Glade
Place the Button in Glade

In the right pane under the General tab, change the button ID to btn_hello and the text of the button to Hello under the Button Content section near the bottom of the right pane. Place the button under the second label.

Placing a Glade Button
Placing a Glade Button

2.4 Changing the Window Size and Title

Click window_main at the top of the right pane in Glade to select the main window. Under the General tab in the right pane change the window width and height  both to 200 in the Default Width and Default Height fields. Change the window title in the Title field to Hello World.

Click the preview snapshot cog icon to see  a preview of the window to make sure that the widgets fit in the window properly.

Changing Glade Window Settings
Changing Glade Window Settings

2.5 Button Callback Function Signal

When the button is clicked, it will emit a signal. We need to attach a function to the signal so that the function will be called when the button is clicked.

Click the button in Glade to select it and then click the Signals tab in the right pane of the Glade editor. Click the text under the Handler heading next to clicked to select it and then a second time to start editing it. Type on and then a name suggested by Glade will pop up (on_btn_hello_clicked). Press the down arrow key to select it and then press the Enter key twice to make the change.

Adding the Glade Button Signal Handler Finishes the GTK 3 C Code Hello World Tutorial GUI
Adding the Glade Button Signal Handler Finishes the GTK 3 C Code Hello World Tutorial GUI

Save the Glade file, we will now edit the C source code.

3. Write the C Code

The final step before building the application is to write the C code for this GTK 3 C Code Hello World Tutorial.

Open the main.c C template file for editing found in the src folder of the project.

We need a pointer to each of the labels in the project so that we can get hold of the labels to change their text. In this simple project, two global pointers are defined at the top of the C file.

#include <gtk/gtk.h>

GtkWidget *g_lbl_hello;
GtkWidget *g_lbl_count;

After connecting the signals in the code, get pointers to the two labels:

gtk_builder_connect_signals(builder, NULL);
g_lbl_hello = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_hello"));
g_lbl_count = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_count"));

Finally we must add a function that writes text to the first label and increments a count in the second label when it is clicked. This function must have the same name as the signal handler function set for the button in Glade.

void on_btn_hello_clicked()
    static unsigned int count = 0;
    char str_count[30] = {0};
    gtk_label_set_text(GTK_LABEL(g_lbl_hello), "Hello, world!");
    sprintf(str_count, "%d", count);
    gtk_label_set_text(GTK_LABEL(g_lbl_count), str_count);

The final code should look as follows:

#include <gtk/gtk.h>

GtkWidget *g_lbl_hello;
GtkWidget *g_lbl_count;

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

    gtk_init(&argc, &argv);

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

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
    gtk_builder_connect_signals(builder, NULL);
    // get pointers to the two labels
    g_lbl_hello = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_hello"));
    g_lbl_count = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_count"));



    return 0;

// called when button is clicked
void on_btn_hello_clicked()
    static unsigned int count = 0;
    char str_count[30] = {0};
    gtk_label_set_text(GTK_LABEL(g_lbl_hello), "Hello, world!");
    sprintf(str_count, "%d", count);
    gtk_label_set_text(GTK_LABEL(g_lbl_count), str_count);

// called when window is closed
void on_window_main_destroy()

4. Build and Run the Project

Open the template directory in a terminal window and build the project using the make file by entering the make command.


The project can be run by either double-clicking the hello icon after navigating to the project folder using a file manager or from within the terminal window by entering:


The Hello World window should open. Clicking on the Hello button should display the “Hello, world!” text in the first label and increment the count in the second label.

5. GTK 3 C Code Hello World Tutorial Development Environment

This tutorial was developed on a Linux Mint 17.3 Cinnamon 64-bit computer with Glade 3.16.1 and GTK+ development library 3.10.8~8+qiana (libgtk-3-dev).

Update 25 October 2016
This tutorial was tested on a Linux Mint 18 Mate 64-bit computer with Glade 3.18.3 and GTK+ development library 3.18.9-1ubuntu3.1 (libgtk-3-dev).

Update 14 October 2019
This tutorial was tested on a Linux Mint 19.2 Cinnamon 64-bit computer with Glade 3.22.1 and GTK+ development library 3.22.30-1ubuntu (libgtk-3-dev). See the video near the top of this article that shows how to build the GUI window using the new Glade which has a different layout.

36 thoughts on “GTK 3 C Code Hello World Tutorial using Glade 3”

  1. After a lot of researching in google about Glade and GTK+ with C, I found your site and I’m absolute in love. This is a extreme useful tutorial for begginers, thanks a lot for doing it.

    My system: Ubuntu 16.04 LTS, Glade 3.18.3 and GTK+ 3.18.9.

    Thanks again, and if can, please post more! =)

  2. I have spent many hours trying to learn how to connect a UI with a C program, with no success until I found your tutorials. I have learned more in the past few days, than I did over the past few months with other tutorials. Thank you.

  3. Hi, thanks. But why are we using Glade here? Can we not just use gtk?
    Do bigger gtk application developers also use glade with gtk?


    1. Also the preview snapshot doesn’t give the accurate picture:

      || image links cut by moderator due to spammy looking image host site ||

      Also in the glade interface designer, I find it tough to place the widgets because I do not see the boundaries of other widgets. Is there any solution for this?

      1. Also the editor in the glade interface designer is not an accurate representation of how the window looks like. I mean when we change the window size, the editor’s measurements do not change accordingly.

        1. I think that this also has to do with packing. The designers of Glade probably assume that most users are going to use boxes to pack widgets. It then does not matter what size the window is during design, as long as the widgets are in the correct positions. In case you missed it here is the tutorial on packing:
          I updated the explanation of GTK packing in the tutorial as requested in a comment by another user.

      2. The answer is in GTK packing which takes some getting used to. I will explain more on this and update the packing tutorial.

    2. You are free to use plain GTK function calls in your code or use Glade. I wanted to use Glade for my own small applications to get away from the tedious work of trying to place widgets within C code. I could not find many tutorials on using Glade with GTK C code, so I started doing my own research and publishing the results here. I am not sure whether Glade is being used with bigger GTK applications and am trying to get a feel for this myself. Of the few smaller projects that I have looked at, some used Glade, but in all of them the Glade file would not even open in newer versions of Glade.

  4. Well, I have found your tutorials really useful, as I’ve never been able to conceptualized these things before. I’m a total C novice, as it turns out, but I’ve been trying anyway. At one point I had it running without segmentation faults, but it wasn’t updating the label. I’ve tried a few different ways to do what I want (output epoch time to label), but after about 8 hours I’ll call it a day for now. If you get a free second to tell me the probably obvious mistake I’ve got going on, it’d be awesome:

    The list of topics on this site, which I discovered after much search as regards Glade, got it immediately on my subscription feed. Ads are disabled, too, if you decide to start getting revenue here.


  5. All I can do is echo the comments before mine – at last! a simple to understand explanation with an easy example that actually does something that can be used. I’ve spent almost a week, on and off, trying to find out how to get a program to change a label, and here it is. Thank you. This is so much better than Gnome’s Getting Started with GTK+ which takes a few baby steps and then hurls one into the middle of an application ocean without so much as a paddle.
    At last I can concentrate on the project that needs a GUI rather than the mechanics of the GUI itself.

  6. Not working, its showing (hello:4436): Gtk-CRITICAL **: IA__gtk_widget_show: assertion ‘GTK_IS_WIDGET (widget)’ failed.
    I compiled it using gcc -o hello test.c -Wall `pkg-config –cflags –libs gtk+-3.0` -export-dynamic.

    1. I also had the problem with the copy/paste of the template. In that case this is what happened

      Comment Below

      I still get a Gtk-CRITICAL **: IA__gtk_widget_show: assertion ‘GTK_IS_WIDGET (widget)’ failed

      error with an exact copy, paste of the code.

      Putting some error checking into it spits out a warning

      “required gtk+ version 3.10, current version is 3.4”

      Normally you assume the requirement is 3.1 OR HIGHER but in this case if I simply remove

      from the .glade file the window pops up and everything works

  7. I have the same problem, but using the makefile. (Same problem with the example template) GTK_IS_WIDGET fails

  8. hello there!
    Thanks for your help, im having a little trobule, can someone lend me a hand?
    Im getting this error while im trying to run the main from code blocks proyects with C

    tbxWindow = GTK_WIDGET ( gtk_builder_get_object ( builder, “tbxWindow” ) );

  9. On function on_btn_hello_clicked, i think this line of code can be improved:

    snprintf(str_count, sizeof(unsigned int),”%d”, count);

    instead of

    sprintf(str_count, “%d”, count);

    1. You mean totally broken.
      I think you ment
      snprintf(str_count, sizeof(str_count),”%d”, count);
      but even then it has it’s issues.

  10. Sorry to resurrect a dead body, but I think there was an important oversight. The connection between the main window DESTROY signal and the void on_window_main_destroy() function is nowhere to be found.
    The connection has to be made pretty much like the connection for the button signal, only the signal is DESTROY instead of CLICK (uppercase used on purpose).
    Failure to do this connection will lead to mem leaks because the window is effectively destroyed but not the program, wich continues running.

  11. I am a GTK beginner and I found your tutorial very nice. The Glade intro is excellent.

    There is a point where I request you to change pointer declaration styles — to avoid immense confusion to others :

    int *p = 0;
    // Wrong — 0 is not being assigned to *p; it is being assigned to p

    int* p = 0;
    //Right. But be careful not to declare multiple variables in a single statement. This is a historical C artifact that has to be lugged around owing a mistake by Dennis Richie when choosing what was meant by:
    int* p1 = 0, p2 = 0;

  12. Hi,
    I started GTK 3 development with this tuturial!!
    Now I need to have an “ON Screen Keyboard” for my application.
    Do you have any recomendation???

  13. Great tutorials! I’ve been working through them and I realize there is A LOT to using Glade and GTK3.

    Question:- If you wanted your GUI to go fullscreen with no header bar, how would you do that with Glade?

    I’ve been experimenting with the command:-

    but no luck.

  14. Really nice toturials. You take it step by step in a clear way. It is helping me a lot. Thank you very much.

    Ben Thijssen

  15. The gtp-hello program worked just fine with gtk3 under GNU/Debian 10, Buster.

    I only ad to add the handler for properly closing the program.
    (maybe I missed it in the text)

  16. Most excellent and appreciated tutorials… I say tutorial(s) because it seems a few people about didn’t look at the prior tutorial where we made a template, and a makefile.

    To find anything out there supporting plain C is a win in my book, but with gtk3… that’s awesome. I think glade itself could use a lot of TLC as it’s not really self explanatory, and the window size should reflect the window size etc. (wysiwyg) but this series and glade has helps me get past all the bs involved in positioning things programmatically on a form.

    again great job, thanks

Leave a Reply

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