问题
I am trying to get clang-format to recognize the main include for foo.cpp
in the following setup:
project/my_lib
├── CMakeLists.txt
├── include
│ └── project
│ └── my_lib
│ └── foo.hpp
├── src
│ └── foo.cpp
└── tests
└── foo_test.cpp
And foo.cpp
has the following content:
#include <project/another_lib/bar.hpp>
#include <project/my_lib/foo.hpp>
#include <vector>
// ...
The problem I think I have identified is the following: clang tries to identify the 'main' include based on the file name, in this case foo.cpp
. And when it looks at #include <project/my_lib/foo.hpp>
it doesn't recognize this as the 'main' include for this cpp file. I know it is possible to specify suffixes for the "main include file" logic, but what would be needed here is a prefix regex.
Any help as to how I could get clang to set the right include to priority 0 in the example above?
Reference: Clang format options, section "IncludeCategories" and following
Edit: I am using clang-format
as integrated in Visual Studio 2019, which means I do not think I have access to the command line options. Moreover, this being a shared project, I'd like to work off of the default behavior.
回答1:
Just as a quick update, which kind of solved my issue: if the include uses regular quotes ("
) then clang-format bundled with Visual Studio seems to pick up the "main" header and order it accordingly.
Not ideal, but still a working solution.
来源:https://stackoverflow.com/questions/65305230/clang-format-main-include-sorting-with-folders-prefix