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:

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.

Use the following at the command line to compile the above program:

In the above line use back-ticks (`) and not single quotes (‘) to enclose pkg-config and its options. Or use:

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.

The help files can be viewed in Devhelp. Install Devhelp using the following command:

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.

Programmatically

When the following program is compiled and run it will display the current version of GLib.

Compile using:

Or:

Run:

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.

Compile using:

Or:

Run:

 

Leave a Reply

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