Assume that the compilation of an executable is done via
cd <EXEFOLDERs> && <CPPCOMPILER> -<ALL_KINDS_OF_FLAGS> <EXECUTABLE_NAME>
where ‘CPPCOMPILER’ is either ‘clang++’ or ‘g++’ (the flags used in this article work with these two compilers, but other compilers may define similar ones).
included upon the compilation of ‘EXECUTABLE_NAME’ can be
printed by simply adding the
to the compilation command. That is, for instance with g++:
cd <TARGETFOLDER> && g++ -<ALL_KINDS_OF_FLAGS> -H <TARGET>
If you want to write the included headers into a file instead, you can use the
-MF option. Moreover, you may also only be interested in headers from your project.
To this end, you can use the
-MM option such that system headers are not considered.
Thus, the final command to write these headers into a file named
looks like this:
cd <TARGETFOLDER> && g++ -<ALL_KINDS_OF_FLAGS> <TARGET> -MM -MF <deps_file>
If you are using a build tool like cmake or some Integrated Development Environment (IDE) for the configuration of your project, you probably don’t exactly know what the compile command looks like. In case that your configuration yields GNU Makefiles, you can find out the command by typing
make -B --dry-run <EXECUTABLE>
into the terminal from within your configured project environment. The
is necessary to print the full command even though a prior compilation had taken place.