cpp-include-sorter

Sort C/C++ #include statements while preserving conditional compilation blocks and comments.

31|6|Updated Jan 29, 2026
One-click install
npx skills add https://github.com/openharmonyinsight/openharmony-skills --skill cpp-include-sorter
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cpp-include-sorter
Source: https://github.com/openharmonyinsight/openharmony-skills/tree/main/skills/cpp-include-sorter
Command: npx skills add https://github.com/openharmonyinsight/openharmony-skills --skill cpp-include-sorter

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires argparse, re, sys, pathlib, and includes scripts (resource) components.

What problem does it solve?

This Skill fixes messy and inconsistent C/C++ #include ordering that makes code reviews harder and can break builds when conditional compilation blocks are not handled carefully.

Core Features & Use Cases

  • Intelligent include grouping: Places the corresponding header first, then system headers (<...>), then local headers ("..."), separated by blank lines.
  • Conditional compilation aware sorting: Sorts #ifdef/#ifndef/#if defined blocks as units, supports #elif/#else, and preserves block structure.
  • Safety checks and stability: Preserves preceding/inline comments, avoids losing headers, and validates correctness after reordering.
  • Use case: When preparing a PR, use it to standardize include blocks across many .cpp/.h files so reviewers can focus on logic changes rather than formatting noise.

Quick Start

Run include sorting on your target directory of .cpp files in dry-run mode to preview changes before committing.

Frequently Asked Questions about cpp-include-sorter

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I sort C++ include statements without breaking conditional compilation blocks?

To sort C++ include statements safely, this Skill reorders headers while treating #ifdef/#elif/#else regions as structural units, ensuring conditional compilation blocks remain valid and intact.

What is the correct order for grouping system and local headers in C++ source files?

The correct grouping order places the corresponding header first, followed by system headers enclosed in angle brackets, then local headers in quotes, with blank lines separating each distinct group.

How do I standardize messy C++ include formatting across a codebase before a PR review?

Standardize include formatting across a codebase by running this Skill in dry-run mode on your target directory, previewing the reordering changes, and then applying them to reduce formatting noise in PRs.

Will sorting C++ #include directives remove or misplace my existing comments?

Sorting C++ #include directives will not remove or misplace comments, as this Skill explicitly preserves preceding and inline comments attached to header statements during the reordering process.

Does C++ include sorting handle preprocessor directives like #if defined and #ifndef?

C++ include sorting handles preprocessor directives by sorting #if defined and #ifndef blocks as complete units, maintaining their internal structure and preventing broken conditional logic.

Can I preview C++ header reordering changes before committing them to my source files?

You can preview C++ header reordering changes before committing by executing the script in dry-run mode, which validates header counts and displays proposed modifications without altering files.