Linux环境vscode clang-format格式化:vscode clang format command is not available亲测有效!

文章讲述了在VSCode中安装并配置clang-format插件时遇到的错误,原因在于设置中的工具路径不正确。作者提供了亲测有效的解决方案,即确认本地安装了clang-format工具,找到其正确路径(如/usr/lib/llvm-13/bin/clang-format),并在VSCode设置中更新插件执行文件路径。

问题现象

vscode安装了clang-format插件,但是使用就报错
格式化报错

问题原因

设置中配置的clang-format插件工具路径不正确。

解决方案-亲测有效!

  1. 确认本地安装了clang-format工具:终端输入clang-format(也可能是clang-format-13等版本,建议tab自动补全一下),如果没有,安装提示安装
    在这里插入图片描述
  2. 我的本地安装的是clang-format-13,查看路径在/usr/lib/llvm-13/bin/clang-format
Zhenxing.Wang3@123:~/data/my_code$ whereis clang-format-13
clang-format-13: /usr/bin/clang-format-13 /usr/share/man/man1/clang-format-13.1.gz
Zhenxing.Wang3@123:~/data/my_code$ cd /usr/bin/
Zhenxing.Wang3@123:/usr/bin$ ll |grep clang
lrwxrwxrwx  1 root           root                 31 Jul  6  2022 clang-format-13 -> ../lib/llvm-13/bin/clang-format*
-rwxr-xr-x  1 root           root               5129 Jul  6  2022 clang-format-diff-13*
lrwxrwxrwx  1 root           root                 35 Jul  6  2022 git-clang-format-13 -> ../lib/llvm-13/bin/git-clang-format*
  1. ctrl+, 打开设置选项,搜索format,将clang-format.executable值设置为刚刚找到的路径即可
    在这里插入图片描述
/eap/.clang-format: line 1: BasedOnStyle:: command not found /eap/.clang-format: line 2: Language:: command not found /eap/.clang-format: line 3: AccessModifierOffset:: command not found /eap/.clang-format: line 4: AlignAfterOpenBracket:: command not found /eap/.clang-format: line 5: AlignConsecutiveAssignments:: command not found /eap/.clang-format: line 6: AlignConsecutiveDeclarations:: command not found /eap/.clang-format: line 7: AlignOperands:: command not found /eap/.clang-format: line 8: AlignTrailingComments:: command not found /eap/.clang-format: line 9: AllowAllArgumentsOnNextLine:: command not found /eap/.clang-format: line 10: AllowAllParametersOfDeclarationOnNextLine:: command not found /eap/.clang-format: line 11: AllowShortFunctionsOnASingleLine:: command not found /eap/.clang-format: line 12: AllowShortIfStatementsOnASingleLine:: command not found /eap/.clang-format: line 13: AlwaysBreakAfterDefinitionReturnType:: command not found /eap/.clang-format: line 14: AlwaysBreakAfterReturnType:: command not found /eap/.clang-format: line 15: AlwaysBreakBeforeMultilineStrings:: command not found /eap/.clang-format: line 16: AlwaysBreakTemplateDeclarations:: command not found /eap/.clang-format: line 17: BinPackArguments:: command not found /eap/.clang-format: line 18: BinPackParameters:: command not found /eap/.clang-format: line 19: $'BraceWrapping:\r': command not found /eap/.clang-format: line 20: AfterCaseLabel:: command not found /eap/.clang-format: line 21: AfterClassKeyword:: command not found /eap/.clang-format: line 22: AfterControlStatement:: command not found /eap/.clang-format: line 23: AfterEnumKeyword:: command not found /eap/.clang-format: line 24: AfterFunctionKeyword:: command not found /eap/.clang-format: line 25: AfterNamespaceKeyword:: command not found /eap/.clang-format: line 26: AfterStructKeyword:: command not found /eap/.clang-format: line 27: AfterUnionKeyword:: command not found /eap/.clang-format: line 28: AfterCXXCatchKeyword:: command not found /eap/.clang-format: line 29: AfterElse:: command not found /eap/.clang-format: line 30: BeforeElse:: command not found /eap/.clang-format: line 31: BeforeWhile:: command not found /eap/.clang-format: line 32: BreakBeforeBinaryOperators:: command not found /eap/.clang-format: line 33: BreakBeforeBraces:: command not found /eap/.clang-format: line 34: BreakBeforeTernaryOperators:: command not found /eap/.clang-format: line 35: BreakConstructorInitializersBeforeComma:: command not found /eap/.clang-format: line 36: BreakInheritanceListBeforeComma:: command not found /eap/.clang-format: line 37: BreakInheritanceListBeforeColon:: command not found /eap/.clang-format: line 38: BreakStringLiterals:: command not found /eap/.clang-format: line 39: ColumnLimit:: command not found /eap/.clang-format: line 40: CommentPragmas:: command not found /eap/.clang-format: line 41: ConstructorInitializerAllOnOneLineOrOnePerLine:: command not found /eap/.clang-format: line 42: ConstructorInitializerIndentWidth:: command not found /eap/.clang-format: line 43: ContinuationIndentWidth:: command not found /eap/.clang-format: line 44: Cpp11BracedListStyle:: command not found /eap/.clang-format: line 45: DerivePointerAlignment:: command not found /eap/.clang-format: line 46: DisableFormat:: command not found /eap/.clang-format: line 47: ExperimentalAutoDetectBinPacking:: command not found /eap/.clang-format: line 48: FixNamespaceComments:: command not found /eap/.clang-format: line 49: $'ForEachMacros:\r': command not found /eap/.clang-format: line 50: -: command not found /eap/.clang-format: line 51: -: command not found /eap/.clang-format: line 52: -: command not found /eap/.clang-format: line 53: $'IncludeCategories:\r': command not found /eap/.clang-format: line 54: -: command not found /eap/.clang-format: line 55: Priority:: command not found /eap/.clang-format: line 56: -: command not found /eap/.clang-format: line 57: Priority:: command not found /eap/.clang-format: line 58: -: command not found /eap/.clang-format: line 59: Priority:: command not found /eap/.clang-format: line 60: IncludeIsMainRegex:: command not found /eap/.clang-format: line 61: IndentCaseLabels:: command not found /eap/.clang-format: line 62: IndentCaseBlocks:: command not found /eap/.clang-format: line 63: IndentGotoLabels:: command not found /eap/.clang-format: line 64: IndentPPDirectives:: command not found /eap/.clang-format: line 65: IndentWidth:: command not found /eap/.clang-format: line 66: IndentWrappedFunctionNames:: command not found /eap/.clang-format: line 67: JavaScriptQuotes:: command not found /eap/.clang-format: line 68: JavaScriptRegex:: command not found /eap/.clang-format: line 69: KeepEmptyLinesAtTheStartOfBlocks:: command not found /eap/.clang-format: line 70: MacroBlockBegin:: command not found /eap/.clang-format: line 71: MacroBlockEnd:: command not found /eap/.clang-format: line 72: MaxEmptyLinesToKeep:: command not found /eap/.clang-format: line 73: NamespaceIndentation:: command not found /eap/.clang-format: line 74: ObjCBlockIndentWidth:: command not found /eap/.clang-format: line 75: ObjCSpaceAfterProperty:: command not found /eap/.clang-format: line 76: ObjCSpaceBeforeProtocolList:: command not found /eap/.clang-format: line 77: PenaltyBreakAssignment:: command not found /eap/.clang-format: line 78: PenaltyBreakBeforeFirstCallParameter:: command not found /eap/.clang-format: line 79: PenaltyBreakComment:: command not found /eap/.clang-format: line 80: PenaltyBreakFirstLessLess:: command not found /eap/.clang-format: line 81: PenaltyBreakString:: command not found /eap/.clang-format: line 82: PenaltyBreakTemplateDeclaration:: command not found /eap/.clang-format: line 83: PenaltyExcessCharacter:: command not found /eap/.clang-format: line 84: PenaltyReturnTypeOnItsOwnLine:: command not found /eap/.clang-format: line 85: PointerAlignment:: command not found /eap/.clang-format: line 86: ReflowComments:: command not found /eap/.clang-format: line 87: SortIncludes:: command not found /eap/.clang-format: line 88: SortUsingDeclarations:: command not found /eap/.clang-format: line 89: SpaceAfterCStyleCast:: command not found /eap/.clang-format: line 90: SpaceAfterLogicalNot:: command not found /eap/.clang-format: line 91: SpaceAfterTemplateKeyword:: command not found /eap/.clang-format: line 92: SpaceBeforeAssignmentOperators:: command not found /eap/.clang-format: line 93: SpaceBeforeCaseColon:: command not found /eap/.clang-format: line 94: SpaceBeforeCpp11BracedList:: command not found /eap/.clang-format: line 95: SpaceBeforeCtorColon:: command not found /eap/.clang-format: line 96: SpaceBeforeInheritanceColon:: command not found /eap/.clang-format: line 97: SpaceBeforeKeywords:: command not found /eap/.clang-format: line 98: SpaceBeforeParens:: command not found /eap/.clang-format: line 99: SpaceBeforeRangeBasedForLoopColon:: command not found /eap/.clang-format: line 100: SpaceInEmptyParentheses:: command not found /eap/.clang-format: line 101: SpacesBeforeTrailingComments:: command not found /eap/.clang-format: line 102: SpacesInAngles:: command not found /eap/.clang-format: line 103: SpacesInContainerLiterals:: command not found /eap/.clang-format: line 104: SpacesInCStyleCastParentheses:: command not found /eap/.clang-format: line 105: SpacesInParentheses:: command not found /eap/.clang-format: line 106: SpacesInSquareBrackets:: command not found /eap/.clang-format: line 107: Standard:: command not found /eap/.clang-format: line 108: $'StatementMacros:\r': command not found /eap/.clang-format: line 109: -: command not found /eap/.clang-format: line 110: -: command not found /eap/.clang-format: line 111: -: command not found /eap/.clang-format: line 112: -: command not found /eap/.clang-format: line 113: -: command not found /eap/.clang-format: line 114: -: command not found /eap/.clang-format: line 115: -: command not found /eap/.clang-format: line 116: -: command not found /eap/.clang-format: line 117: -: command not found /eap/.clang-format: line 118: -: command not found /eap/.clang-format: line 119: -: command not found /eap/.clang-format: line 120: -: command not found /eap/.clang-format: line 121: -: command not found /eap/.clang-format: line 122: -: command not found /eap/.clang-format: line 123: -: command not found /eap/.clang-format: line 124: -: command not found /eap/.clang-format: line 125: -: command not found /eap/.clang-format: line 126: -: command not found /eap/.clang-format: line 127: -: command not found /eap/.clang-format: line 128: -: command not found /eap/.clang-format: line 129: -: command not found /eap/.clang-format: line 130: -: command not found /eap/.clang-format: line 131: -: command not found /eap/.clang-format: line 132: -: command not found /eap/.clang-format: line 133: -: command not found /eap/.clang-format: line 134: -: command not found /eap/.clang-format: line 135: -: command not found /eap/.clang-format: line 136: -: command not found /eap/.clang-format: line 137: -: command not found /eap/.clang-format: line 138: -: command not found /eap/.clang-format: line 139: -: command not found /eap/.clang-format: line 140: -: command not found /eap/.clang-format: line 141: -: command not found /eap/.clang-format: line 142: -: command not found /eap/.clang-format: line 143: -: command not found /eap/.clang-format: line 144: -: command not found /eap/.clang-format: line 145: -: command not found /eap/.clang-format: line 146: -: command not found /eap/.clang-format: line 147: -: command not found /eap/.clang-format: line 148: -: command not found /eap/.clang-format: line 149: -: command not found /eap/.clang-format: line 150: -: command not found /eap/.clang-format: line 151: -: command not found /eap/.clang-format: line 152: -: command not found /eap/.clang-format: line 153: -: command not found /eap/.clang-format: line 154: -: command not found /eap/.clang-format: line 155: -: command not found /eap/.clang-format: line 156: TabWidth:: command not found /eap/.clang-format: line 157: UseCRLF:: command not found /eap/.clang-format: line 158: UseTab:: command not found /eap/.clang-format: line 159: $'WhitespaceSensitiveMacros:\r': command not found /eap/.clang-format: line 160: -: command not found /eap/.clang-format: line 161: -: command not found
最新发布
08-19
你遇到的错误信息如下: ``` /eap/.clang-format: line 1: BasedOnStyle:: command not found ... ``` 这表明你当前的 `.clang-format` 文件被系统**当作 shell 脚本执行了**,而是被 `clang-format` 工具解析为配置文件。这是由于 `.clang-format` 文件的**格式正确或文件编码/换行符有误**。 --- ## 🔍 问题原因分析: 你复制的 `.clang-format` 文件中,每一行看起来像这样: ``` BasedOnStyle: Google Language: Cpp AlignAfterOpenBracket: Align ... ``` 但系统却把它当成了 shell 命令: ``` BasedOnStyle:: command not found ``` 这通常是因为: ### ❌ 错误方式: 你可能**手动创建了一个 `.clang-format` 文件并粘贴了内容,但文件格式对**,比如: - 文件被保存为 **可执行脚本**(例如误加了 `#!/bin/bash`) - 文件的**换行符是 Windows 风格(CRLF)**,而你在 Linux 环境下运行 - 文件被保存为错误的编码格式(如 UTF-8 with BOM) --- ## ✅ 正确做法:创建 `.clang-format` 文件 ### ✅ 方法一:使用 `clang-format` 自动生成 你可以使用 `clang-format` 自带的命令生成一个默认的 `.clang-format` 文件: ```bash clang-format -style=LLVM -dump-config > .clang-format ``` 然后你可以用文本编辑器打开 `.clang-format`,手动修改为你需要的格式风格(比如缩进为 4、BraceWrapping 设置为 Stroustrup 等)。 --- ### ✅ 方法二:正确粘贴配置内容 请确保你复制的 `.clang-format` 内容是纯文本格式,并且保存为 UTF-8 编码,**无 BOM**,**换行符为 LF(Unix)**。 #### ✅ 推荐做法: 1. 使用 VS Code 或其他编辑器新建 `.clang-format` 文件; 2. 将以下内容粘贴进去(这是你之前提供的配置,但修正了格式): ```yaml BasedOnStyle: Google Language: Cpp AccessModifierOffset: -4 AlignAfterOpenBracket: Align AlignConsecutiveAssignments: true AlignConsecutiveDeclarations: true AlignOperands: true AlignTrailingComments: true AllowAllArgumentsOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true AllowShortFunctionsOnASingleLine: None AllowShortIfStatementsOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false AlwaysBreakTemplateDeclarations: Yes BinPackArguments: true BinPackParameters: true BraceWrapping: AfterCaseLabel: false AfterClassKeyword: false AfterControlStatement: Never AfterEnumKeyword: false AfterFunctionKeyword: false AfterNamespaceKeyword: false AfterStructKeyword: false AfterUnionKeyword: false AfterCXXCatchKeyword: false AfterElse: false BeforeElse: false BeforeWhile: false BreakBeforeBinaryOperators: NonAssignment BreakBeforeBraces: Stroustrup BreakBeforeTernaryOperators: true BreakConstructorInitializersBeforeComma: false BreakInheritanceListBeforeComma: true BreakInheritanceListBeforeColon: true BreakStringLiterals: true ColumnLimit: 100 CommentPragmas: '^ IWYU pragma:' ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 Cpp11BracedListStyle: true DerivePointerAlignment: false DisableFormat: false ExperimentalAutoDetectBinPacking: false FixNamespaceComments: true ForEachMacros: - foreach - Q_FOREACH - BOOST_FOREACH IncludeCategories: - Regex: '^<.*\.h>' Priority: 1 - Regex: '^<.*' Priority: 2 - Regex: '.*' Priority: 3 IncludeIsMainRegex: '(Test)?$' IndentCaseLabels: true IndentCaseBlocks: false IndentGotoLabels: false IndentPPDirectives: After IndentWidth: 4 IndentWrappedFunctionNames: false JavaScriptQuotes: Leave JavaScriptRegex: false KeepEmptyLinesAtTheStartOfBlocks: true MacroBlockBegin: '' MacroBlockEnd: '' MaxEmptyLinesToKeep: 1 NamespaceIndentation: None ObjCBlockIndentWidth: 4 ObjCSpaceAfterProperty: false ObjCSpaceBeforeProtocolList: true PenaltyBreakAssignment: 2 PenaltyBreakBeforeFirstCallParameter: 19 PenaltyBreakComment: 300 PenaltyBreakFirstLessLess: 120 PenaltyBreakString: 1000 PenaltyBreakTemplateDeclaration: 500 PenaltyExcessCharacter: 1000000 PenaltyReturnTypeOnItsOwnLine: 60 PointerAlignment: Right ReflowComments: true SortIncludes: true SortUsingDeclarations: true SpaceAfterCStyleCast: false SpaceAfterLogicalNot: false SpaceAfterTemplateKeyword: true SpaceBeforeAssignmentOperators: true SpaceBeforeCaseColon: false SpaceBeforeCpp11BracedList: false SpaceBeforeCtorColon: true SpaceBeforeInheritanceColon: true SpaceBeforeKeywords: true SpaceBeforeParens: ControlStatements SpaceBeforeRangeBasedForLoopColon: true SpaceInEmptyParentheses: false SpacesBeforeTrailingComments: 1 SpacesInAngles: false SpacesInContainerLiterals: true SpacesInCStyleCastParentheses: false SpacesInParentheses: false SpacesInSquareBrackets: false Standard: Auto StatementMacros: - Q_ASSERT - Q_CHECK_PTR - Q_CHECK_EXCEPTION_THROWN - Q_FATAL - Q_TEST - Q_UNUSED - QT_CHECK_INIT - QVERIFY - QVERIFY2 - QCOMPARE - QCOMPARE2 - QSKIP - QFAIL - QWARN - QDEBUG - QEXPECT_FAIL - QFETCH - QFETCH_GLOBAL - QTEST - QTEST_GUI - QTEST_MOUSE - QTEST_KEYBOARD - QTEST_APP - QTEST_WIDGET - QTEST_WINDOW - QTEST_DIALOG - QTEST_MAIN - QTEST_GUI_MAIN - QTEST_MOUSE_MAIN - QTEST_KEYBOARD_MAIN - QTEST_WIDGET_MAIN - QTEST_WINDOW_MAIN - QTEST_DIALOG_MAIN - QTEST_APP_MAIN - QTEST_GUI_APP - QTEST_MOUSE_APP - QTEST_KEYBOARD_APP - QTEST_WIDGET_APP - QTEST_WINDOW_APP - QTEST_DIALOG_APP - QTEST_MAIN_APP - QTEST_GUI_MAIN_APP - QTEST_MOUSE_MAIN_APP - QTEST_KEYBOARD_MAIN_APP - QTEST_WIDGET_MAIN_APP - QTEST_WINDOW_MAIN_APP - QTEST_DIALOG_MAIN_APP TabWidth: 4 UseCRLF: false UseTab: Never WhitespaceSensitiveMacros: - STRINGIZE - MACRO_ARGS ``` 3. **保存为 `.clang-format` 文件,确保:** - **编码为 UTF-8(无 BOM)** - **换行符为 LF(Unix)** - **文件名无后缀,为 `.clang-format`** --- ## 🛠 检查和修复文件格式 ### ✅ 检查换行符: ```bash file .clang-format ``` 如果输出为: ``` .clang-format: ASCII text, with CRLF line terminators ``` 说明是 Windows 格式,需转换为 Unix 格式: ```bash dos2unix .clang-format ``` 如果没有安装 `dos2unix`,可以使用 `sed` 替换: ```bash sed -i 's/\r$//' .clang-format ``` --- ### ✅ 检查文件权限: ```bash ls -l .clang-format ``` 确保没有可执行权限: ```bash -rw-r--r-- 1 user group 3954 Apr 5 12:00 .clang-format ``` 如果有 `x` 权限,使用: ```bash chmod -x .clang-format ``` --- ## ✅ 验证 `.clang-format` 是否有效 你可以运行以下命令验证格式是否正确: ```bash clang-format -n -style=file your_source_file.c ``` 如果没有报错,并输出格式化后的内容,说明配置文件是正确的。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

There Is No Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值