Clang - ArchWiki

Jump to content
From ArchWiki

Clang is a C/C++/Objective C/CUDA compiler based on LLVM. The most recent iteration is distributed under the "Apache 2.0 License with LLVM exceptions".

## Installation

Install the clang package.

## Build packages with Clang

### Generic setup

To change the default compiler for building packages, edit:

    /etc/makepkg.conf

    ...
    export CC=clang
    export CXX=clang++

To use `libc++` as the C++ Standard Library instead of GCC's `libstdc++`: install the libc++ package, then add `-stdlib=libc++` to `CXXFLAGS` in your `/etc/makepkg.conf`.

For LTO support: install the lld package, then add `-fuse-ld=lld` to `LDFLAGS` in your `/etc/makepkg.conf`.

If you are building with `debug`, you also need to remove `-fvar-tracking-assignments` from `DEBUG_CFLAGS` and `DEBUG_CXXFLAGS`, as Clang does not support it.

Note: For packages that specify GCC-specific build options, there may be build errors that require either editing the source package, the PKGBUILD or commenting out the Clang lines in `makepkg.conf`.

### Qt packages

Qt packages may require extra setup. Qt has predefined build configurations called "mkspecs", defaulting to GCC for Linux.

In some cases, mkspec will be automatically set to `linux-clang` based on `CC`/`CXX` variables. But in other cases (e.g. packages with direct call of `qmake`) it will not, so we can set it explicitly:

    /etc/makepkg.conf

    export QMAKESPEC=linux-clang

Note: Some packages will require llvm to be installed because `linux-clang` is configured to use tools like `llvm-ar`.

## Using the Static Analyzer

To analyze a project, simply place the word `scan-build` in front of your build command. For example:

    $ scan-build make

If your project is already compiled, `scan-build` will not rebuild and will not analyse it. To force recompilation and analysis, use `-B` switch:

    $ scan-build make -B

It is also possible to analyze specific files:

    $ scan-build gcc -c t1.c t2.c

## Tips and tricks

This article or section needs expansion.

Reason: Describe the basic usage of ClangFormat, Clang-Tidy, and Clang-Include-Fixer. (Discuss in Talk:Clang)

### Bash completion

In order to enable Bash completion, install bash-completion and source `/usr/share/clang/bash-autocomplete.sh` in a Bash startup file.

## See also

- Wikipedia:Clang
- scan-build: running the analyzer from the command line
- Compiling CUDA with clang

Categories:

- Development
- Package development

Hidden category:

- Pages or sections flagged with Template:Expansion