What is glib?

glib is a C portability and utility library for UNIX-like systems and Windows. This chapter covers some of the most commonly-used library features in GTK+ and Gnome applications. glib is simple, and the concepts are familiar; so we’ll move quickly. For more complete coverage of glib, see glib.h or the free glib reference manual that comes with the library. (By the way: don’t be afraid of using the glib, GTK+, or Gnome header files; they are very clean and easy to read, and are handy as a quick reference. For that matter, don’t be afraid to look at the source code, if you have very specific questions about the implementation.)

glib’s various facilities are intended to have a consistent interface; the coding style is semi-object-oriented, and identifiers are prefixed with “g” to create a kind of names- pace.

glib has a single header file, glib.h.

glib provides substitutes for many standard and commonly-used C language con- structs. This section describes glib’s fundamental type definitions, macros, memory allocation routines, and string utility functions.

Type Definitions
Rather than using C’s standard types (int, long, etc.) glib defines its own. These serve a variety of purposes. For example, gint32 is guaranteed to be 32 bits wide, something no standard C type can ensure. guint is simply easier to type than unsigned. A few of the typedefs exist only for consistency; for example, gchar is always equivalent to the standard char.

The following primitive types are defined by glib:
gint8, guint8, gint16, guint16, gint32, guint32, gint64, guint64—these give you in- tegers of a guaranteed size. Not all platforms provide 64-bit integers; if a platform has them, glib will define G_HAVE_GINT64. (If it isn’t obvious, the guint types are unsigned, the gint types are signed.)
gboolean is useful to make your code more readable, since C has no bool type.
gchar, gshort, glong, gint, gfloat, gdouble are purely cosmetic.
gpointer may be more convenient to type than void *. gconstpointer gives you const void*. (const gpointer will not do what you typically mean it to; spend some time with a good book on C if you don’t see why.)

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: