我的主目录中有 .clang-format
并设置 indentwidth 4 如下。
BasedOnStyle: LLVM
Standard: Cpp11
IndentWidth: 4
TabWidth: 4
UseTab: Never
但是当我使用 clang-format -style='~/.clang-format' a.cpp
格式化我的代码时,缩进宽度变为 2。例如:
// See this indent width 2. The original file has width 4,
// but clang-format changes it to width 2.
int main(int argc, char const *argv[]) {
A a;
a.bar();
clang-format --version 的输出是
LLVM (http://llvm.org/):
LLVM version 3.4.2
Optimized build.
Default target: x86_64-unknown-linux-gnu
Host CPU: core-avx-i
如何让 clang-format 格式化我的代码(.h,.c,..)缩进宽度为 4?
最佳答案
http://clang.llvm.org/docs/ClangFormat.html
-style
选项不采用文件路径。它使用字符串 file
来指示使用.clang 格式的文件,并在正在处理的文件的父目录中查找该文件,或者在转换时在工作目录及其父目录中查找该文件标准输入。
你也可以给它一个字符串,直接设置你想要的选项:
clang-format -style="{IndentWidth: 4,TabWidth: 4}"
您还可以使用 -dump-config
选项来检查配置。
-style='~/.clang-format'
使用 ~
来引用您的主目录通常依赖于 shell globbing。 shell 不会在这样的参数中为您做那件事。因此,即使 -style
确实选择了路径,也不会产生正确的路径。
关于c++ - 设置 IndentWidth 在 clang 格式中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26740500/