In a collaborative project, code formatting software, such as Clang-Format, have been widely used to make sure that the code written by different people have uniform style and format. I have just learned some basic usages of Clang-Format and documented them in this blog post.
Installation
1
$ sudo apt-get install clang-format
Format Configuration
In order to format our code, we need a configuration file .clang-format. This file could be generated from some pre-configured styles.
To generate .clang-format from style llvm, run the following command in the terminal.
Other candidate styles include google, chromium, mozilla, webkit, microsoft.
We could further modify the .clang-format file using our preferences. Below is my modified format file. More style examples could be found from Clang documentations.
Clang-Format could format a single file or all files with the same file extension. For example, to format .cpp extension, run the following command in the terminal.
1
$ clang-format -i *.cpp
To format all .h, .c, .hpp, .cpp, .cu files together, run the following command in the terminal.