Install Clang

In this article, we have explained step by step how to build and install Clang from source code on any system with OS Linux, Ubuntu and Windows.

Table of contents:

  1. Introduction to Clang
  2. Build and Install Clang
  3. Check Clang Installation

Introduction to Clang

Clang is an open-source compiler for C and C++ programs and is a part of LLVM project. It is compatible with GCC and MSVC compiler. Clang compiler can compile the following Programming Languages:

  • C
  • Objective C
  • C++
  • Objective C++

It is optimized for the following systems:

  • X86-32
  • X86-64
  • ARM

It is used to build production softwares like Google Chrome and Firefox.

Clang is often used over other C++ compilers like G++ as Clang support critical additional features like support for low precision data types like INT8 and is highly optimized for specific systems.

Build and Install Clang

The steps to build and install Clang from source code is as follows:

  • Get the LLVM source which has Clang
git clone https://github.com/llvm/llvm-project.git
  • Build the source code using cmake
cd llvm-project
mkdir build
cd build
cmake -DLLVM_ENABLE_PROJECTS=clang -G "Unix Makefiles" ../llvm

If you do not have cmake, you can install it using the following commands:

  • Download CMake binary depending on your system from this official download page

  • Use the following command after extracting the download file:

./bootstrap
make
make install

If your system does not have "make", you can install it using the following command:

sudo apt-get install build-essential

Once done, install Clang compiler using the make command:

sudo make -j 16

16 is the number of threads you want to use for the installation. Higher the number of threads, faster is the installation. You can check the number of threads supported in your system using the command:

lscpu

or

htop

With this, Clang will be installed in your system.

Check Clang Installation

To check if Clang has been installed correctly, use the following commands:

clang --help
clang --version

To try, you can also build a C program and run the executable. The commands will be as follows:

clang test.c
./a.out

With this, your system will be ready with Clang installed and you can use it to build any software. Enjoy.