GTK in C

Fathopert, 08 December 2023

In this article, you will learn how to create a simple program that shows an empty window in C99 using the GTK library.

I work in Linux Mint 20.3 "Una" and use these particular versions of programs and libraries:

Installing GTK

Use the following command to install the GTK library:

apt install libgtk-3-dev libgtkmm-3.0-dev

Writing a Simple Program

In the project directory create the src and build folders. Then, in the src folder, create the main.c file and paste the following code into it:

#include "gtk/gtk.h"


/* function declarations -------------------------------------------- */
static void destroy(GtkWidget* window, gpointer data);
static gboolean delete_event(GtkWidget* window, GdkEvent* event,
                             gpointer data);

/* function implementations ----------------------------------------- */
static void
destroy(GtkWidget* window, gpointer data)
{
        gtk_main_quit();
}

static gboolean
delete_event(GtkWidget* window, GdkEvent* event, gpointer data)
{
        return FALSE;
}

int
main(int argc, char** argv)
{
        GtkWidget *window;


        /* Initialize GTK+ and all of its supporting libraties */
        gtk_init(&argc, &argv);

        /* Create a new window, give it a title */
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(window), "Hello from GTK");
        gtk_container_set_border_width(GTK_CONTAINER(window), 10);
        gtk_widget_set_size_request(window, 420, 272);

        /* Connect the main window to the destroy and delete-event signals. */
        g_signal_connect(G_OBJECT(window), "destroy",
                         G_CALLBACK(destroy), NULL);
        g_signal_connect(G_OBJECT(window), "delete_event",
                         G_CALLBACK(delete_event), NULL);

        /* Display the window to user */
        gtk_widget_show_all(window);

        /* Run the main loop */
        gtk_main();

        return 0;
}

Creating a CMake Project

In the project directory (outside of the src folder!), create a CMakeLists.txt file. Paste the following into it:

cmake_minimum_required(VERSION 3.2)
project(c-gtk-test VERSION 0.1 LANGUAGES C)

set(CMAKE_C_STANDARD 99)

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTKMM REQUIRED gtkmm-3.0)

add_executable(${CMAKE_PROJECT_NAME}
    src/main.c
)

target_include_directories(${CMAKE_PROJECT_NAME}
    PRIVATE src
    PRIVATE ${GTKMM_INCLUDE_DIRS})

target_link_libraries(${CMAKE_PROJECT_NAME}
    PRIVATE ${GTKMM_LIBRARIES})

Building and Running the Program

We are now ready to compile our program.

  1. Change into the build folder and create a Makefile using CMake:
    cd path/to/build
    cmake ..
    
  2. Build the executable:
    cmake --build .
    
  3. Run the program:
    ./c-gtk-test
    

If you stick with the article, you will see a window like this:

GTK Window

Hooray!



References

  1. Krause A. Foundations of GTK+ Development. (2007)
  2. Long S. An Introduction to C & GUI Programming. (2019)