This document introduces the GObject library. This cannot replace the official tutorial/reference. It is recommended to lookup the official reference every time you encountered new concepts.
--
After trying to learn GObject object system for more than ten times (and failed as many times as I have tried), I decided to try another approach.
Q: You are stupid.
A: Yeah. I think I am. Or I am just stuck.
Q: What made you stuck?
A: I have always believed that GLib/GObject is overly complicated and verbose and ugly in grammar and not suitable for everyday programming. See how many boilerplates you need to define a class. You need to include the name of the class in every method call. You need to cast a lot to supress warnings. ... All of these haunted me and told me "you could not understand it" from time to time.
Q: Then?
A: I must believe GObject is simple. (I really wonder if hypnotism work this well?)
-- Background information --
Q: What is GObject?
A: It is a part of the GLib library. It allows you to write object-oriented programs in the C language.
Q: What is GLib?
A: A utility library in the C language. It provides platform-transparency and helps in all trivial aspects like string manipulation, memory allocation, File/Network IO, concurrency/synchronization, event/signal/mainloop, dynamically loaded modules and a object-oriented type system, of course. I guess it is a helper library to make GTK+ easier to implement, but it is not bound to GUI programming.
:: For more information, visit
http://www.gtk.org/.
-- Blogging plan --
I will post more articles in my blog to record my process of learning. By the way, this document may contain error (feedback is always welcome). If you are in doubt, you should always consult the official GObject documentation which is the sub-ultimate authority of it.
Q: What is the ultimate authority, then?
A: The f**king source code, of course. It is always right because it is what the library is compiled from. Read it when the documentation fails to tell the whole story.
I shall create my own fundamental type, which I am not supposed to create since I am not Tim Janik. Then I shall see whether my code works. And then I will make it classed, instantiatable, inheritable. Then add properties, signals and implement interfaces. I will also consider GValue container later.
Q: Wait. Who is asking question in these Q/A paragraphs?
A: Myself. Many questions will be raised during learning. I will try to answer my own questions. (Be critical when reading my "answers")
-- A minimal GObject program --
Install GLib 2.x first. Download at
http://www.gtk.org/. If you use the GLib provided by your distribution, make sure the development package is installed. (glib2-devel for Fedora)
#include <glib-object.h>
int main() {
g_type_init(); // This is necessary
return 0;
}
Compile with:
引用
gcc `pkg-config --cflags --libs gobject-2.0` example.c -o example