Getting started with writing a GLib/GTK+ application in C

Here is a guide to get started, with the most useful links, books references and advice.

Update: a small "book" is now available: The GLib/GTK+ Development Platform.

For learning a specific subject you can also find short tutorials on the web instead of reading a book. However as your code base becomes bigger, maintenance is more important, and having a deeper understanding will be useful to keep a good code quality.

Prerequisites

  • Of course, if not already done, learn correctly the C language and do some exercises. The reference book is the K&R book.

  • Object-oriented programming (OOP) is also needed (see GObject below).
  • Reading a book on data structures and algorithms is also recommended.
  • If you want to develop your application on a UNIX system, another prerequisite is to know how UNIX works, the command line, a bit of shell scripting and how to write a Makefile. See this blog post for book references (e.g. UNIX for the Impatient, by Paul Abrahams).

  • Know a version control system like Git. A good book is Pro Git, by Scott Chacon.

Why the C language?

  • It is a static-typed language, lots of trivial errors are discovered at compile time. It is important for maintainability, and for doing code refactorings.
  • It is also an explicit-typed language, the variable types are visible in the code. It is a form of auto-documentation. Knowing the type of a variable is important to understand the code.
  • The C language has a very good toolchain: text editors (Vim, Emacs, …), gdb, valgrind, etc.
  • The verbosity of C has an advantage: the code can be "grepped" easily. For example the function gtk_widget_show() contains the namespace (gtk), the class (widget) and the method (show). With an object-oriented language, it is generally "object.show()". If we search "show" in the code, there will most probably be false positives. And knowing the namespace and the class of a method can be useful, it is also a form of auto-documentation.
  • When we develop an application, one day we may want to make some of the code reusable for other applications. If this code is written in C, it'll be easier to create a library that can be used by other programming languages (thanks to GObject introspection).
  • The GLib/GTK+ documentation is primarily written for the C language. And those libraries are themselves written in C.

Learn: GLib -> GObject -> GIO -> GTK+

First, look at the GTK+ web site. See also the GNOME developer center, and the platform overview.

The logical learning path is GLib -> GObject -> GIO -> GTK+. GObject is based on GLib; GIO is based on GLib and GObject; and GTK+ is based on the three previous libraries.

Unfortunately there is no good and recent book for an introduction to all these libraries.

  • For GLib: the old GTK+/Gnome Application Development book by Havoc Pennington is still relevant (but only the chapter 2 for GLib, some of the other chapters are outdated). The book is available for download. Skim also through the latest GLib API reference to know what features are available.

  • GObject: see the documentation, especially the part 1 "Concepts" and the tutorial.

  • GIO: skim through the API reference.
  • GTK+: see the "Getting Started with GTK+" tutorial in the documentation.

Pay attention to not use a deprecated API. Read the latest API references (you can also install devhelp, it's easier for searching through the API).

The important concepts of GLib/GObject is object-oriented programming, and event-driven programming (with a main event loop, signals, etc.).

Don't be afraid of the GObject boilerplate (macros, etc), and write your own GObject classes (with tools for generating the boilerplate). It's important to have a good object-oriented code architecture for your application, without global variables and the like.

Follow the GNOME code conventions, for the indentation, aligning parameters on the parenthesis, the source tree structure, etc. See this wiki page. If every program has different code conventions, it's a nightmare for someone willing to contribute.

A good way to learn GLib/GTK+ development is to contribute to an existing free/libre software, for example in GNOME or Xfce. Or you can simply read the source code to know how it is implemented, and learn about different possible code architectures.

Autotools

A Makefile is generally not sufficient if you want to install your application on different systems. The Autotools (autoconf, automake and libtool) is what GNOME and Xfce softwares use. There are some macros available for e.g. the user documentation, the code coverage support for unit tests, etc. The most recent book is Autotools, by John Calcote.

GTK-Doc, unit tests

GTK-Doc is the API documentation system used by GLib/GTK+. Writing GTK-Doc comments for your code is better, even for internal code not exposed in a library. You can generate HTML pages and read them in devhelp. When your code base becomes bigger, it's far more easier to have a good internal documentation.

Write also unit tests for the backend (your algorithms and data structures, the real work done by the application).

Programming best-practices

After having some practice, it is interesting to learn more about programming best-practices. Writing code of good quality is important for preventing bugs and for maintaining a software in the long-term.

Personal project vs contributing to an existing project

If you contribute to an existing project, you'll learn a lot by reading the code and with the review process when you submit patches. But if you don't have a lot of experience, it can be difficult to contribute to an existing project, as there are not a lot of easy tasks. And the review process can take some time, your patches may be ignored, so you can not work at your full capacity. With your own project, there is no reviews, you can produce more code. But without reviews and advice from more experienced developers, you'll make mistakes. You can anyway read source code from an existing program to learn how it is done, what is the code architecture, etc. And when in doubt, you can anyway ask questions on the GTK+ Forums, or a mailing list, IRC, etc.

You can start your own application to learn by yourself how to use GLib/GTK+. Once you have some experience, you can start contributing to an existing project, so you'll learn more quickly. In parallel you can still work on your application to apply what you've learned, and to produce more code to have more practice. Once you have a foot in a project, and if you're a student, it'll be more easier to get accepted for a Google Summer of Code or another outreach program.

When you develop an application, you can also contribute to the underlying libraries, when you come across a bug, or you want to improve the documentation so it is useful for other developers, or you want to develop a new feature (when you have more experience and have already contributed to the library), etc.

SébastienWilmet/DevGettingStarted (last edited 2016-08-07 18:38:01 by SébastienWilmet)