Welcome to Modular C

Table of Contents

Jens Gustedt

1 Introduction

Modular C is an extension of the C programming language that has been proposed by Jens Gustedt.

It consists in the addition of a handful of directives and a naming scheme transforming traditional translation units into modules.

The change to the C language is minimal since we only add one feature, composed identifiers, to the core language.

Our modules can import other modules as long as the import relation remains acyclic and a module can refer to its own identifiers and those of the imported modules through freely chosen abbreviations. Other than traditional C include, our import directive ensures complete encapsulation between modules.

The abbreviation scheme allows to seamlessly replace an imported module by another one with equivalent interface. In addition to the export of symbols, we provide parameterized code injection through the import of snippets. This implements a mechanism that allows for code reuse, similar to X macros or templates. Additional features of our proposal are a simple dynamic module initialization scheme, a structured approach to the C library and a migration path for existing software projects.

The preprint #8751 contains a first research report describing Modular C.

You can find a reference implementation of a compiler frontend with our gitlab project modular-c. In particular, you may download it at:

Constructive feedback, improvements and patches are highly welcome.

2 A reference implementation

The reference implementation still has its short comings but should already be useful. Please look into the code and the (few) Makefile of the source to see how it can be used.

Currently it provides

  • A complete interface to the C library, C, as defined in the C11 standard. The optional features <stdatomic.h> (C∷atomic) and <threads.h> (C∷thrd) are only provided if the platform provides them. The only environment that I know of that does that completely is musl.
  • A partial interface to POSIX, POSIX. In particular this has now a partial stub implementation of C11 threads that sits on top of POSIX threads, if the platform has no <threads.h>. Interfacing POSIX is a long project, since this is just huge. Please contact me if you would be willing to help.
  • A partial interface to Linux, Linux. This is similar to the above, and in fact refers in most parts just to the same code as for POSIX. But it also interfaces Linux specific stuff such as Linux∷futex.
  • A partial replacement of features from my P99 preprocessor library that is integrated into the C library module
  • Exclusive-Inclusive Linear locks have their own module hierarchy eilck. This is a thread level lock library that is implemented in Modular C and interfaced to plain C and C++. Besides its use as a lock library, this can be taken as an involved example how to write production code in Modular C by still remaining compatible to legacy interfaces.

Documentation is written in doxygen and can be generated from the sources. This documentation includes a discussion of all implemented directives as well as the module structure of the modular C library interface.

Author: Jens Gustedt

Created: 2021-09-29 Mi 14:45