How can I install clang
学习当地常用口语可以快速融入当地文化,如问路:'Excuse me, how can I get to...?' #生活知识# #旅游生活# #旅游语言学习#
This answer is one of the most-up-to-date answers as of May 2025, and is also the only answer that explains how to get the latest version of clang-format direct from LLVM, the people who make it.
To install the latest version of clang-format and git-clang-format (runnable as git clang-format)
Summary
To install clang-format in Ubuntu, you can use sudo apt install clang-format, or some variant of that (see below), but it frequently installs a really old version that is missing a ton of features. So: [Preferred] To install the latest version of clang-format in Ubuntu, you must get it from the official release pages on GitHub directly. I explain how to do this in detail below.[Old method] To install an old version of clang-format:
Do:
sudo apt update
And then try, in this order, one-at-a-time, until one works:
sudo apt install clang-format sudo apt install clang-format-14.0 sudo apt install clang-format-13.0 sudo apt install clang-format-12.0 sudo apt install clang-format-11.0 sudo apt install clang-format-10.0 sudo apt install clang-format-9.0 sudo apt install clang-format-8.0 sudo apt install clang-format-7.0 sudo apt install clang-format-6.0 sudo apt install clang-format-5.0 sudo apt install clang-format-4.0 sudo apt install clang-format-3.6 sudo apt install clang-format-3.4 sudo apt install clang-format-3.0
On Ubuntu 14.04, for instance, the first command above that works is sudo apt install clang-format-3.6. As of Ubuntu 16.04, I believe, sudo apt install clang-format works, but no matter what version of Ubuntu you're on, Ubuntu 20.04 included, sudo apt install clang-format installs a quite-outdated version (ex: version 6.0.0 on Ubuntu 18.04). So, to get the latest version of clang-format, keep reading.
[Preferred method] How to install the latest version of clang-format on Ubuntu
The super quick instructions to get a recent version
To get clang-format and git-clang-format from LLVM (the parent organization which makes the clang C and C++ compiler, as well as those tools) directly, follow my instructions farther below. However, that requires downloading the entire compressed clang compiler toolset release which is ~600 MB, and extracting it into a folder which is ~5 GB when fully extracted, just so you can copy out a couple megabytes of these executables. That takes some time.
So, if you're in a huge hurry, and if you want to trust executables from my personal repo, I keep a recent version (14.0.0 or later) of both clang-format and git-clang-format in the bin dir of my repo here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter/tree/main/bin. It is frequently not recommended to trust other peoples' executables, however, so I provide the exact method and instructions below which I used to extract those executables from the original release from LLVM (the maker of clang-format), which you can follow yourself if you like instead.
If you'd like to quickly download and install clang-format and git-clang-format from my personal repo, do this:
wget https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter/raw/main/bin/clang-format wget https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter/raw/main/bin/git-clang-format chmod +x clang-format git-clang-format mkdir -p ~/bin mv clang-format ~/bin mv git-clang-format ~/bin # log out of Ubuntu and log back in # ensure it worked and you now have a later version clang-format --version # Check the help menus clang-format -h git clang-format -h # OR (same thing as the line just above): git-clang-format -h
The full instructions to get the latest version from LLVM, the makers, directly:
This requires downloading the entire LLVM clang C and C++ compiler toolset, which is a compressed file ~600 MB in size, and then extracting it into a folder which is ~5 GB in size when extracted, just so you can copy out a few magabytes of executables from within it.
These instructions were originally posted in the README of my eRCaGuy_CodeFormatter repo here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter#installation-instructions
The full steps to get the latest clang-format and git-clang-format from the latest official LLVM release:
Go to the LLVM official clang releases page: https://github.com/llvm/llvm-project/releases Find the latest binary release for your operating system. Example: for 64-bit Linux, it is currently (as of Mar. 2022): clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz. The download link is: https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz Use the link you found above for the next commands:url="https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz" # download it; be patient: the file is ~600 MB wget "$url" # extract it; be patient: this could take several minutes, as the file is # about 5 GB when extracted! On my high-speed computer with SSD, it took # ~1 minute time tar xf clang+llvm*.tar.xz # cd into the bin dir cd clang+llvm*/bin # make a ~/bin dir if it doesn't exist yet mkdir -p ~/bin # copy out `clang-format` into your ~/bin dir cp clang-format ~/bin # copy out `git-clang-format` into your ~/bin dir cp git-clang-format ~/bin # Manually edit your ~/.profile file to ensure it contains the following in # order to ensure ~/bin is part of your executable PATH variable. This is # part of your default Ubuntu ~/.profile file (for which you have a backup # copy in /etc/skel/.profile): # # # set PATH so it includes user's private bin if it exists # if [ -d "$HOME/bin" ] ; then # PATH="$HOME/bin:$PATH" # fi # Now, if this is your first time creating and using the ~/bin dir, log out # of Ubuntu and log back in. # check your clang-format version to ensure it shows the version you just # installed clang-format --version # Ensure it is found in your ~/bin dir; my output to this command is: # `/home/gabriel/bin/clang-format` which clang-format # Check `git-clang-format` too which git-clang-format # Check the help menus clang-format -h git clang-format -h # OR (same thing as the line just above): git-clang-format -h # manually delete the the extracted folder if desired, and the # downloaded *.tar.xz file as well, if desired
Note that git has a pretty neat feature that causes any executable in your path which begins with git- to automatically be treated as a git command. Whenever you run git some_command, git automatically searches all of your system's PATH variable for an executable named git-some_command. If it exists, git runs it. So, simply by virtue of the fact that you put an executable in your path named git-clang-format, git allows you to run it as git clang-format (withOUT the dash after git). Optionally, of course, you can also just run that same executable as git-clang-format, since that's what its filename is.
See below for recommended git clang-format usage and workflow.
Additional setup & usage info & resources:
If you followed the steps just above, you now have the latest version fo both clang-format and git-clang-format. Having the latter allows you to run git clang-format as a git command to auto-format your files before you commit them in git. git-clang-format is a Python script written by LLVM, the makers of the clang C and C++ compiler and clang-format. It is located in the official LLVM GitHub repository here: https://github.com/llvm/llvm-project/blob/main/clang/tools/clang-format/git-clang-format. Place it into your PATH; ex: in a file called ~/bin/git-clang-format, and mark this file as executable (chmod +x ~/bin/git-clang-format). The recommended git workflow to call and use this git-clang-format auto-formatter would then be:# See what git changes you have git difftool # OR: `git diff` if you haven't configured a difftool such as meld git diff # Add (stage) a C or C++ file to be committed git add my_changed_file.c # Run `git-clang-format` to have `clang-format` check and # auto-format **just your changed lines**. (This runs the # `~/bin/git-clang-format` Python script). git clang-format # See what changes `clang-format` just made to your changed lines git difftool # OR git diff # Add (stage) this file again since it's been changed by # `git-clang-format` git add my_changed_file.c # commit the changed file git commit Other, potentially-useful info.: git-clang-format python script setup instructions: https://dx13.co.uk/articles/2015/4/3/Setting-up-git-clang-format.html git clang-format usage and workflow instructions: https://electronjs.org/docs/development/clang-format Update Apr. 2020: I just got clang-format up and running fully on a project on GitHub. I run it with ./run_clang-format.sh; here's how: I wrote some instructions here: https://github.com/AmboVent-1690-108/AmboVent#setup Here's the PR where I added everything: https://github.com/AmboVent-1690-108/AmboVent/pull/39. You can take a look to see how I did it all. I borrowed from my notes and files I have in my dotfiles project here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/tree/master/clang-format
Related
See also my clang-format-based project here: eRCaGuy_CodeFormatterSee also
My answer: How to call clang-format over a cpp project folder?网址:How can I install clang https://klqsh.com/news/view/122363
相关内容
Installing Clang on MacHow run clang from command line on Windows?
Ways to install Windows 11
How can I access the /android
How to Get Help with File Explorer in Windows 11/10
Sign in to Microsoft 365
How To Be Successful
How to Study for (and Take!) Open Book Exams
How to Set Up Multiple Monitors Windows 11: A Step
Easiest way to set up VPN access to home network?