Chapter 2. clang
clang is a LLVM compiler front end for C-based languages: C, C++, Objective C/C++, OpenCL, and Cuda.
Clang and LLVM Toolset is distributed with clang 7.0.1.
2.1. Installing clang
In Clang and LLVM Toolset on Red Hat Enterprise Linux 7, clang is provided by the llvm-toolset-7.0-clang package and is automatically installed with the llvm-toolset-7.0 package. On Red Hat Enterprise Linux 8, clang is provided by the llvm-toolset module. See Section 1.4, “Installing Clang and LLVM Toolset”.
2.2. Using clang
You can execute any command using the scl
utility on Red Hat Enterprise Linux 7, causing it to be run with the Clang and LLVM Toolset binaries available. To use Clang and LLVM Toolset on Red Hat Enterprise Linux 7 without a need to use scl enable
with every command, run a shell session with:
$ scl enable llvm-toolset-7.0 'bash'
2.2.1. Compiling a C Source File to a Binary File
To compile a C program to a binary file:
For Red Hat Enterprise Linux 7:
$ scl enable llvm-toolset-7.0 'clang -o output_file source_file'
For Red Hat Enterprise Linux 8:
$ clang -o output_file source_file
This creates a binary file named output_file
in the current working directory. If the -o
option is omitted, the compiler creates a binary file named a.out
by default.
Example 2.1. Compiling a C Program with clang
Consider a source file named hello.c
with the following contents:
#include <stdio.h> int main(int argc, char *argv[]) { printf("Hello, World!\n"); return 0; }
Compile this source code on the command line by using the clang
compiler from Clang and LLVM Toolset:
For Red Hat Enterprise Linux 7:
$ scl enable llvm-toolset-7.0 'clang -o hello hello.c'
For Red Hat Enterprise Linux 8:
$ clang -o hello hello.c
This creates a new binary file called hello
in the current working directory.
2.2.2. Compiling a C Source File to an Object File
When you are working on a project that consists of several source files, it is common to compile an object file for each of the source files first and then link these object files together. This way, when you change a single source file, you can recompile only this file without having to compile the entire project.
To compile a C source file to an object file:
For Red Hat Enterprise Linux 7:
$ scl enable llvm-toolset-7.0 'clang -o object_file -c source_file'
For Red Hat Enterprise Linux 8:
$ clang -o object_file -c source_file
This creates an object file named object_file
. If the -o
option is omitted, the compiler creates a file named after the source file with the .o
file extension.
2.2.3. Linking C Object files to a Binary File
To link object files together and create a binary file:
For Red Hat Enterprise Linux 7:
$ scl enable llvm-toolset-7.0 'clang -o output_file object_file ...'
For Red Hat Enterprise Linux 8:
$ clang -o output_file object_file ...
Certain more recent library features are statically linked into applications built with Clang and LLVM Toolset to support execution on multiple versions of Red Hat Enterprise Linux. This creates an additional minor security risk as standard Red Hat Enterprise Linux errata do not change this code. If the need arises for developers to rebuild their applications due to this risk, Red Hat will communicate this using a security erratum.
Because of this additional security risk, developers are strongly advised not to statically link their entire application for the same reasons.
2.3. Running a C Program
When clang
compiles a program, it creates an executable binary file. To run this program on the command line, change to the directory with the executable file and run the program:
$ ./file_name
Example 2.2. Running a C Program on the Command Line
Assuming that you have successfully compiled the hello
binary file as shown in Example 2.1, “Compiling a C Program with clang”, you can run it by typing the following command:
$ ./hello Hello, World!
2.4. Using clang++
You can execute any command using the scl
utility on Red Hat Enterprise Linux 7, causing it to be run with the Clang and LLVM Toolset binaries available. To use Clang and LLVM Toolset on Red Hat Enterprise Linux 7 without a need to use scl enable
with every command, run a shell session with:
$ scl enable llvm-toolset-7.0 'bash'
2.4.1. Compiling a C++ Source File to a Binary File
To compile a C++ program on the command line, run the clang++
compiler as follows:
For Red Hat Enterprise Linux 7:
$ scl enable llvm-toolset-7.0 'clang++ -o output_file source_file ...'
For Red Hat Enterprise Linux 8:
$ clang++ -o output_file source_file ...
This creates a binary file named output_file
in the current working directory. If the -o
option is omitted, the clang++
compiler creates a file named a.out
by default.
2.4.2. Compiling a C++ Source File to an Object File
When you are working on a project that consists of several source files, it is common to compile an object file for each of the source files first and then link these object files together. This way, when you change a single source file, you can recompile only this file without having to compile the entire project.
To compile an object file on the command line:
For Red Hat Enterprise Linux 7:
$ scl enable llvm-toolset-7.0 'clang++ -o object_file -c source_file'
For Red Hat Enterprise Linux 8:
$ clang++ -o object_file -c source_file
This creates an object file named object_file
. If the -o
option is omitted, the clang++
compiler creates a file named after the source file with the .o
file extension.
2.4.3. Linking C++ Object Files to a Binary File
To link object files together and create a binary file:
For Red Hat Enterprise Linux 7:
$ scl enable llvm-toolset-7.0 'clang++ -o output_file object_file ...'
For Red Hat Enterprise Linux 8:
$ clang++ -o output_file object_file ...
Certain more recent library features are statically linked into applications built with Clang and LLVM Toolset to support execution on multiple versions of Red Hat Enterprise Linux. This creates an additional minor security risk as standard Red Hat Enterprise Linux errata do not change this code. If the need arises for developers to rebuild their applications due to this risk, Red Hat will communicate this using a security erratum.
Because of this accitional security risk, developers are strongly advised not to statically link their entire application for the same reasons.
Example 2.3. Compiling a C++ Program on the Command Line
Consider a source file named hello.cpp
with the following contents:
#include <iostream> using namespace std; int main(int argc, char *argv[]) { cout << "Hello, World!" << endl; return 0; }
Compile this source code on the command line by using the clang++
compiler from Clang and LLVM Toolset:
For Red Hat Enterprise Linux 7:
$ scl enable llvm-toolset-7.0 'clang++ -o hello hello.cpp'
For Red Hat Enterprise Linux 8:
$ clang++ -o hello hello.cpp
This creates a new binary file called hello
in the current working directory.
2.5. Running a C++ Program
When clang++
compiles a program, it creates an executable binary file. Change to the directory with the executable file and run this program:
./file_name
Example 2.4. Running a C++ Program on the Command Line
Assuming that you have successfully compiled the hello
binary file as shown in Example 2.3, “Compiling a C++ Program on the Command Line”, you can run it by typing the following at a shell prompt:
$ ./hello Hello, World!
2.6. Using the clang Integrated Assembler
To produce an object file from an assembly language program, run the clang
tool as follows:
For Red Hat Enterprise Linux 7:
$ scl enable llvm-toolset-7.0 'clang option... -o object_file source_file'
For Red Hat Enterprise Linux 8:
$ clang option... -o object_file source_file
This creates an object file named object_file
in the current working directory.
2.7. Additional Resources
A detailed description of the clang compiler and its features is beyond the scope of this book. For more information, see the resources listed below.
Installed Documentation
clang(1) — The manual page for the
clang
compiler provides detailed information on its usage; with few exceptions,clang++
accepts the same command line options asclang
. To display the manual page for the version included in Clang and LLVM Toolset:For Red Hat Enterprise Linux 7:
$ scl enable llvm-toolset-7.0 'man clang'
For Red Hat Enterprise Linux 8:
$ man clang
Online Documentation
-
clang — The clang compiler documentation provides detailed information on
clang
usage.
See Also
- Chapter 1, Clang and LLVM Toolset — An overview of Clang and LLVM Toolset and more information on how to install it on your system.