Managing Headers with Include What You Use

Опубликовано: 13 Март 2024
на канале: Utah Cpp Programmers
564
14

Reading and tokenizing header files are consistently shown to be one of the most costliest parts of the C++ compilation process. Replacing header files with the binary representation of modules was a huge addition to the language in the C++20 standard. What can we do in the meantime until we switch to modules?

Include-What-You-Use is a clang-based tool designed to help you properly manage the header files you include in every source file. The idea is that if you reference an external symbol in a source file (one not defined in that source file), you should be directly including the header that declares that symbol. Directly including the header means the header file providing the declaration was included in the source file or in the header file corresponding to the source file, e.g. foo.h for the source file foo.cpp. Without direct inclusion of the appropriate header file, your source file is compiling "by accident" instead of "by design".

Include-What-You-Use can alert you to unnecessarily included headers (those that provide no declarations being used by the source file) as well as missing headers providing declarations that are being used directly.

This month, Richard Thomson will give us an introduction to the Include-What-You-Use tool and show how the tool can be incorporated into a continuous integration server with a GitHub action.

Example code: https://github.com/LegalizeAdulthood/...

Include What You Use: https://include-what-you-use.org/

Meetup: https://www.meetup.com/utah-cpp-progr...
Past topics: https://utahcpp.wordpress.com/past-me...
Future topics: https://utahcpp.wordpress.com/future-...