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 6.0.1.
2.1. Installing clang
In Clang and LLVM Toolset, clang is provided by the llvm-toolset-6.0-clang package and is automatically installed with the llvm-toolset-6.0 package. See Section 1.4, “Installing Clang and LLVM Toolset”.
2.2. Using the C Compiler
To compile a C program on the command line, run the clang
compiler as follows:
$ scl enable llvm-toolset-6.0 '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 file named a.out
by default.
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:
$ scl enable llvm-toolset-6.0 '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. To link object files together and create a binary file:
$ scl enable llvm-toolset-6.0 'clang -o output_file object_file ...'
Note that you can execute any command using the scl
utility, causing it to be run with the Clang and LLVM Toolset binaries available. This allows you to run a shell session with Clang and LLVM Toolset clang
directly available:
$ scl enable llvm-toolset-6.0 'bash'
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.
Example 2.1. Compiling a C Program on the Command Line
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:
$ scl enable llvm-toolset-6.0 'clang -o hello hello.c'
This creates a new binary file called hello
in the current working directory.
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 on the Command Line”, you can run it by typing the following at a shell prompt:
$ ./hello Hello, World!
2.4. Using the C++ Compiler
To compile a C++ program on the command line, run the clang++
compiler as follows:
$ scl enable llvm-toolset-6.0 '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.
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:
$ scl enable llvm-toolset-6.0 '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. To link object files together and create a binary file:
$ scl enable llvm-toolset-6.0 'clang++ -o output_file object_file ...'
Note that you can execute any command using the scl
utility, causing it to be run with the Clang and LLVM Toolset binaries available. This allows you to run a shell session with Clang and LLVM Toolset clang
directly available:
$ scl enable llvm-toolset-6.0 'bash'
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:
$ scl enable llvm-toolset-6.0 '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:
$ scl enable llvm-toolset-6.0 'clang option... -o object_file source_file'
This creates an object file named object_file
in the current working directory.
Note that you can execute any command using the scl
utility, causing it to be run with the Clang and LLVM Toolset binaries available. This allows you to run a shell session with Red LLVM Developer Toolset:
$ scl enable llvm-toolset-6.0 'bash'
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:$ scl enable llvm-toolset-6.0 'man clang'
Online Documentation
- clang — The clang compiler documentation provides detailed information on
clang
's 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.