9.1 - Using Glib

9.1 - Using Glib

*GLib- is a cross-platform C utility library used to simplify application development. It serves as the low-level foundation for GTK and GNOME, but it’s useful in any C project that needs robust, portable utility functions.

Why Use GLib?

GLib enhances C with:

  • Portable data types (gint, gboolean, etc.)
  • Dynamic strings (GString)
  • Collections (GList, GHashTable)
  • Command-line argument parsing
  • Error handling
  • Event loops and I/O utilities

Example: GLib in Action

#include <glib.h>

int main(int argc, char *argv[]) {
    GString *str = g_string_new("Hello");
    g_string_append(str, ", GLib!");
    g_print("%s\n", str->str);
    g_string_free(str, TRUE);

    g_print("Program received %d arguments.\n", argc);
    for (int i = 0; i < argc; i++) {
        g_print("Arg %d: %s\n", i, argv[i]);
    }

    return 0;
}

1. GLib Header: <glib.h>

This header includes the full GLib core API. It gives access to:

  • GLib data types
  • Memory management tools
  • String and file utilities
  • Logging and debugging features

2. GLib Data Types

GLib defines platform-independent data types:

GLib Type Standard C Equivalent
gint int
guint unsigned int
gboolean int (TRUE or FALSE)
gchar char
gpointer void*
gsize size_t

Why use these?

Using GLib types improves portability*- and **consistency, especially on different architectures and platforms.

3. Dynamic Strings: GString

GString *str = g_string_new("Hello");
g_string_append(str, ", GLib!");
g_print("%s\n", str->str);
g_string_free(str, TRUE);
  • GString handles string growth automatically.
  • g_string_append() concatenates strings safely.
  • g_string_free() releases memory.

4. Command-Line Argument Handling

g_print("Program received %d arguments.\n", argc);
for (int i = 0; i < argc; i++) {
    g_print("Arg %d: %s\n", i, argv[i]);
}

GLib provides g_print(), which behaves like printf() but is UTF-8 safe and cross-platform.

For advanced CLI parsing, GLib also offers GOptionContext, but argc/argv can still be used directly in simple cases.

5. GLib Utility Functions

Here are some common GLib functions you’ll likely use often:

Function Description
g_print() Safe, portable version of printf()
g_malloc(), g_free() Safer wrappers over malloc() and free()
g_strcmp0() Safe string comparison that handles NULL
g_getenv() Gets environment variable
g_error(), g_warning() Logging and debugging output
g_ascii_strcasecmp() Case-insensitive ASCII string compare

Compilation with GLib

Use pkg-config to compile your GLib programs:

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

Breakdown:

  • --cflags: Provides necessary -I include flags.
  • --libs: Adds linker flags for GLib (-lglib-2.0).
  • -o glib_example: Output binary.

Sample Output

Hello, GLib!
Program received 1 arguments.
Arg 0: ./glib_example

If you run with arguments (e.g. ./glib_example foo bar), each argument will be printed.

More information

You can refer to https://docs.gtk.org/glib/ to see more information about using Glib

Summary

GLib extends C with modern features like dynamic strings, flexible types, safe memory handling, and helpful utilities. It helps you write cleaner, more portable C code—especially when building large or GUI applications.



© 2025 Easy and fast Programming guide For C

Powered by Tessera for Jekyll