35
Intermediate
Skill guide
Formatting with clang-formatClangFormat (often called Clang) is a tool that allows you to automatically format C, C++, and Objective-C files. It makes your code more readable and saves your time and the time of anyone reviewing your code for a pull request by making it so neither of you has to worry about formatting very much. On Adafruit repositories, clang-format is run automatically on every commit and pull request, but you still have to run it locally since when it is run through a CI, it just tells you what needs to be reformatted without actually reformatting it.
Step 1) Install clang-formatFor mac/linux, you can install with a package manager
macOS:
brew install clang-format
brew install clang-format
If you don't already have Homebrew installed, you can do it here
Linux:
sudo apt install clang-format
sudo apt install clang-format
Windows:
Download the "Windows Installer" from the "Windows Snapshot Builds" section from the link below.
Step 2) Run itNavigate to the folder you'd like to run clang-format in and then run the following command, replacing File_To_Format.cpp with the filename of the file you'd like to format:
clang-format -i File_To_Format.cpp
clang-format -i File_To_Format.cppStep 3) Add an alias to your .bashrc (optional)
I've found it can be really useful to have one simpler command that runs clang on all the pertinent files in a directory, and I modified a command from our Arduino CI repository to do that. Here's how you can use that too.
Linux:
nano .bashrc
and hit enter.alias format='find . -name "*.cpp" -o -name "*.c" -o -name "*.h"|xargs -I {} clang-format -i {}'
source .bashrc
format
, and hit enter.These instructions may work on mac, but you will have to replace .bashrc
with .bash_profile
.
Page last edited March 08, 2024
Text editor powered by tinymce.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4