Glib
1.1 Introduction
G is common in the Open Source world. It represents GNU ("GNU's not UNIX "). For example, GTK +, glib, gobject, and gnome, and some other software packages, such as ghostscript and GCC, are full of G.
To understand the subsequent sections, you must learn some basics of glib (libglib-2.0 ). It provides basic data structures and practical functions for GTK + and gnome programs. This chapter will introduce the structure and API of glib. In Chapter 2, you will learn about glib's Object System (gobject ).
Glib is unavoidable when you use gnome and GTK +. Other libraries, such as orbit, do not reference other libraries except glib. The Federation actions provided by glib (this does not know what to translate) and utility programming programs provide convenience and are easily transplanted to other platforms.
This chapter does not contain graphic operation code. It is just a simple one. It gradually describes the functions and data structures of glib. It may be a bit boring to read, but you can also jump to Chapter 3 to read the content of GTK +. However, you will often return to view the content of the first two chapters.
1.2 glib naming coventions (Glib naming Rules)
Like many other libraries, glib also standardizes naming rules for consistency and ease of use.
The function names are generally in lower case and are underlined between the names of each part, such as g_timer_new () and g_list_append (). And all function names start with G.
In glib, all functions have the prefix g _.
The type name does not contain underscores, and all types of Components in glib start with an uppercase letter g, such as gtimer and Glist. However, the basic types in glib are notable exceptions. This article will be introduced in section 3.
If a function is mainly used to operate on a specific type, the prefix of the function matches the corresponding type. For example, g_timer _ * is the type of gtimer. G_list _ * is the Glist type.
These rules sound more complex than they actually do.
1.3 Basic Types (basic type)
Before using glib, you must first adapt to the basic types of glib. You may be wondering why guchar is better than unsigned.
Char is good. If your program remains on the same platform, use guchar and use unsigned
Char has no substantial difference. However, if you want to write programs transplanted between different platforms, such as between windows and UNIX.
Then you will be very grateful to glib for abstracting the basic types.
For example, if you want to define a 16-bit unsigned integer on all possible platforms, it may seem troublesome to use the C language. Fortunately, glib helped you deal with this. All basic types are listed in the following table.
To use glib and its type, you must include glib. h In the source code.
# Include
<Glib. h>
Gpointer and gconstpointer types often appear in the glib data structure. These two are non-typed memory pointers. In glib, the function is responsible for checking the types of the two pointers. programmers and compilers do not care about this. This is especially convenient when comparing callback functions, sorting, and traversal.
The true and false constants are defined for the gboolean type in the glib header file. Do not use comparison operators when using these constants. For example, use: If (my_gboolean) instead of: If (my_gboolean = true ).
Glib type |
Corresponding type in C |
Gchar |
Char |
Ugchar |
Unsigned char |
Gint |
Int |
Guint |
Unsigned int |
Gshort |
Short |
Gushort |
Unsigned short |
Glong |
Long |
Gulong |
Unsigned long |
Gfloat |
Float |
Gdouble |
Double |
Gint8 |
Int, 8 bits wide |
Guint8 |
Unsigned int, 8 bits wide |
Gint16 |
Int, 16 bits wide |
Guint16 |
Unsigned int, 16 bits wide |
Gint32 |
Int, 32 bits wide |
Guint32 |
Unsigned int, 32 bits wide |
Gint64 |
Int, 64 bits wide |
Guint64 |
Unsigned int, 64 bits wide |
Gpointer |
Void *, untyped pointer |
Gconstpointer |
Const void *, constant untyped pointer |
Gboolean |
Boolean value, either true or false |