cmake include header into every source file cmake include header into every source file c c

cmake include header into every source file


CMake doesn't have a feature for this specific use case, but as you've hinted, compilers such as GCC have the -include flag which acts as if there was an #include "foo.h" in the source file, and since CMake can pass arguments to compilers, you can do it via add_definitions.

This answer covers what the flag is for GCC, Clang and MSVC which should cover a lot of bases. So in CMake, detect what the compiler is and pass the appropriate flag.

Here's what the CMake code might look like:

if(MSVC)    add_definitions(/FI"foo.h")else()    # GCC or Clang    add_definitions(-include foo.h)endif()

Comments

In general, doing this is a bad idea. Code inspection tools (like IDEs, or doxygen) will be confused by it, not to mention other humans looking at the code. If not all source files actually require the definition, adding extra #includes will slow down compile time. If you actually do need the same header (and it's not a system header) in all your source files, it may be symptomatic of high coupling in your code. And for what benefit? Not having to add one line to your files?

However, it's necessary to note that compilers support this for a reason; there are a few weird edge cases (example 1, example 2) where it's a useful thing to do.

Just be aware that you're doing this for the right reasons.


as pointed out in the examples above, "force including" could be useful for pre-compiled headers.

this is actually what cmake's target_precompile_headers does.https://cmake.org/cmake/help/git-stage/command/target_precompile_headers.html

for this context-of-use:

  1. force including the header is actually a nice feature, since it allows you to use precompiled headers with MSVC, clang and gcc without changing your source.

  2. it is also a nice feature, because it allows you to switch on/off PCH with just a configuration change (e.g. in cmake). this allows you to test that your source builds also without the precompiled headers (e.g. for compilers/situations that don't support it). for instance clang-tidy gets confused about PCH (at least in combination with MSVC).