tutorial: include-what-you-use for a cmake/mingw-build on win

Written by  on August 5, 2020 

tl;dr:
0. Get the latest prebuilt version for Win: version 0.8 (3 years old :/)
1. Put this somewhere into you CMakeLists.txt
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "C:\\include-what-you-use\\bin\\include-what-you-use.exe")
2. Enjoy the overflowing build-output for your legacy-project.


‘include what you use’ sounds like a good policy to avoid unnecessary artifacts/buildtime/clutter. More than a decade ago some Java-IDE hat something like this (suggestions for (missing) includes), PyCharm does it for Python-import, the ReSharper-plugin for VisualStudio could do this as well really efficiently for huge C++ code bases. But this is a CMake-based project for windows. What to do now?

0. Get ‘include-what-you-use’ from their page and extract to some directory.
1. Figure out how to use an OSS project on Windows: check this page as example for sparse documentation! As well: the projects page. Never mind … why are there no simple examples for all supported platforms?
2. Play around until you get something, which is accepted from CMake and Ninja .. jk.
2. Set this to the CMakeLists.txt of your choice:
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "C:\\include-what-you-use\\bin\\include-what-you-use.exe")

Additional help on SO: https://stackoverflow.com/questions/30951492/how-to-use-the-tool-include-what-you-use-together-with-cmake-to-detect-unused-he
Good additional page.


problems (WIP):
– false positives (forward include in .h, but the .cpp includes it and also needs it). If you follow the advice, it won’t build.
– rewriting Qt-includes: from “- #include <QtWidgets/QMessageBox> // lines 19-19” to “#include “qmessagebox.h” // for QMessageBox” -> definitely not good!
– Qt (especially in .moc from ui-files) reports false positives for header-files


Applying it on one of my tiny toy-projects gives the following output:

WTF? O_O
For seven files such a huge mess? Just follow the hints from “should add these lines:” and “should remove these lines:”, apply a bit of brainpower.

Category : C++cmakemingwstatic code analysisWin