~couchNerd ~serialOutrager ~puts2In42 ~makesPigsFly

Want To Program Smartly In C? Use GLib

· by Shantanu Goel · Read in about 3 min · (568 Words)
C Code data structures GLib Linux Mac OS X portability security utility functions windows

GLib - An Introduction:

GLib is a utility library for C, which augments the standard C library in several purposeful ways to make your life that much easier while programming. GLib has the following things to offer you:

**1. Portability: **The main issue that haunts any C developer is the portability of code. One cannot rely on the standard C library for this as you may find many functions that work differently under different platforms are aren’t there at all sometimes. GLib ensures that the all the functionality exposed by it remains consistent across platforms, so that you can rest assured that your code will work the way its supposed to work irrespective of the Operating System it’s being used for (Of course, this assumes that you have ensured about portability aspects of your non-GLib related source code). Moreover, GLib is available for a vast array of contemporary Operating Systems including GNU/Linux, Microsoft Windows and Mac OS X.

**2. Security: **Though you still need to be careful about things like freeing allocated memories properly, etc but GLib does ensure that all its functionality is secure. Moreover, GLib has a policy of ensuring that all its functions are threadsafe. This saves you from a lot of checks and balances and locks and scheduling considerations if you had written all this yourself.

3. Useful Data Types: GLib exposes a lot of data types. Some are very basic that maintain portability across OS’s and 32-bit and 64-bit systems. e.g. you can rest assured that gint32 will always be 32 bit and gint64 will always be 64 bit data types.

Apart from this, it also provides a lot of derived data types e.g. singly linked lists, doubly linked lists, hash tables, stacks, queues, trees, and much more. It’d basically cover most of you data structure needs that you’d have otherwise had to implement yourselves. And it also provides helper functions that makes working with them so much more easier. If you have ever used Perl, and have wished that C programming could be a bit faster like it, you will be pleasantly surprised.

**4. Utility Functions: **GLib also provides various utility functions to ease out your manipulation of data. Some of the functions are meant as more secure and portable replacements for those provided with standard C library, while rest are meant to provide other useful functionality which you earlier had to implement in your code. Some of the major areas covered by GLib’s utility functions are String manipulation, character set manipulation and conversion (including unicode and base64), using regular expressions, file manipulation, shell functions, config file parsing (my favourite), etc.

End Note: I can understand that there would be a lot of people who believe that use of GLib is dumbing down programming in C. After all, we take pride being in control of our code and this is why we love progrmming in C because we have options to do things in our own way. Yes, that is all true but there comes a time where you’d like to spend more time in developing the core functionality of your app, or focus more on giving a rapid shape to your new idea, rather than reinventing the wheel and fumbling around with writing the helper functions. So, keeping that in mind I believe GLib is wonderful piece of code that has enabled me to churn out new apps that much faster.

Comments