Learning C

A few months ago, this post came out by Daniel Colascione, a contributor to GNU Emacs, my editor of choice. I found it hilarious, and scary, and exciting. And it made me want to learn more about GTK+ and how to build GUIs. Since then I’ve come across the Suckless organization’s page and gotten even more interested in the craft of programming. I particularly enjoyed following the ‘Related Links’ section of that page. This ties well with how I like fiddling with Golang. I enjoy working with Ruby, but working with Golang makes me feel the same purity that I feel when working in Emacs as opposed to RubyMine.

Since reading the emacs-gtk+ post, I subscribed to the emacs-devel mailing list and followed the brouhaha that happened with the thread starting here. And now there is a new issue that started out here about a ‘portable dumper’ for emacs. There is a lot of tension in this thread, with the contributor offering to fork GNU Emacs in the first 5 emails, and one of the maintainers offering to quit the position of a maintainer as a response to that. Now, the last time I was a pseudo-bug-tester lurking in the mailing list of a free-software project was approximately a decade ago, and that was before I knew programming. Each organization has its own personality, and I don’t know if this tension is just me being a noob on the personality of the emacs-devel mailing list, but it certainly concerns me when a maintainer makes it sound like the project could be dying due to a lack of maintainers.

This bugs me. While I’ve been donating to the FSF (it manages the GNU project), I don’t know how much of that goes into helping Emacs specifically. I dabble with other editors, but always come back to GNU Emacs. Since Eli Zaretskii (one of the maintainers) has said that the number of people willing and able to work on Emacs at the C-level is dwindling, I’m going to take this as a reason to (1) learn C programming, and (2) learn the C-internals of Emacs so that I can (3) contribute as a programmer to this project at some point in the future when I’ve done enough learning. I don’t want Emacs to die out.

I will focus on the C side of emacs since (1) there appear to be more people willing to work on elisp code (60% of the code), and (2) I think learning C is a lot more transferable skill than elisp.

Lisp Objects

According to the Hacker Guide and ‘src/lisp.h’, a Lisp_Object in C-land is usually an INT. Thats right, an integer. Not even a struct. Well, there is a compiler flag which which you can ask for Lisp_Object to be a struct, and then its made as a struct with one field in it, which is an INT! Yup, a Lisp_Object in Emacs is typedef’d to a ‘__int64’. The Emacs source uses the 3 least significant bits of the int64 to act as a ‘tag’ that denotes the type of the object, and the rest of the int64 is set to a memory address of the ‘value’ of the object. Right, C, low-level language. Welcome to the jungle.

Progress in small steps

After hitting my head against the wall on emacs source-code for a week or two, I figured I’m a little out of my league. So I started looking for something smaller to sink my teeth into. I’ve made a new repository of C code on my github account, and learned how to interact with PostgreSQL in C. And today I learned about GLib.

GLib is a library of utilities, and was extracted from GTK+. GTK+ is a widget toolkit that was extracted from GIMP. I got tired of reading a GTK+ book and wanted to fiddle with code, and learned more about GLib and found out that there is a JSON library that fits in with GLib datastructures. Perfect! GLib could make things way easier for a C programmer; utility libraries rock. It provides a host of datastructures and other modules that aren’t provided by the C language and aren’t provided by the standard library. Datastructures like linked-lists. Life is easier with more powerful datastructres.

One of best things I like about GLib is that its so well documented. I can look through a tutorial for json-glib and figure out how to get started, and then open GDB to see what datatype I have, and then its so simple to look up what operations I can do on that datastructure. I love this. Great documentation makes life so much easier.

Having said that, I have a new-found appreciation for higher-level languages. I can definitely appreciate why the GNU project is pushing for Guile as its official extension language; a programmer would be able to do most activities in a high-level language (Scheme, essentially), and call down to C-level when they find they actually have a performance bottleneck, or some functionality is already provided by a C library thats been in production for >20 years. Genius!

Biggest hurdle so far

The biggest issue I’ve had so far with working in C was in trying to figure out how to point GCC to the correct include files that are strewn all over the system. C doesn’t have something as fancy as Ruby’s bundler, and it was a pain to type in each and every ‘-I’ and ‘-l’ option. And then I discovered pkg-config. Its great, and I’m very happy that something like this exists. Good stuff.

Leave a Reply