C++-使用clang-format格式化代码

clang-format既是一个库,也是一个单独的工具,它可以自动格式化代码。下面我们介绍如何在QtCreator中使用clang-format。

点击帮助->关于插件,勾选Beautifier

重启后,点击工具->选项->Beautifier->Clang Format,选择使用File定义风格

windows下,在.pro目录下创建.clang-format文件,linux下,在用户目录创建.clang-format文件。

我的格式化配置如下

复制代码
BasedOnStyle: Google
IndentWidth: 4
AccessModifierOffset: -4
BreakBeforeBraces: Custom
BraceWrapping:
    AfterFunction: true
ColumnLimit: 120
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: Empty

关于clang-format配置项有哪些以及含义都在参考链接1中,这里不再赘述。

general中可以选择保存时格式化,另外还可以添加格式化快捷键。

在使用windows qtcreator时,我这里默认使用clang-format.bat,并且报错

Error in text formatting: Could not format file xxxxx

我的解决方法是在https://github.com/llvm/llvm-project/releases下载LLVM-16.0.5-win64.exe

,解压后在bin下找到clang-format.exe,修改Clang Format command为exe。

参考链接:

  1. https://clang.llvm.org/docs/ClangFormatStyleOptions.html
  2. https://blog.csdn.net/xuyouqiang1987/article/details/128410408
相关推荐
肆忆_4 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星8 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc