Search code examples
c++header-files

Dump C++ source with merged #includes


Is there a tool (or Gcc/Clang flag) that creates a single-header C++ file, from a header file that #includes multiple other headers (STL and custom ones)? STL header #includes should be left untouched.

For example, given these files:

// a.h
#pragma once
#include <my_lib/aa.h>
void baz() {}


// aa.h
#pragma once
void foo(int) {}


// b.h
#pragma once
void bar(float) {}


// master.h
#pragma once
#include <string>
#include <my_lib/a.h>
#include <memory>
#include <my_lib/b.h>
#include <vector>

Running the tool on master.h would give this output (whitespace may differ):

#pragma once
#include <string>
void foo(int) {}
void baz() {}
#include <memory>
void bar(float) {}
#include <vector>

If there are any #includes inside #ifdefs, they should be dumped as well.


Solution

  • I've created a script to do this.