skills/build-systems/include-what-you-use/SKILL.md
Include What You Use (IWYU) skill for optimizing C/C++ header includes. Use when reducing compilation cascades, interpreting IWYU reports, applying mapping files, deciding between forward declarations and full includes, or integrating IWYU with CMake. Activates on queries about IWYU, include-what-you-use, header bloat, reducing includes, forward declarations, compilation cascade, or slow C++ compilation from headers.
npx skillsauth add mohitmishra786/low-level-dev-skills include-what-you-useInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Guide agents through using IWYU to reduce unnecessary #include directives, interpret IWYU reports and mapping files, decide between forward declarations and full includes, and integrate IWYU into CMake builds to reduce compilation cascades in large codebases.
# Install
apt-get install iwyu # Ubuntu/Debian
brew install include-what-you-use # macOS
# Run on a single file
iwyu -Xiwyu --error main.cpp 2>&1
# Run via compile_commands.json
iwyu_tool.py -p build/ src/main.cpp 2>&1 | tee iwyu.log
# Run on entire project
iwyu_tool.py -p build/ 2>&1 | tee iwyu.log
# CMakeLists.txt — use IWYU as include checker during build
find_program(IWYU_PROGRAM NAMES include-what-you-use iwyu)
if(IWYU_PROGRAM)
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
${IWYU_PROGRAM}
-Xiwyu --mapping_file=${CMAKE_SOURCE_DIR}/iwyu.imp
-Xiwyu --no_comments
)
endif()
# Build with IWYU analysis
cmake -S . -B build -DCMAKE_CXX_COMPILER=clang++
cmake --build build 2>&1 | tee iwyu.log
main.cpp should add these lines:
#include <string> // for std::string
#include "mylib/widget.h" // for Widget
main.cpp should remove these lines:
- #include <vector> // lines 5-5
- #include "internal/detail.h" // lines 8-8
The full include-list for main.cpp:
#include <iostream> // for std::cout
#include <string> // for std::string
#include "mylib/widget.h" // for Widget
---
Reading the output:
# fix_include script (comes with IWYU)
fix_include < iwyu.log
# Options
fix_include --nosafe_headers < iwyu.log # more aggressive — also removes system headers
fix_include --comments < iwyu.log # preserve // comments in includes
fix_include --dry_run < iwyu.log # preview changes without applying
# Limit to specific files
fix_include --only_re='src/.*\.cpp' < iwyu.log
# Run and apply in one pipeline
iwyu_tool.py -p build/ 2>&1 | fix_include
IWYU prefers forward declarations (class/struct declarations without definition) when the full type isn't needed:
// full_include.h — DON'T include this if only a pointer is used
#include "widget.h" // full definition: Widget members, vtable, etc.
// forward_decl.h — OK when Widget* or Widget& is sufficient
class Widget; // forward declaration
void process(Widget *w); // pointer: forward decl is enough
// When forward declaration is sufficient:
// - Pointer or reference parameter: Widget*, Widget&
// - Return type as pointer: Widget*
// - Base class declared elsewhere (but defined in .cpp)
// When full include is required:
// - Inheriting from Widget: class MyWidget : public Widget
// - Accessing Widget members: w.field, w.method()
// - Creating Widget instances: Widget w;
// - Sizeof(Widget)
// - Template instantiation: std::vector<Widget>
// IWYU-friendly header
#pragma once
class Widget; // forward declare (saves downstream compilation)
class Container {
Widget *head_; // pointer: forward decl is enough
public:
void add(Widget *w);
Widget *get(int idx);
};
// Container.cpp includes "widget.h" — only .cpp pays the compile cost
IWYU mapping files teach IWYU about indirect includes (where #include <vector> is provided by some internal STL header):
# iwyu.imp — IWYU mapping file
[
# Map internal LLVM headers to public ones
{ "include": ["<llvm/ADT/StringRef.h>", "private",
"<llvm/ADT/StringRef.h>", "public"] },
# Map system headers to POSIX equivalents
{ "include": ["<bits/types.h>", "private", "<sys/types.h>", "public"] },
{ "include": ["<bits/socket.h>", "private", "<sys/socket.h>", "public"] },
# Symbol → header mappings
{ "symbol": ["std::string", "private", "<string>", "public"] },
{ "symbol": ["NULL", "private", "<cstddef>", "public"] },
]
# Use mapping file
iwyu -Xiwyu --mapping_file=iwyu.imp main.cpp
# IWYU ships with common mappings
ls /usr/share/include-what-you-use/
# gcc.stl.headers.imp, boost-1.62.imp, libcxx.imp, etc.
iwyu -Xiwyu --mapping_file=/usr/share/include-what-you-use/gcc.stl.headers.imp
IWYU has limits — be aware:
# IWYU may give wrong advice for:
# - Macros from headers (hard to track)
# - Template specializations in included headers
# - Headers required for correct ODR linking
# Safe iterative workflow:
# 1. Run IWYU
# 2. Apply fixes with fix_include
# 3. Rebuild and run tests
# 4. Revert any changes that break the build
# 5. Repeat until clean
# Check for compilation cascade: how many TUs include a header
grep -rl '#include "expensive.h"' src/ | wc -l
skills/build-systems/build-acceleration for ccache and other compile speed techniquesskills/build-systems/cmake for CMake integration of build analysis toolsskills/compilers/cpp-modules for C++20 modules as a long-term solution to include bloatdevelopment
QEMU/KVM skill for virtualization and kernel development. Use when running qemu-system-x86_64 with KVM, configuring virtio devices, VFIO passthrough, QMP monitor, libvirt, or booting custom kernels. Activates on queries about QEMU, KVM, virtio, VFIO, virsh, virt-install, or -kernel -append.
development
Hardware virtualization internals skill for Intel VT-x and AMD-V. Use when studying VMCS/VMCB, EPT/NPT page tables, VMEXIT handling, APIC virtualization, or building minimal hypervisors. Activates on queries about VMX, SVM, VMCS, EPT, NPT, VMEXIT, or type-1 hypervisor.
testing
Linux containers internals skill for namespaces, cgroups, and OCI. Use when understanding clone/unshare namespaces, cgroups v2 limits, overlayfs, runc, seccomp profiles, capabilities, or escape mitigations. Activates on queries about namespaces, cgroups, overlayfs, runc, seccomp-bpf, OCI spec, or container escape.
tools
Reverse engineering skill for binary analysis. Use when decompiling with Ghidra, analyzing with radare2, scripting RE tools, triaging with strings/file/xxd, or diffing binaries. Activates on queries about Ghidra, radare2, r2, decompiler, Binary Ninja, Diaphora, or stripped binary analysis.