Want To Program Smartly In C? Use GLib
Blog | Tech Blog | Secure Coding | Twitter | RSS Feed | Get Email Updates
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.
© Shantanu Goel | Want To Program Smartly In C? Use GLib
|
Liked this post? Get FREE Updates Subscribe to RSS feed |






This post has 12 comments
November 13th, 2008
The same might be said for the apache portable runtime.
November 13th, 2008
If there is no need to implement custom helper functions, there is actually no need program in C. In that case, you can use the one or the other scripting language, that will internally use the boilerplate algorithms.
The very purpose of taking on the cost of programming in C, is to implement specific helper mechanisms that will speed up things for that particular kind of application.
But then again, the source code for a library such as GLib is definitely a very good source of inspiration, when rolling your own helper mechanisms in C.
November 14th, 2008
Do you know stdint.h?
November 14th, 2008
Any possibility that GLib has lock-free data structures?
November 14th, 2008
@Erik
what’s an “helper mechanism” ?
November 14th, 2008
Glib is a good example for a widely used OSS lib.
What I don’t understand about it, is that it leaks memory and no one doesn’t seem to care about it. You can check and proove that with tools like Duma, Dmalloc, Valgrind etc.
e.g. you have to call g_type_init() which will set up Signal / Slot mechanism and allocate memory here and there. It has no counter part, like glib_deinit or glib_shutdown.
Same applies for DBus, which is even much more apreciated in the OSS world.
Besides those leaks that Libs are really power- and usefull. Hopefully one day the developers will investigate for those leaks.
BR
paines
November 14th, 2008
Beside portability, utility functions and data structures there’s yet another killer feature I love: glib’s main loop. It gives all you need: timers, file descriptor and child watches, idlers and possibility of plugging custom watches into main loop. Good both for gui and service daemons.
November 14th, 2008
@paines:
What would be the use of a glib_shutdown? The type systems is
supposed to run as long as the program runs. It doesn’t make
sense to shut down the type system unless the process is going
to terminate anyway.
November 14th, 2008
How portable is this to Windows? A quick look at the documentation, and it does not tell how to build or compile applications for it on Windows.
November 14th, 2008
and note: most of those memory leaks
are a part of your own ignorance
if you knew how glib operated, you’d know that
they aren’t mempry leaks …
any memory leaks you find are most likely going to be from your own code
November 15th, 2008
hi there
i’d like to learn how to programm (java/c/c++/mysql- which ever easy to start and similar)
the thing is i have no clue where to start im windows user 32bit, if there any one who kindly give me instruction step by step i’d appreceate it
November 15th, 2008
@mb: That is correct. Anyway I think, it isn’t good programming style to leave it to the OS to cleanup after you.
Trackbacks