藉助 Clang 工具建立 C/C++ 程式碼格式化工具


在本教程中,我們將討論一個藉助 clang 工具建立 C/C++ 程式碼格式化工具的程式。

設定

sudo apt install python
sudo apt install clang-format-3.5

然後,我們將建立一個 Python 檔案,其位置為當前使用者具有讀寫許可權的位置。

示例

import os
cpp_extensions = (".cxx",".cpp",".c", ".hxx", ".hh", ".cc", ".hpp")
for root, dirs, files in os.walk(os.getcwd()):
   for file in files:
      if file.endswith(cpp_extensions):
         os.system("clang-format-3.5 -i -style=file " + root + "/" + file)

在當前使用者的頂級目錄中建立一個檔案格式化檔案。

輸出

clang-format-3.5 -style=google -dump-config > .clang-format

最後,將此檔案複製到當前專案的頂級目錄中。

現在,你可以使用你自己的程式碼格式化工具了。只需執行建立的 Python,即可開始使用!

更新於:2020 年 3 月 16 日

157 次瀏覽

開始你的 職業生涯

完成課程即可獲得認證

開始
廣告