Web development in C: crazy? Or crazy like a fox?

Preliminary verdict: crazy like a fox (ish)

Luc Perkins
6 min readSep 17, 2013

--

No one does web development in C these days—or at least it sure seems that way. Tons of people are doing great things in Rails and Django and Spring and tons of other frameworks and in just about any runtime you can imagine (even Haxe and OCaml and Lua), but you never read Hacker News stories along the lines of “Why we at CompanyX chose C for our web servers” or “Check out my new C web framework” or “Why C is the easiest thing ever with no drawbacks.”

The reasons for this aren’t terribly difficult to discern. Let’s face it: compared with higher-level languages, C is hard. Like, objectively hard. Many of its abstractions, like structs and void pointers, have become pretty remote from the daily practice of most developers. And when was the last time you wrote a function that returned 1 or 0? If you’re a Rubyist or a Pythonista or a server-side JavaScript devotee, you may never have done so.

Another barrier: there isn’t much of what we nowadays call an ecosystem surrounding web development in C, at least that I’ve found, unless it’s tucked away in unseen corners of the digital universe (which I suppose is not inconceivable). And so if you’re looking for a Facebook SDK or an easy-to-use ORM or a dead-simple Sinatra-style routing DSL or one of the myriad other tools that so many web developers rely upon heavily these days, you might come up empty-handed if you look for their C counterparts.

In other words, if you decide to do web development in C, you’ll probably have to go it alone in building large parts of your application stack—maybe even all of it. But there are signs that that could be changing, and I think that this could be an encouraging development.

Frameworks

While some Lone Wolf McQuade-style developers prefer to work without frameworks (or to create their own from scratch), let’s face it: frameworks can offer enormous productivity gains. And if you use solid frameworks in a careful fashion, those productivity gains can be reaped at little detriment to things like performance and customizability.

There aren’t a whole lot of frameworks for C, but there are a few that I’ve come across that are worth mentioning:

Onion seems to be the most widely used and well supported framework out there. It’s been featured in a number of benchmark games (like here) and offers support for multiple Linux distros (Debian and Gentoo at the moment). It has support for threading, websockets, I18N, and SSL, and it even comes with a templating engine, otemplate, which is inspired by Django’s templating engine. Even better, the GitHub repo comes replete with numerous example projects for the uninitiated. Onion seems to be the winner of the bunch.

But there are others worth mentioning. Kore seems to be essentially a replacement for PHP. It’s a templating-driven framework with a simple API for building “dynamic websites,” which should provide some indication as to its general purpose: to bind specific pages to functions called by worker processes. I’m not sure if there are a lot of great use cases for Kore nowadays, but maybe worth a shot as a replacement for Jekyll (thought doing this would require a lot of work).

Klone is like Kore in that it seems designed as more of a templating engine and intended for “LAMP-like systems.” But it seems to be a slightly more well-rounded web server, with support for forms, cookies, file uploads, sessions, and so on. But it lacks Onion’s feature set by a wide margin.

And of course no web development runtime ecosystem could be complete without a Sinatra-style microframework. Ladies and gentlemen, I present to you Bogart. The creator of Bogart, Tyler McMullen, has described the project as “(mostly) facetious.” But a project like Bogart at least has its heart in the right place, and I’d love to see someone created a fully fleshed out C microframework (or to see Bogart more actively maintained).

Database bindings, et al

Runtime-specific clients for databases, message queues, and other services have of course become an indispensable tool in web development. The productivity gains from well thought-out DB and MQ clients can be massive, and rolling out your own clients in-house only makes sense if you’re doing something pretty hardcore.

For C web development, there are actively maintained bindings for just about all of the DBs and MQs that you would expect: Redis (created by Redis creator Antirez himself), PostgreSQL, MySQL, MongoDB, RabbitMQ, Riak, zeroMQ, CouchDB, and so on. These bindings vary in quality and in the level of activity surrounding them, but they’re out there, and they’re usually maintained by pretty hardcore folks.

The state of things in the C universe isn’t even in the same ballpark as it is for Ruby, NodeJS, and other communities. But there’s currently a lot more than nothing, and all of the above projects range somewhere between promising and great.

Other tools

So you’ve picked a framework and a couple of DB bindings, written your requisite to-do list app (or hopefully something more substantial), and now you need to actually deploy this thing. One way to do that is on a VPS, of course, but what if you want to run it on a Paas? A few years ago that would have been impossible, but nowadays there are Heroku buildpacks (like this one).

Even better, dotCloud’s Docker has made it possible to run anything in containers, and there are indications that such Linux containers could one day be the basis of most or even all cloud applications. If that’s the case, then running C web applications on the platforms of the future will proceed exactly like running applications in any other runtime, which would be a nice boost to C web development in general.

High-level development in C

Yes, I know, C is a “low-level” language, probably the lowest-level all-purpose language that is readable by semi-ordinary human beings. But it’s becoming possible to use C in a way that is far more grokkable for those coming from object-oriented and functional backgrounds. The best example of this that I’ve found: Cello.

The beauty of Cello is that it isn’t a newfangled runtime that compiles to C, it isn’t C++, and it isn’t some kind of monstrosity that violates the purity of C. Nope, it’s just plain C with a lot of syntactic sugar on top. You include it like you would include any other C library with simple #include <Cello.h> up at the top, and once you’ve done that you can start working with constructions like foreach and map and with, lambdas/anonymous functions, try/catch routines, straightforward lists, deletion via a simple delete() function, and so on. There are even classes if you’re into that sort of thing.

The potential impact of a library like Cello goes well beyond web development, of course. But it really does seem to promise a pretty broad streamlining of C into something vastly more user-friendly than it has been in the past. And that would be great news for C web development.

A bright future?

If you take all of the above factors into consideration, there seems to be a solid set of stepping stones for C web development in the future. It remains to be seen if developers outside of the C community—or even within it for that matter—jump on the wagon.

I actually plan on building a basic CRUD app using Onion and Postgres (via libpq) and documenting the process. I’ll keep you posted on that.

In the meantime, I’m curious: are there other frameworks that I’ve missed? Are there other tools that deserve mentioning? Are there important aspects of C web development that are worthy of attention that I haven’t raised here? Please feel more than free to weigh in here on Hacker News.

--

--

Luc Perkins

Technical writer @streamlio. Ph.D political theorist.