Large C codebase – how to organize code?

4 points by DictumMortuum ↗ HN
I'm in the process of migrating the common libraries I used during my university years to github. They were the building blocks for larger implementations, like bplus trees, static hashing, etc.

I haven't really coded in C since then, so when I looked at the source code after all those years I found a large number of bad practices and I'm slowly working towards fixing.

My question is - is there any guide towards organizing large C codebases that provides info like:

- How to name header guards? - How to name variables used for the same purpose for consistency? - How to name globals? - How to name the API functions? etc

2 comments

[ 0.23 ms ] story [ 18.7 ms ] thread
Divide the code into modules which are as independent of each other as reasonably possible and thus can be reasoned about in isolation.

From the above follows that the modules do not hold global state and communicate with each other through well-defined interfaces - and not through global state.

Unfortunately C does not have an actual module system but only the "unit of translation" concept. In general you want two files per module e.g. my_module.c and my_module.h and prefix everything in there with my_module to avoid namespace conflicts e.g. my_module_new_foo(), my_module_update_foo(foo, MY_MODULE_FLAG | MY_MODULE_OTHER_FLAG)

EDIT: Note that the standard scheme to avoid holding global state in a module is that the module provides a function which allocates an "instance" of the module and all module functions work on such "instances" e.g.

  RNG_INSTANCE* r = rng_new();
  double v = rng_uniform(r);
  ...
  rng_free(r);