C - header files


I'm a fan of the C language for certain applications (mainly numerical computations). I'm fairly productive in it, but there is one thing I'm sick of: writing and organizing header files.

Are there any proposals to add a module system to the official C standard (like for C++)?

Sort of:

  import stdio, stdlib;

  module example_module;

  // exposed data structure
  struct some_public_structure { .. };

  static struct some_private_structure { .. }:

  // exposed function
  void public_function(..) {...}

  static void private_function(..) {...}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

C - header files

C won't be any more C without header files.

And you forgot one useful feature in header files: complex macros.

Did you consider to switch to something else, like e.g. Ocaml?

Of course I have played with

Of course I have played with OCaml (I liked it). But I'm more comfortable with C / C++. Anyway, I might give a serious try.

Modular Information Hiding and Type Safe Linking for C

Modular Information Hiding and Type Safe Linking for C. Saurabh Srivastava, Michael Hicks, Jeffrey S. Foster, and Patrick Jenkins.

This paper presents CMod, a novel tool that provides a sound module system for C. CMod works by enforcing a set of four rules that are based on principles of modular reasoning and on current programming practice. CMod's rules flesh out the convention that .h header files are module interfaces and .c source files are module implementations. Although this convention is well-known, existing explanations of it are incomplete, omitting important subtleties needed for soundness. In contrast, we have proven formally that CMod's rules enforce both information hiding and type-safe linking.

To use CMod, the programmer develops and builds their software as usual, redirecting the compiler and linker to CMod's wrappers. We evaluated CMod by applying it to 30 open source programs, totaling more than one million lines of code. Violations to CMod's rules revealed more than a thousand information hiding errors, dozens of typing errors, and hundreds of cases that, although not currently bugs, make programming mistakes more likely as the code evolves. At the same time, programs generally adhere to the assumptions underlying CMod's rules, and so we could fix rule violations with a modest effort. We conclude that CMod can effectively support modular programming in C: it soundly enforces type-safe linking and information hiding while being largely compatible with existing practice.

Very interesting! The CMod

Very interesting! The CMod homepage. Their implementation is in OCaml.


reducing it to an earlier joke."


Makeheaders may interest you.

The D Programming Language

Perhaps you should try D instead of C: http://www.digitalmars.com/d/

Quoted from the Overview section:
"Modules: Source files have a one-to-one correspondence with modules. Instead of #include'ing the text of a file of declarations, just import the module. There is no need to worry about multiple imports of the same module, no need to wrapper header files with #ifndef/#endif or #pragma once kludges, etc."


CMod looks very interesting. I'll keep an eye on this one.