GLib is a utility library required by GTK+, but which can also be used independently in non-GUI applications. This article shows how to compile C programs that use GLib in Linux. It also shows how to install the correct documentation for the version of GLib installed on the system and how to find the version number of the currently installed GLib library.
Commands and libraries used in this article were tested on Linux Mint 18, but should work on all Debian based Linux distributions including Ubuntu. Before compiling a program, make sure that the GLib development library is installed on your system:
sudo apt-get install libglib2.0-dev
Part 7 of GTK 3 Programming with C and Glade Tutorial
See the full GTK3 tutorial index
Video update October 2019: Watch the video to see how to convert the command line GLib program at the end of this page into a Glade GUI application.
Compiling a Simple GLib C Program
The example program below is a simple “Hello World” C program written using a GLib function. Instructions on compiling the program in Linux are shown below the code listing.
#include <glib.h> int main(void) { g_print("Hello, world!\n"); return 0; }
Use the following at the command line to compile the above program:
gcc main.c `pkg-config --cflags --libs glib-2.0` -o hello
In the above line use back-ticks (`) and not single quotes (‘) to enclose pkg-config and its options. Or use:
gcc main.c $(pkg-config --cflags --libs glib-2.0) -o hello
The compiled program called hello can be run from the command line as follows:
./hello
Installing GLib Help
Install the GLib help documentation for the version of GLib installed on your system.
sudo apt-get install libglib2.0-doc
The help files can be viewed in Devhelp. Install Devhelp using the following command:
sudo apt-get install devhelp
Devhelp can be found on the Linux Mint 18 Mate menu under Menu → Programming → Devhelp. The image below shows the GLib Reference Manual opened in Devhelp.

Finding the Version Number of GLib
Clicking GLib Reference Manual in the left pane of Devhelp should show the version number of the documentation which should correspond to the version number of the installed library. There are two other ways to determine the installed version of the library — using pkg-config and programmatically as described below.
Using pkg-config
Entering the following command in a terminal window will display the currently installed version of GLib.
pkg-config --modversion glib-2.0
Programmatically
When the following program is compiled and run it will display the current version of GLib.
#include <glib.h> int main(void) { g_print("glib version number is %d.%d.%d\n", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION); g_print("GLIB_MAJOR_VERSION = %d\n", GLIB_MAJOR_VERSION); g_print("GLIB_MINOR_VERSION = %d\n", GLIB_MINOR_VERSION); g_print("GLIB_MICRO_VERSION = %d\n", GLIB_MICRO_VERSION); return 0; }
Compile using:
gcc main.c `pkg-config --cflags --libs glib-2.0` -o version
Or:
gcc main.c $(pkg-config --cflags --libs glib-2.0) -o version
Run:
./version
GLib C Coin Toss Example Program
An additional GLib C program which simulates a coin toss. When the program is run it will print either Heads or Tails to the terminal window. Heads or tails for the coin toss is determined by the g_random_boolean() function.
#include <glib.h> int main(void) { gboolean result; result = g_random_boolean(); if (result == TRUE) { g_print("Heads\n"); } else { g_print("Tails\n"); } return 0; }
Compile using:
gcc main.c `pkg-config --cflags --libs glib-2.0` -o coin_toss
Or:
gcc main.c $(pkg-config --cflags --libs glib-2.0) -o coin_toss
Run:
./coin_toss
Watching your video’s to learn this for the Raspberry Pi.
Every thing is working Shuc a change from the other tutorials I tried where I was translating everything to the current version by trial and error to get it to work from other videos.