天天看點

Qt Creator使用clang-format實作源代碼格式化排版(Windows/macOS1)

本篇先針對Windows系統來描述,末尾再補充macOS

1、clang官網下載下傳clang-format.exe

https://releases.llvm.org/download.html

 下載下傳最新版本

https://github.com/llvm/llvm-project/releases

方法1:完整版

我們需要從官網下載下傳完整的LLVM編譯器,Pre-Built Binaries,Windows (64-bit),LLVM-9.0.0-win64.exe。安裝時,請勾選添加環境變量,Add  LLVM to the system PATH for all users。

安裝完畢之後,把C:\Program Files\LLVM\bin目錄下的clang-format檔案和clang-tidy複制到某個檔案夾裡,比如

F:\Qt\Qt5.12.7\Tools\QtCreator\bin\clang\bin9\  這裡的bin9是建立的檔案夾,差別于原來的bin

方法2:單獨版

單獨版就不需要從官網下載下傳完整的LLVM編譯器,因為完整版太大了,包含很多其他工具,沒必要。

我們僅僅從

https://llvm.org/builds/

,下載下傳clang-format-2663a25f.exe即可,網頁截圖如下:

Qt Creator使用clang-format實作源代碼格式化排版(Windows/macOS1)

下載下傳後把clang-format-2663a25f.exe拷貝到路徑F:\Qt\Qt5.12.7\Tools\QtCreator\bin\clang\bin\

clang-format官方文檔說明

http://clang.llvm.org/docs/ClangFormatStyleOptions.html https://clang.llvm.org/docs/ClangFormat.html

如果你的電腦安裝了VS2017以上版本,那麼該版本自帶了clang-format.exe,路徑是C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\vcpackages

但是VS2017自帶的clang-format的LLVM版本是v6.0,比較舊,不建議使用了。

2、Qt Creator Clang-Tidy設定

在Qt Creator裡,點選Tools(工具)->Options(選項)->Analyzer(分析器),

在Clang-Tidy填寫F:\Qt\Qt5.12.7\Tools\QtCreator\bin\clang\bin\clang-tidy.exe

在Diagnostic configuration裡選擇Clang-Tidy and Clazy preselected checks [built-in]。

Qt Creator使用clang-format實作源代碼格式化排版(Windows/macOS1)

3、Qt Creator Clang-format插件

Qt Creator,在Help欄裡,選擇About Plugins,勾上Beautifier,重新開機qtcreator;

Qt Creator使用clang-format實作源代碼格式化排版(Windows/macOS1)

選擇Tools->Options,點選左欄的Beautifier,選擇Clang Format;

Clang format的路徑填寫:F:\Qt\Qt5.12.7\Tools\QtCreator\bin\clang\bin\clang-format-2663a25f.exe

在Options裡選擇use customized style,Add建立一個配置,名稱随意取,例如myclangconfig;

Qt Creator使用clang-format實作源代碼格式化排版(Windows/macOS1)