Channels ▼
RSS

Design

General-Purpose C Libraries: Limited Options


No language ever came into being more in need of a library than C. The trembling, naked child that opened its eyes on the world it would soon conquer was armed with a mere nosegay of functions consisting of minimal string, memory, math, file I/O, and OS-interface functions. As a programmer it was possible (in fact, routine) to know all the library functions by heart when banging out C code. Only OS calls required consultation with manuals, and even most of those eventually found their ways into developers' muscle memory.

However, development was characterized by frequently writing (and rewriting) code to handle collections, memory management, date computations, networking, and so on. Many a programmer will recollect the innumerable implementations of linked lists that had to be written because, in fact, no standard library for them existed.

In light of such need, in addition to the great popularity of C, you would think that a wide variety of solutions would have flourished. But in fact, the number of general-purpose libraries for C is comparatively small. And those that make the cut are not exactly hot springs of activity.

For pure C, there are essentially three non-GUI choices: the Apache Portable Runtime (APR), glib, and the Mozilla Netscape Portable Runtime (NSPR).

All three libraries were offshoots of larger projects — of the Apache Web Server, GNOME, and Mozilla's many projects — so they're demonstrably portable and battle tested, although as we'll see, the support for users with questions about features or defects varies widely.

All three provide sophisticated memory management, network I/O functions, and a wide variety of collections. After that, each library takes its own path. The NSPR has decent online documentation. It appears to be developed and supported by a single individual who works for Red Hat. The mailing list is overgrown with spam, in which are buried news of updates. Questions by users seem to go unanswered. On the evidence, I'd say that Red Hat supports it to the extent it needs it, and there is no longer any community around the product.

The APR is a larger library containing an additional layer of higher-level functions that do things such as handle XML, database access, cryptography, and the like. And the APR community, while small, is active. The mailing list is maintained. However, there is no usable documentation for the library. The little there is consists of out-of-date Doxygen docs that only explain syntax. The design of the APR, the API to use for a given problem, and so on, is all data held in the heads of the cabal that updates it. This is obviously a seriously gating factor, made worse by the unusual memory allocation design APR uses. It's neither intuitive nor obvious, but it must be mastered to use the APR effectively. So, except for the needs of the Web server and other C projects (such as Subversion), which are developed from people originally exposed to the APR, it's a very difficult library to use.

In contrast, glib has acceptable documentation (far better than APR and somewhat better than NSPR). This would make it a natural choice, especially because it has the entire GNOME series of libraries and utilities in its ecosystem. However, glib is very much a child of UNIX/Linux parentage. To run it on Windows, you have to include the MinGW package. And while there is a minimal set of Win32-specific APIs, these are few in number and of limited use. The documentation is conspicuous in ignoring Windows and matters related to the platform. Given that GNOME is a Linux-oriented package, this may seem natural, but for many potential adoptees, this aspect will be offputting. If this limitation is not a problem, then glib is likely the best option here.

The upshot is that while these three C libraries fill in the missing functionality of the language, none of them is a compelling or natural choice. Each has limitations or obstacles that preclude enthusiastic adoption. In this way, C and C++ are vastly different languages. C++, for all the complaints about its complexity, does provide the STL and will soon include many Boost libraries in its own standard library — all of which are well documented, well supported, and widely used.


Coming Soon
Later this month, we'll be inaugurating a series of articles by Andrew Koenig and Barbara Moo on the new features in the C++0x standard.

— Andrew Binstock
Editor in Chief
alb@drdobbs.com
Twitter: platypusguy


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video