Search code examples
c++clang-formatprecompiled-headers

How to make sure pch.h is the first included header using clang-format?


Some compilers require the pre-compiled-header to be included first. Clang-format can sort the headers (which is good). I want to make sure that pch will always be first.

How can I do this?

(My work around is to use IncludeBlocksStyle: Preserve and seperate #include "pch.h" from the rest.)


Solution

  • You can use negative priorities in IncludeCategories, so you can do something like this:

    IncludeBlocksStyle: Regroup
    IncludeCategories:
      - Regex: '"pch.h"'
        Priority: -1000
      # Other groups definitions here