Compiling GLib C Programs

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:


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.

The GLib Reference Manual in Devhelp
The GLib Reference Manual 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


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);
	return 0;

Compile using:

gcc main.c `pkg-config --cflags --libs glib-2.0` -o version


gcc main.c $(pkg-config --cflags --libs glib-2.0) -o 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) {
	else {
	return 0;

Compile using:

gcc main.c `pkg-config --cflags --libs glib-2.0` -o coin_toss


gcc main.c $(pkg-config --cflags --libs glib-2.0) -o coin_toss



3 thoughts on “Compiling GLib C Programs”

  1. 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.

  2. Hello CodeNerd,
    I’m really enjoying your set of tutorials on GTK+ and C, like many other people… thank you very much for your excellent work.
    I’m working mostly on Linux, Raspberry pi, Android, and PICs; on embedded applications, CNC, and manufacturing of instruments.
    Sounds like you are from South Africa?
    Any chance you’re in Cape Town? (which is where I am)
    kind regards…

    1. Hi Neil
      I am glad to hear that you are enjoying the tutorials.
      Yes, I am from SA, but not in CT.

Leave a Reply

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