5

I tried searching with pacman -Ss for clang-format, git-clang-format, etc, but I'm not having any luck.

However, I have seen some references to it on some other websites, implying it may exist (or have existed) somewhere.

Anaksunaman
  • 16,718
  • 4
  • 38
  • 45
Tyler Shellberg
  • 225
  • 1
  • 3
  • 12

5 Answers5

6

You can get it as part of the LLVM compiler build for Windows. You can download it from https://llvm.org/builds/. Once installed, clang-format.exe can be found in C:\Program Files\LLVM\bin.

I know it's a bit heavy to install the whole compiler just to get the formatter, but yeah, it just feels safer compared to other sources, given that it's from LLVM itself.

Besides, the installer holds an archive that several tools (e.g. 7-Zip) can open. If you have downloaded the installer, but do not want to install the whole thing, extract the single executable from the archive under bin.

Gautham
  • 161
  • 1
  • 2
  • 3
    One can directly download `clang-format.exe` and add it to PATH without installing LLVM. It is available at the bottom of the page [builds](https://llvm.org/builds/). – Burak May 11 '21 at 21:38
  • The link in builds is old and points to version 12. The latest builds can be found on the LLVM github releases and a quick search showed that version 16.04 has a win64.exe https://github.com/llvm/llvm-project/releases?page=1 – Greg Jun 04 '23 at 01:32
5

As utilities, clang-format and git-clang-format are available in several forms:

  • As a part of clang.

    [As noted by OP in the final comments below, and with OP's answer to their own question, these should be included with MSYS2/MingGW on Windows].

  • As pre-compiled Python wheels for Linux (only) available via pip (PyPI).

  • As modules available for Node.js. On Windows, use the standard Windows installer to install Node.js. Otherwise, example Linux package manager commands for installing Node.js are here.

Python

For Python versions on Linux, you may wish to try:

python3 -m pip install clang-format 

Node.js

Assuming Node.js is installed, you can also get them both with:

npm install -g clang-format

Note that on Windows, using the Node.js option seems to be the simplest solution for obtaining just clang-format and git-clang-format (without installing MinGW).


Anaksunaman
  • 16,718
  • 4
  • 38
  • 45
  • 1
    The nodejs modules are just wrappers around the real clang-format, from what I can tell on that page. Doesn't that mean the normal clang-format executable still has to be installed somewhere, or did I misunderstand? – Tyler Shellberg Nov 26 '19 at 16:52
  • Is npm available on mingw/mingw64? I attempted Pacman -Ss npm and as well for "node", but got no results related to node-js. – Tyler Shellberg Nov 26 '19 at 17:08
  • No luck with nodejs either. Wondering if it's possible to build clang-format from source. ```clang```, ```clang-analyzer```, ```clang-tools-extra```, ```compiler-rt```, ```libblocksruntime``` (all version 9) are all available on mingw. Apparently ```clang``` itself isn't enough to run the compiler though. – Tyler Shellberg Nov 26 '19 at 17:18
  • I saw that you updated your post. With Python3, I get: ```ERROR: Could not find a version that satisfies the requirement clang-format (from versions: none) ERROR: No matching distribution found for clang-format```. For nodejs, I cannot find how to install it on mingw. – Tyler Shellberg Nov 26 '19 at 18:02
  • Both of those alternative commands failed with the same error. No idea what you mean by "specialized". – Tyler Shellberg Nov 26 '19 at 18:18
  • I apparently have ```clang``` installed, though I cannot use it to compile anything. Here is an example of what pacman says regarding clang for my mingw: https://pastebin.com/peKkLrui – Tyler Shellberg Nov 26 '19 at 18:36
  • ```error: target not found: nodejs error: target not found: npm``` – Tyler Shellberg Nov 26 '19 at 18:51
  • No. Mingw is its own version of linux. It is not Arch, nor Ubuntu nor anything else. It is also not the Windows Subsystem for Linux, though it is similar in purpose. (At least as I understand things) – Tyler Shellberg Nov 26 '19 at 19:23
  • Mingw is not Windows, though it is running inside of Windows. But if I install things inside Windows, Mingw will not see them - they must be installed through Mingw. IE, if I have Python installed in Windows, Mingw will not see it from its command prompt. If I install Python through Mingw, I cannot use it through a Windows shell prompt. They are separate. – Tyler Shellberg Nov 26 '19 at 19:31
  • I'm an idiot. Apparently clang-format is already included in Msys2/Mingw. Oof. – Tyler Shellberg Nov 26 '19 at 20:25
  • `pip install clang-format` works on Windows now -- win_amd64 and win32 builds are available now. I tested the 64-bit version on Windows 10. It works. – Ross Bencina May 26 '23 at 11:39
2

It turns out that MSYS2 already comes with clang installed, and clang comes with clang-format. As of my version of clang, I have clang-format 9.0.0 installed. it's possible much earlier versions of clang did not come with it installed.

Tyler Shellberg
  • 225
  • 1
  • 3
  • 12
1

I solved this problem by doing the following:

  1. I opened MSYS2 MINGW64

  2. I ran pacman -S clang and followed the prompts to install it.

  3. I ran where clang-format to learn of its location (in my case it was C:\msys64\usr\bin\clang-format.exe)

  4. I added its folder path (C:\msys64\usr\bin\) to Windows' user and system Path variable.

All shells could then find clang-format and after restarting it, VS Code knew about it too.

nb: it could be that clang maybe overkill just for this command - a better pacman command might be adequate here.

user2565010
  • 111
  • 2
0

The clang package was not available in my MSYS2 installation by default, so I had to install it:

pacman -Sy --needed mingw-w64-ucrt-x86_64-clang

You can see where the binary is installed (to add to your PATH, if needed) with the command:

pacman -Ql mingw-w64-ucrt-x86_64-clang | grep bin/clang-format

The output should looks something like this:

mingw-w64-ucrt-x86_64-clang /ucrt64/bin/clang-format.exe
diogovk
  • 448
  • 1
  • 4
  • 12