此内容没有您所选择的语言版本。

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'
Copy to Clipboard Toggle word wrap

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'
Copy to Clipboard Toggle word wrap

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 ...'
Copy to Clipboard Toggle word wrap

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'
Copy to Clipboard Toggle word wrap
Important

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;
}
Copy to Clipboard Toggle word wrap

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'
Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap

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!
Copy to Clipboard Toggle word wrap

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 ...'
Copy to Clipboard Toggle word wrap

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'
Copy to Clipboard Toggle word wrap

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 ...'
Copy to Clipboard Toggle word wrap

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'
Copy to Clipboard Toggle word wrap
Important

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;
}
Copy to Clipboard Toggle word wrap

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'
Copy to Clipboard Toggle word wrap

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
Copy to Clipboard Toggle word wrap

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!
Copy to Clipboard Toggle word wrap

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'
Copy to Clipboard Toggle word wrap

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'
Copy to Clipboard Toggle word wrap

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 as clang. To display the manual page for the version included in Clang and LLVM Toolset:

    $ scl enable llvm-toolset-6.0 'man clang'
    Copy to Clipboard Toggle word wrap

Online Documentation

  • clang — The clang compiler documentation provides detailed information on clang's usage.

See Also

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat