What's the best way to programmatically create a header file for another project?

Here's the specific use case: one program fingerprints the device for discrete information like version number, id string, etc, and then creates a header file populating static structs/program constants. This header file is then consumed by multiple projects to define a type of that class.

I thought about reading writing in an xml or flat file, that means consuming projects need to know that structure of the and plug in libraries to read it.

  • 1
    I think this is better suited for SO. Apr 29, 2011 at 21:17
  • 1
    This question is fine here: Programmmers.SE is about write-boarding ideas. There's nothing about this question that involves getting into code.
    – user8
    Apr 29, 2011 at 21:32
  • These things can be done with a macro language like m4 which is very well suited to generate files from templates. Apr 29, 2011 at 21:55

2 Answers 2


If you're building on *NIX (Unix, Linux, or OSX), a common approach is to have a make rule that converts a .h.in to .h using sed (or perl). The implementation would vary on Windows, but what you should be aiming at is



Your fingerprinting tool (or a wrapper around it) then does simple text replacement, so you end up with

#define VERSION 2.3.4
#define COPYRIGHT "(C) 2011 Stack Overflow"
#define SOMETHING_ELSE "A bucket of fish"

You could use Ant to do replacements (although Ant isn't really the right tool for C programming)

Depending on what exactly you're fingerprinting, automake/autoconf might be worth investigating - on Windows you may need to run these under MinGW or cygwin.

Using a template like this means you can leave any header comments, macro definitions, etc. in the header template.

  • They often use m4 for this. It has a learning curve, but works out well in the long run. en.wikipedia.org/wiki/M4_(computer_language)
    – S.Lott
    Apr 29, 2011 at 22:28
  • m4 is usually used to generate code that then uses sed to create the macros, actually. Apr 30, 2011 at 0:51
  • Thanks, that's something like I was angling towards. The project is to poll a usb device, and produce a set of pre-populated #defs and structs which describe the device (e.g. hw id, transfer rate, etc) in a single file. You can then include that file in any number of projects, e.g. in Teensy usb programming projects.
    – Sorcerer13
    May 3, 2011 at 18:53

Your build automation infrastructure would deal with included headers. Consider using GNU make (or ninja, etc...). If your C compiler is GCC (or Clang), notice the -M and related compiler flags. You might also want to use ccache and/or precompiled headers to speed up incremental build time. But read Recursive make considered harmful and consider other build automation tools, perhaps even omake or scons.

Your Makefile could contain -include directives. With ninja, your build.ninja file is usually generated (e.g. by some ad-hoc Python or Guile script, by cmake or meson, etc...), etc...

Both Bismon and RefPerSys are generating more and more of their own header files (in Bismon: C files; in RefPerSys: C++ files). But so do s48, CAIA (see this, old tarball here), and Chicken/Scheme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.