Boost the power of C with these open source libraries
======
GObject and libsoup do a lot of work for you, so you can turn your attention to inventing amazing applications in C.
![Why and how to handle exceptions in Python Flask][1]
(Image by: Image from Unsplash.com, Creative Commons Zero)
The [GLib Object System (GObject)][2] is a library providing a flexible and extensible object-oriented framework for C. In this article, I demonstrate using the 2.4 version of the library.
The GObject libraries extend the ANSI C standard, with typedefs for common types such as:
* gchar: a character type
* guchar: an unsigned character type
* gunichar: a fixed 32 bit width unichar type
* gboolean: a boolean type
* gint8, gint16, gint32, gint64: 8, 16, 32, and 64 bit integers
* guint8, guint16, guint32, guint64: unsigned 8, 16, 32, and 64 bit integers
* gfloat: an IEEE Standard 754 single precision floating point number
* gdouble: an IEEE Standard 754 double precision floating point number
* gpointer: a generic pointer type
### Function pointers
GObject also introduces a type and object system with classes and interfaces. This is possible because the ANSI C language understands function pointers.
To declare a function pointer, you can do this:
```c
void (*my_callback)(gpointer data);
```
But first, you need to assign the `my_callback` variable:
```c
void my_callback_func(gpointer data)
{
//do something
}
my_callback = my_callback_func;
```
The function pointer `my_callback` can be invoked like this:
```c
gpointer data;
data = g_malloc(512 * sizeof(gint16));
my_callback(data);
```
### Object classes
The GObject base class consists of 2 structs (`GObject` and `GObjectClass` ) which you inherit to implement your very own objects.
You embed GObject and GObjectClass as the first struct field:
```c
struct _MyObject
{
GObject gobject;
//your fields
};
struct _MyObjectClass
{
GObjectClass gobject;
//your class methods
};
GType my_object_get_type(void);
```
The object’s implementation contains fields, which might be exposed as properties. GObject provides a solution to private fields, too. This is actually a struct in the C source file, instead of the header file. The class usually contains function pointers only.
An interface can’t be derived from another interface and is implemented as following:
```c
struct _MyInterface
{
GInterface ginterface;
//your interface methods
};
```
Properties are accessed by `g_object_get()` and `g_object_set()` function calls. To get a property, you must provide the return location of the specific type. It’s recommended that you initialize the return location first:
```c
gchar *str
str = NULL;
g_object_get(gobject,
"my-name", &str,
NULL);
```
Or you might want to set the property:
```c
g_object_set(gobject,
"my-name", "Anderson",
NULL);
```
### The libsoup HTTP library
The `libsoup` project provides an HTTP client and server library for GNOME. It uses GObjects and the glib main loop to integrate with GNOME applications, and also has a synchronous API for use in command-line tools. First, create a `libsoup` session with an authentication callback specified. You can also make use of cookies.
In this example code, there are two callbacks. One handles authentication, and the other handles the request itself.
Suppose you want a web server to allow a login with the credentials username **my-username** and the password **my-password**, and to set a session cookie with a random unique user ID (UUID) string.
I hope my examples show how the GObject and libsoup projects give C a very real boost. Libraries like these extend C in a literal sense, and by doing so they make C more approachable. They do a lot of work for you, so you can turn your attention to inventing amazing applications in the simple, direct, and timeless C language.