Clang: Difference between revisions

From ArchWiki
(add zh-cn)
 
(39 intermediate revisions by 23 users not shown)
Line 1: Line 1:
[[Category:Development]]
[[Category:Package development]]
[[Category:Package development]]
[[de:Clang]]
[[es:Clang]]
[[fi:Clang]]
[[ja:Clang]]
[[ja:Clang]]
[[pt:Clang]]
[[ru:Clang]]
[[zh-hans:Clang]]
[[zh-hans:Clang]]
[http://clang.llvm.org/ Clang] is a C/C++/Objective C compiler based on LLVM. It is distributed under the BSD Licence.
[https://clang.llvm.org/ 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 ==
== Installation ==
Install {{pkg|clang}} from the [[Official repositories]].
 
[[Install]] the {{Pkg|clang}} package.


== Build packages with Clang ==
== Build packages with Clang ==
Add {{ic|1=export CC=clang}} and (for C++) {{ic|1=export CXX=clang++}} to your {{ic|/etc/makepkg.conf}}. If you are building with {{ic|debug}} also remove {{ic|-fvar-tracking-assignments}} from {{ic|DEBUG_CFLAGS}} and {{ic|DEBUG_CXXFLAGS}} as clang does not support it.


NB: 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.
{{Merge|makepkg|compiler flags for use with [[makepkg]]}}
 
=== Generic setup ===
 
To change the default compiler for building packages, [[textedit|edit]]:
 
{{hc|/etc/makepkg.conf|2=
...
export CC=clang
export CXX=clang++
}}
 
To use {{ic|libc++}} as the [[Wikipedia:C++ Standard Library|C++ Standard Library]] instead of [[GCC]]'s {{ic|libstdc++}}: install the {{Pkg|libc++}} package, then add {{ic|1=-stdlib=libc++}} to {{ic|CXXFLAGS}} in your {{ic|/etc/makepkg.conf}}.
 
For LTO support: [[install]] the {{Pkg|lld}} package, then add {{ic|1=-fuse-ld=lld}} to {{ic|LDFLAGS}} in your {{ic|/etc/makepkg.conf}}.
 
If you are building with {{ic|debug}}, you also need to remove {{ic|-fvar-tracking-assignments}} from {{ic|DEBUG_CFLAGS}} and {{ic|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 {{ic|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 {{ic|linux-clang}} based on {{ic|CC}}/{{ic|CXX}} variables.
But in other cases (e.g. packages with direct call of {{ic|qmake}}) it will not, so we can set it explicitly:
 
{{hc|/etc/makepkg.conf|2=
export QMAKESPEC=linux-clang
}}
 
{{Note|Some packages will require {{Pkg|llvm}} to be installed because {{ic|linux-clang}} is configured to use tools like {{ic|llvm-ar}}.}}


== Using the Static Analyzer ==
== Using the Static Analyzer ==
To analyze a project, simply place the word {{ic|scan-build}} in front of your build command. For example:
To analyze a project, simply place the word {{ic|scan-build}} in front of your build command. For example:
  $ scan-build make
  $ scan-build make


{{tip|If your project is already compiled, {{ic|scan-build}} won't rebuild and won't analyse it. To force recompilation and analysis, use {{ic|-B}} switch:
If your project is already compiled, {{ic|scan-build}} will not rebuild and will not analyse it. To force recompilation and analysis, use {{ic|-B}} switch:
{{bc|$ scan-build make -B}}
 
}}
$ scan-build make -B


It is also possible to analyze specific files:
It is also possible to analyze specific files:
  $ scan-build gcc -c t1.c t2.c
  $ scan-build gcc -c t1.c t2.c


== References ==
== Tips and tricks ==
*[http://clang-analyzer.llvm.org/scan-build.html scan-build: running the analyzer from the command line]
 
{{Expansion|Describe the basic usage of [https://clang.llvm.org/docs/ClangFormat.html ClangFormat], [https://clang.llvm.org/extra/clang-tidy/index.html Clang-Tidy], and [https://clang.llvm.org/extra/clang-include-fixer.html Clang-Include-Fixer].}}
 
=== Bash completion ===
 
In order to enable Bash completion, install {{Pkg|bash-completion}} and source {{ic|/usr/share/clang/bash-autocomplete.sh}} in a [[Bash#Configuration files|Bash startup file]].
 
== Troubleshooting ==
 
=== Stack protector ===
 
The {{Pkg|clang}} package enables {{ic|-fstack-protector-strong}} on default. This practice should not cause any problem for compiling most programs and improve the overall security and robustness with a minimal cost. However, there are situations where the stack protector canary is uninitialized in TLS (for example, when you are implementing the {{ic|_start}} function on yourself). In such cases, compiling with {{ic|-fstack-protector-strong}} may lead to segmentation faults or other unexpected errors. One should be aware of the divergence between the {{Pkg|clang}} package and upstream.
 
== See also ==
 
* [[Wikipedia:Clang]]
* [https://clang-analyzer.llvm.org/scan-build.html scan-build: running the analyzer from the command line]
* [https://llvm.org/docs/CompileCudaWithLLVM.html Compiling CUDA with clang]

Latest revision as of 15:23, 9 December 2023

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

This article or section is a candidate for merging with makepkg.

Notes: compiler flags for use with makepkg (Discuss in Talk: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.

Troubleshooting

Stack protector

The clang package enables -fstack-protector-strong on default. This practice should not cause any problem for compiling most programs and improve the overall security and robustness with a minimal cost. However, there are situations where the stack protector canary is uninitialized in TLS (for example, when you are implementing the _start function on yourself). In such cases, compiling with -fstack-protector-strong may lead to segmentation faults or other unexpected errors. One should be aware of the divergence between the clang package and upstream.

See also