Skip to content

(git) clang-format on Windows using LLVM

This is just a quick summary on how to use clang-format and in particular git clang-format on a Windows machine.

clang-format is a great and flexible tool to format your code automatically. jetbrains summarizes it well:

“Clang-Format is a widely-used C++ code formatter. As it provides an option to define code style options in YAML-formatted files — named .clang-format — these files often become a part of your project where you keep all code style rules.”

I believe it speeds up development as I can just type away and then auto-format rather than having to manually indent and align things. This is especially useful if you are refactoring old code, but also when writing new code. I puts the code in a standard format which in turn makes it easier to read and understand. No more lines that are 200 characters wide!

For reference, my currently used .clang-format file can be found here.

Installing clang-format on Windows using LLVM

Getting clang-format running on Windows is fairly easy, as it’s contained in the LLVM compiler. Simply download and install LLVM to the default path, using either their installer or better yet, using winget with a simple:
winget install -e --id LLVM.LLVM -y

Once installed, it can be either added to PATH to run from anywhere in the command line. Assuming you are using Visual Studio Code (which you should!), using clang-format is very easy using Clang-Format extension:

  • Install the extension
  • Set the correct executable path using
    "clang-format.executable": "c:\\Program Files\\LLVM\\bin\\clang-format.exe"
  • (optional) Place a .clang-format file in your workspace directory, if none is present the default style options will be used
  • right-click in any source file and click “Format Document” or use the keyboard shortcut Shift+Alt+F

Using git clang-format

I use clang-format all the time, but my coworkers are less strict and thus I often end up with poorly formatted code and once I make code changes I will also format the code. However, that messes up the git commit with changes that are not related to what I am actually working on. This makes it harder for my coworkers to identify what changes I made and makes the commit history messy in general.

Recently, I have stumbled over a better way to coerce my coworkers into using clang-format, whether they like it or not: git-clang-format. It’s been there alongside clang-format the whole time, but only the “Format your code – all the time” blog post by Erik Larsson made me aware of it.

In short, git clang-format enables you to automatically run formatting on all files changes during a commit. So once you commit and push your code, it will be formatted correctly and thus the code changes and the correct formatting are contained in the same commit.

Running git clang-format on Linux is as simple as calling git clang-format, but with our LLVM install things are a tiny bit more tricky. git-clang-format is actually a python script, thus it requires a “Python 2.7 or Python 3” installation. The file can be found alongside clang-format in the LLVM bin folder (“C:\Program Files\LLVM\bin\git-clang-format” in my case).

To use it in your current workspace, just copy it in your root directory. From the interactive terminal or a normal PowerShell window, you can run
python .\git-clang-format
but you will get an error message saying “error: cannot find executable “clang-format””. However, we can provide a path to the script, and that should do the trick:

python .\git-clang-format --binary 'C:\Program Files\LLVM\bin\clang-format.exe'

If you have not changed any code since your last commit, you will get the message “clang-format did not modify any files”. Now, change some files but DO NOT run code-formatting yourself. When running the above command again, the script is very helpful, as it will notice the changes and will list them, but will also inform you that you need to “Please commit, stage, or stash them first.”

Go ahead and stage your changes. Running the above command yet again, we finally succeed:

You will have to stage the changed files again and you are ready to commit your properly formatted code!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I got started using these instructions, but they did not work for me out of the box. It seems the default Telegraf Docker Image is missing some SNMP tools / mibs files to function.

Here is how I got it working on my Synology NAS…

Start Default Telegraf Docker Container

We download and start the container with default config, we will later override that config so that we get Synology logging enabled. If we start the container with the “Synology” config already, it will never start because the standard MIBs files are missing and the error stops the container…
So to get started with a working container:

  1. In the Synology user interface, open the Docker Package
  2. Download and launch telegraf:latest Docker image
  3. A Create Container Dialog opens, go to Advanced Settings and apply:
    • Volume -> Add Folder -> mibs folder -> /usr/share/snmp/mibs
    • Network-> Check “Use the same network as Docker Host
    • Complete the creating process to launch the Docker container, it should say “Running” when you switch to “Container” in der Docker Add-on, similar to this:
  4. Log into the running container
    • Login to Synology NAS using SSH, see this how-to if you need help with that
    • Run sudo docker ps
    • your output should show a running telegraf container:
    • We need the name of the running container, in my case “telegraf1”
    • we open a shell inside the container by running
      sudo docker exec -it telegraf1 /bin/bash (where you need to replace “telegraf1” with your container name)
Install the missing MIBs files
  1. In the container shell, we first get ourselves a text editor and enable MIBs loading:
    • apt-get update – update the package manager
    • apt-get upgrade – upgrade all packages
    • apt-get install nano – install an editor, so we can edit files
    • open snmp.conf using nano /etc/snmp/snmp.conf
    • using your cursor, comment out line 4 using an “#” and save the file by pressing ctr+x and entering “Y
  2. Now, we actually add the MIBs
    • When we run snmptranslate -Tp we see that the MIBs files are missing
    • We install them, roughly following this guide but with some extra steps…
    • First, we need to add “non-free” packages to our source list, so we can install the MIBs packages
      • sudo nano /etc/apt/sources.list
      • add “non-free” to all non-commented lines (2, 4, 6 in my case) like so
      • close nano saving the file as before
      • Update package list, like before using apt-get update
    • Finally we can download all MIBs using
      apt-get install snmp-mibs-downloader
    • Now when we run snmptranslate -Tp, we should see a loooong and complicated looking table/tree with lots of entries:
    • We are done “inside” the container, so we can leave with exit
Restart container with Synology Config

Now that we have installed and enabled the missing MIBs, we can start our container with the real “functional” configuration file.

  1. Back in the Synology Docker Add-On, under “Container” stop the running telegraf container
  2. Edit the container and add another Volume
    • docker/telegraf/conf -> /etc/telegraf
  3. Start the container, in the logs the last entry should read
    “Using config file: /etc/telegraf/telegraf.conf”
Other potentially useful sources:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Find my version of “The Hex Game” here.

Being an embedded firmware engineer comes with regular conversions from hex (0xAF) to binary (1010 1111) and vice versa. The conversion rules are simple and there are many good calculators and tools to do the math for you.

I encounter lots of 8 to 32 bit registers in my daily coding life. Often single bits of those registers have to be written or read bitwise and I don’t want to have to either think about the conversion in my head for a longer time or use a converter. I’d much rather have this conversion become somewhat automatic in my head.

To practice quickly converting between 16 bit binary and hexadecimal numbers, I adapter “The Binary Game V2” which itself is a clone of “The Binary Game” by Cisco to display hex numbers rather than decimal numbers. Having never touched Javascript before, this took me longer than anticipated. I shall not touch Javascript again…

As a side note, converting large decimal numbers to hexadecimal is cumbersome by hand, as it involves long divisions and is better left to a computer.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.