Chapter 2. Using clang

Clang is a LLVM compiler front end for C-based languages: C, C++, Objective C/C++, OpenCL, and Cuda.

LLVM Toolset is distributed with clang 11.0.1 on RHEL 7 and clang 11.0.0 on RHEL 8.

Note

You can execute any command using the scl utility on Red Hat Enterprise Linux 7, causing it to be run with the LLVM binaries available. To use 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-11.0 'bash'

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-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 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 LLVM Toolset:

  • For Red Hat Enterprise Linux 7:

    $ scl enable llvm-toolset-11.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. 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-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 compiler creates a file named after the source file with the .o file extension.

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

    $ clang -o object_file source_file

This creates an object file named object_file in the current working directory.

2.5. 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.6. 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.