Clang/LLVM

Clang

The Clang tool is a front end compiler that is used to compile programming languages such as C++, C, Objective C++ and Objective C into machine code.

Use Clang with Incredibuild

What is Clang?

The Clang tool is a front end compiler that is used to compile programming languages such as C++, C, Objective C++ and Objective C into machine code.

Clang is also used as a compiler for frameworks like OpenMP, OpenCL, RenderScript, CUDA and HIP. Clang uses the LLVM compiler as its back end and it has been included in the release of the LLVM since the LLVM 2.6.

Clang is also built to be a drop-in replacement for GCC command. In its design, the Clang compiler has been constructed to work very similarly to GCC to ensure that portability is maximized. However, the difference between the two is that while GCC defaults to gnu89, Clang defaults to gnu99.

Try it now
what is clang

How does Clang work? How to use Clang?

The Clang Compiler has been designed to work just like any other Compiler. Clang works in three different stages.

The first stage is the front end that is used for parsing source code. It checks the code for errors and builds a language-specific Abstract Syntax Tree (AST) to work as its input code.

The second stage is the optimizer that is used for optimizing the AST that was generated by the frontend.

The third and final stage is the back end. This is responsible for generating the final code to be executed by the machine which can depend on the target.

The History of Clang + Latest version

The original contributor of Clang is Apple. Apple developers originally made use of GCC as a front-end for LLVM but it caused certain problems for developers that were working with LLVM. This was mostly because the GCC code was large and it doesn’t integrate into Apple’s IDE (Integrated development environment).

In addition to this, the LLVM has a license which was not compatible with GCC’s license. During this time, Apple software made heavy use of Objective-C, which has a low priority for GCC developers as front-end. This was why Apple developed the Clang Project and made it open source in July 2007.

The latest version of Clang is the Clang 12. As of November 2020, it has been built with full support for all published C++ standards and partially supports the upcoming C++20 standards.

Try it now

Get Free License

How to download / Install Clang?

To download and install Clang, you need to apply the following instructions.

  • Open a terminal window. Enter the command (clang — version) to confirm if the Clang Compilers had already been installed.
  • To update or install the Clang Compilers, you need to type in the following command: command xcode-select–install

This will generate a pop-up window asking you if you want to install it now or later. Once you click on install, this will direct you to a License Agreement. Select “Agree” and you will see the installation in progress. Once the installation is completed, click on “Done.”

In case you still want to double-check the installation, type in the command: clang –version into the Terminal.

Clang examples

The first version of clang was Clang 1.0 and it was released with LLVM 2.6. The next version was Clang C++ which was able to parse GCC 4.2 libstdc, compile itself and function as a working code generator for non-trivial operations.

From Clang 3.0 down to the latest version – Clang 12.0.0, the compiler has evolved with a ton of meaningful features.

It has become more scalable, extremely light and very fast. It is designed to keep more data in the course of the compiling process when compared to GCC (learn about what is GCC). Also, it preserves the form of the main code.

The reason for this is that it will be easy to map errors back into the main source. Clang provides error reports that are specific and detailed and it is also machine-readable. This way IDEs get to index the compiler’s output while compiling.

Clang features / Clang attributes

According to performance comparisons carried out by Alibabatech, the results show that Clang’s front-end is significantly faster than GCC in the compilation and it uses less memory. This gives Clang an edge especially for making large projects.

Clang compiler generates diagnostics (error and warning messages) that are more useful and understandable.

Also, Clang uses a Library based Architecture as the front-end is separated into distinct libraries that can suit various needs. This makes it easy to time and estimates the cost of every stack layer.

Clang’s library-based process makes it easy for fresh developers to join because all they need is to get the basics and not the entire picture.

Clang Supports Diverse Clients as the primary goal is to aid incremental compilation. This gives it a better connection to the GUI of IDE. Great care was taken to build and design clang to give it a firm foundation for a new set of C++, Objective C and C development tools.

Clang supports a wide range of language extensions as they are compatible with Microsoft, GCC, and other well-known compilers to enhance functionality via specific features of clang.

Clang Compilers are also able to integrate with IDEs which gives the user more visibility over the entire project. Clang uses the BSD license of the LLVM for commercial projects as it was designed not to be used as a separate compiler alone.

It was designed as a library embedded within a third-party app. The BSD license encourages several contributors to go through and improve the source code which enhances it at the end of the day.

To learn more about Clang features, visit this GCC vs Clang blog post.

Clang has some advantages when used for the development of performance-critical software like Firefox or Chrome.

These Include:

clang advantages

Clang advantages

  • Better diagnostics (more colored, more expressive, suggestions on how errors can be fixed, typdef preservation, quote source which includes pointing out the correct place, etc.)
  • The compilation is faster (between 5-20%). This is further improved by support for precompiled headers (PCH). This is supported by the data gotten from SPEC compilation shown in the picture above.
  • Clang is easier to read (when looking for compiler bugs) and reuse (the libcland feature allows other tools to be written and the compiler extended) as it is a newer modular codebase that is written in C++.

Clang for Windows

One of the recommended IDEs for using Clang on Windows is Visual Studio as it comes with full support for targeting Clang/LLVM based toolsets. To install the Clang tools on Windows is easy. There is a Clang compiler for Windows which is an optional component built with the ‘Desktop development with C++’ workload. This will handle the complete development of Clang on Windows. To get the best support in IDE, it is best to use the latest version of Clang.

Clang for Linux

For you to use Clang/LLVM on a remote Linux machine with Visual Studio, what you need to do is to install alongside the package manager of the distributor. Once the “which clang” feature can detect the compiler, then you are good to go.

In addition, Clang is offered in every Linux/GNU distribution or BSD as it is part of its packaging systems. Clang is the default compiler for macOS X starting from Xcode 4.2.

It is highly recommended that developers that want the quickest incremental builds should use the Ninja build system.

The generated Visual Studio project files can be used to edit the source code of Clang which will generate a second build directory alongside for running the tests

Clang for Mac

Clang can be installed on macOS by using Visual studio code or with Homebrew.

  • The first step is to install Visual Studio Code on macOS.
  • After this, you need to install the C++ extension for VS code. Next, ensure that Clang is installed on your macOS. To do this, go to a macOS Terminal and type this command: clang –version
  • If Clang is not installed on your macOS, then enter this command to install; xcode-select –install.
  • You can then create a new folder from which you will be running projects.

Installing with Homebrew

  • To begin, run the command: $ brew install llvm
  • After installing Clang using brew, the new binaries won’t be in the path automatically. Keep in mind the formula says: OS X already provides this software and installing another version in parallel can cause all kinds of trouble.
  • The binaries are here: $(brew –prefix llvm)/bin
  • Run this command to show the Homebrew llvm/clang in your PATH:
    $ echo ‘export PATH=”/usr/local/opt/llvm/bin:$PATH”‘ >> ~/.bash_profil

Clang and Incredibuild:

Incredibuild can highly accelerate Chang/LLVM compilations executed by the Visual Studio IDE or any of the long lists of build systems Incredibuild accelerates such as CMake, ninja and others. By using its uniquely distributed computing technology, Incredibuild seamlessly distributes clang compilation processes across hundreds of remote cores in the network resulting in drastic acceleration of clang based compilation time. This has also provided more support for the distribution of Clang++ through Unreal Engine.

icon

Bottom line

Clang is not just like any other compiler, it comes with an Infrastructure that allows it to build tools and it can also extend its behaviors easily. The LLVM/Clang source code includes many tools while others are available on the web. Clang can be used as a very good C/C++ parser for building tools.

Get Free License

More Compilers

  • CUDA

  • GCC

  • MS (VS) C++

  • g++ / gnu

  • tcc

  • Intel c++ (floating license)