Chapter 3. Using clang++

3.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-11.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.

3.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-11.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.

3.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-11.0 'clang++ -o output_file object_file ...'
  • For Red Hat Enterprise Linux 8:

    $ clang++ -o output_file object_file ...
Important

Certain more recent library features are statically linked into applications built with 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 3.1. 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 LLVM:

  • For Red Hat Enterprise Linux 7:

    $ scl enable llvm-toolset-11.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.

3.4. 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 3.2. Running a C++ program on the command line

Assuming that you have successfully compiled the hello binary file as shown in Example 3.1, “Compiling a C++ Program on the Command Line”, you can run it by typing the following at a shell prompt:

$ ./hello
Hello, World!

3.5. Additional Resources

A detailed description of the clang compiler and its features is beyond the scope of this document. 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 as clang. To display the manual page for the version included in LLVM Toolset:

    • For Red Hat Enterprise Linux 7:

      $ scl enable llvm-toolset-11.0 'man clang'
    • For Red Hat Enterprise Linux 8:

      $ man clang

Online documentation

  • clang — The clang compiler documentation provides detailed information about use of clang.

See Also

  • Chapter 1, LLVM — An overview of LLVM and more information on how to install it on your system.