HOME BLOG ARCHIVE TAGS

Tricks of the Trade #2: amalgamating legacy C files

July 26, 2018

In large C++ projects, sometimes it’s hard to use legacy/stable C code without spreading rules/deps all over the build system.

Linkage declarations and preprocessor inclusion can be used to overcome this troublesome scenario. The idea is to “hitchhike” an existing (related) C++ file 0:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// common.h
extern "C"
{
  #include "legacy.h"
}

// common.cpp
extern "C"
{
  #include "legacy.c"
}

// some_file.cpp
#include "common.h"
// (...)

This is another case for file amalgamation, common in SCU/ unity builds 1.


Notes:
[0] - see mixing-c-and-cpp;
[1] - mostly implemented for optimization purposes (e.g., in WebKitGTK+ unified build);