C++ Dependencies

Finding required header files

Assume you wrote a library. Or you inherited one and want to install it or make an installable package.

What include directories (or files) do you actually need to export for a given c++ library project?

In general there are headers that are meant to be included. Others are only used internally. But the first set of headers usually includes other headers, often from dependencies of the library. Which in turn may include other headers.

To find out which headers are visible (and thus required) for the depender (the user of the library) you can do the following.

How to compile the dummy file with -E?

If you are using cmake as a build system, it assembles the command line for your compiler for you, so often one doesn’t know how to compile “by hand”.

I usually get the compiler command from compile_commands.json, change -c to -E and execute it. To get that file set the variable CMAKE_EXPORT_COMPILE_COMMANDS to ON either in CMakeLists.txt:


or via the command line:


You can probably (I didn’t test it) also just set the -E option via add_compiler_options(yourtarget PRIVATE -E).