使用 LLVM 15.0.7 Toolset


Red Hat Developer Tools 1

安装和使用 LLVM 15.0.7 Toolset

Jacob Valdez

Red Hat Developer Group Documentation Team

摘要

LLVM 工具集是用于 Red Hat Enterprise Linux (RHEL)操作系统的开发人员的红帽产品。使用本指南概述 LLVM Toolset,了解如何调用和使用不同的 LLVM 工具版本,并通过更深入的信息查找资源。

使开源包含更多

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。我们从这四个术语开始:master、slave、黑名单和白名单。由于此项工作十分艰巨,这些更改将在即将推出的几个发行版本中逐步实施。有关更多详情,请参阅我们的首席技术官 Chris Wright 提供的消息

第 1 章 LLVM Toolset

LLVM Toolset 是 Red Hat Enterprise Linux (RHEL)上开发人员的产品。它提供了 LLVM 编译器基础架构框架、Clang 编译器用于 C 和 C++ 语言、LLDB 调试器以及用于代码分析的相关工具。

LLVM Toolset 作为 Red Hat Enterprise Linux 7 的 Red Hat Developer Tools 的一部分发布。对于 Red Hat Enterprise Linux 8,LLVM Toolset 作为一个模块提供。LLVM Toolset 作为 Red Hat Enterprise Linux 9 的软件包提供。

1.1. LLVM Toolset 组件

以下组件作为 LLVM Toolset 的一部分提供:

Expand
Name版本Description

clang

15.0.7

C 和 C++ 的 LLVM 编译器前端。

lldb

15.0.7

使用 LLVM 的部分 C 和 C++ 调试器。

compiler-rt

15.0.7

LLVM 和 Clang 的运行时库。

llvm

15.0.7

一组模块化和可重复使用的编译器和工具链技术。

libomp

15.0.7

用于并行编程的 Open MP API 规格的库。

lld

15.0.7

LLVM 链接器。

python-lit

15.0.7

用于 LLVM 和 Clang 测试套件的软件测试工具。

注意

CMake 构建管理器不是 LLVM Toolset 的一部分。在 Red Hat Enterprise Linux 7 中,CMake 作为单独的软件包提供。在 Red Hat Enterprise Linux 8 中,CMake 在系统存储库中可用。在 Red Hat Enterprise Linux 9 中,CMake 包括在系统存储库中。有关如何安装 CMake 的更多信息,请参阅在 Red Hat Enterprise Linux 上安装 CMake

1.2. LLVM Toolset 兼容性

在以下构架中,Red Hat Enterprise Linux 7 和 Red Hat Enterprise Linux 8 和 Red Hat Enterprise Linux 9 提供了 LLVM Toolset:

  • AMD 和 Intel 64 位
  • 64 位 ARM (RHEL 8 和 RHEL 9)
  • IBM Power Systems, Little Endian
  • IBM Power Systems,Mintle Endian (仅限 RHEL 7)
  • 64-bit IBM Z

1.3. 在 Red Hat Enterprise Linux 7 上访问 LLVM Toolset

为了能够在 Red Hat Enterprise Linux 7 上安装 LLVM Toolset,您必须访问并启用 Red Hat Developer Tools 和 Red Hat Software Collections 软件仓库。
如果这些软件仓库已附加到您的系统,请参阅安装 LLVM Toolset

流程

  1. 运行以下命令安装 Wget

    # yum install wget
    Copy to Clipboard Toggle word wrap
  2. 运行以下命令下载最新的订阅数据:

    # subscription-manager refresh
    Copy to Clipboard Toggle word wrap
  3. 运行以下命令注册您的系统:

    # subscription-manager register
    Copy to Clipboard Toggle word wrap

    要使用图形用户界面(GUI) 注册您的系统,请按照注册和取消注册系统 指南进行操作。

  4. 显示所有可用订阅列表,并运行以下命令来识别池 ID:

    # subscription-manager list --available
    Copy to Clipboard Toggle word wrap
  5. 在以池 ID 开头的行中找到 池 ID
  6. 运行以下命令,将提供对 Red Hat Developer Tools 存储库的访问权限的订阅附加:

    # subscription-manager attach --pool=<pool ID from the subscription>
    Copy to Clipboard Toggle word wrap
    • 将 subscription &gt; 中的 & lt; pool ID 替换为您在上一步中标识的池 ID。
  7. 运行以下命令验证哪些订阅是否已附加到您的系统:

    # sudo subscription-manager list --consumed
    Copy to Clipboard Toggle word wrap
  8. 运行以下命令启用 rhel-7-变体-devtools-rpms 存储库:

    # subscription-manager repos --enable rhel-7-<variant>-devtools-rpms
    Copy to Clipboard Toggle word wrap
    • <variant > 替换为您的 Red Hat Enterprise Linux 系统变体: serverworkstation

      使用 server 访问广泛的开发工具。

  9. 运行以下命令启用 rhel-variant-RHSCL-7-rpms 存储库:

    # subscription-manager repos --enable rhel-<variant>-rhscl-7-rpms
    Copy to Clipboard Toggle word wrap
    • <variant > 替换为您的 Red Hat Enterprise Linux 系统变体: serverworkstation
  10. 运行以下命令,在您的系统中添加 Red Hat Developer Tools GPG 密钥:

    # cd /etc/pki/rpm-gpg
    # wget -O RPM-GPG-KEY-redhat-devel https://www.redhat.com/security/data/a5787476.txt
    # rpm --import RPM-GPG-KEY-redhat-devel
    Copy to Clipboard Toggle word wrap

1.4. 安装 LLVM Toolset

完成以下步骤,安装 LLVM Toolset,包括所有开发和调试工具以及依赖软件包。

先决条件

流程

在 Red Hat Enterprise Linux 7 中,运行以下命令安装 llvm-toolset-15.0 集合:

# yum install llvm-toolset-15.0
Copy to Clipboard Toggle word wrap
重要

这不会在 Red Hat Enterprise Linux 7 上安装 LLDB 调试器。要安装 LLDB 调试器,请运行:

# yum install llvm-toolset-15.0-lldb
Copy to Clipboard Toggle word wrap

在 Red Hat Enterprise Linux 8 中,运行以下命令安装 llvm-toolset 模块:

# yum module install llvm-toolset
Copy to Clipboard Toggle word wrap
重要

这不会在 Red Hat Enterprise Linux 8 上安装 LLDB 调试器或 python3-lit 软件包。要安装 LLDB 调试器和 python3-lit 软件包,请运行:

# yum install lldb python3-lit
Copy to Clipboard Toggle word wrap

在 Red Hat Enterprise Linux 9 中,运行以下命令安装 llvm-toolset 软件包:

# dnf install llvm-toolset
Copy to Clipboard Toggle word wrap
重要

这不会在 Red Hat Enterprise Linux 9 上安装 LLDB 调试器或 python3-lit 软件包。要安装 LLDB 调试器和 python3-lit 软件包,请运行:

# dnf install lldb python3-lit
Copy to Clipboard Toggle word wrap

1.5. 安装 CMake 构建管理器

CMake 构建管理器是一种工具,它独立于您的编译器管理源代码的构建过程。Cmake 可以生成原生构建环境,以编译源代码、创建库、生成打包程序和构建可执行文件。
完成以下步骤以安装 CMake 构建管理器。

先决条件

流程

要安装 CMake,请运行以下命令:

  • 在 Red Hat Enterprise Linux 7 上:
# yum install llvm-toolset-15.0-cmake
Copy to Clipboard Toggle word wrap
  • 在 Red Hat Enterprise Linux 8 中:
# yum install cmake
Copy to Clipboard Toggle word wrap
  • 在 Red Hat Enterprise Linux 9 中:
# dnf install cmake
Copy to Clipboard Toggle word wrap

其他资源

  • 有关 CMake 构建管理器的更多信息,请参阅官方 CMake 文档概述 关于 CMake
  • 有关使用 CMake 构建管理器简介,请参阅:

1.6. 安装 LLVM Toolset 文档

您可以在本地系统中安装 LLVM Toolset 的文档。

先决条件

流程

要安装 llvm-doc 软件包,请运行以下命令:

  • 在 Red Hat Enterprise Linux 7 上:

    # yum install llvm-toolset-15.0-llvm-doc
    Copy to Clipboard Toggle word wrap

    您可以在以下路径中找到文档: /opt/rh/llvm-toolset-15.0/root/usr/share/doc/llvm-toolset-15.0-llvm-15.0.7/html/index.html

  • 在 Red Hat Enterprise Linux 8 中:

    # yum install llvm-doc
    Copy to Clipboard Toggle word wrap

    您可以在以下路径中找到文档: /usr/share/doc/llvm/html/index.html

  • 在 Red Hat Enterprise Linux 9 中:

    # dnf install llvm-doc
    Copy to Clipboard Toggle word wrap

    您可以在以下路径中找到文档: /usr/share/doc/llvm/html/index.html

1.7. 安装 CMake 文档

您可以在本地系统中安装 CMake 构建管理器的文档。

先决条件

流程

要安装 cmake-doc 软件包,请运行以下命令:

  • 在 Red Hat Enterprise Linux 7 上:

    # yum install llvm-toolset-15.0-cmake-doc
    Copy to Clipboard Toggle word wrap

    您可以在以下路径中找到文档:Opt /rh/llvm-toolset-15.0/root/usr/share/doc/llvm-toolset-15.0-cmake-3.18.2/html/index.html

  • 在 Red Hat Enterprise Linux 8 中:

    # yum install cmake-doc
    Copy to Clipboard Toggle word wrap

    您可以在以下路径中找到文档: /usr/share/doc/cmake/html/index.html

  • 在 Red Hat Enterprise Linux 9 中:

    # dnf install cmake-doc
    Copy to Clipboard Toggle word wrap

    您可以在以下路径中找到文档: /usr/share/doc/cmake/html/index.html

第 2 章 Clang 编译器

clang 是基于 C、C++、目标 C/C++、OpenCL 和 Cuda 的 LLVM 编译器前端。

LLVM 工具集随 Clang 15.0.7 一起分发。

2.1. 先决条件

2.2. 编译源文件

使用 Clang 编译器编译源文件以及编译语言源文件。clang 创建一个可执行文件,从而进行编译。为了调试代码,请通过在 Clang 命令中添加 -g 标志来启用调试信息。

注意

要编译 C++ 程序,请使用 clang++ 而不是 clang

流程

要编译您的程序,请运行以下命令:

  • 在 Red Hat Enterprise Linux 7 上:

    $ scl enable llvm-toolset-15.0 'clang -o -g <binary_file> <source_file>'
    Copy to Clipboard Toggle word wrap
    • <binary_file > 替换为输出文件所需的名称,<source_file > 替换为您的源文件的名称。
  • 在 Red Hat Enterprise Linux 8 中:

    $ clang -o -g <binary_file> <source_file>
    Copy to Clipboard Toggle word wrap
    • <binary_file > 替换为输出文件所需的名称,<source_file > 替换为您的源文件的名称。
  • 在 Red Hat Enterprise Linux 9 中:

    $ clang -o -g <binary_file> <source_file>
    Copy to Clipboard Toggle word wrap
    • <binary_file > 替换为输出文件所需的名称,<source_file > 替换为您的源文件的名称。

2.3. 运行安装程序

Clang 编译器创建可执行二进制文件,从而进行编译。完成以下步骤以执行此文件并运行安装程序。

先决条件

  • 您的程序已编译。
    有关如何编译程序的更多信息,请参阅 编译源文件

流程

要运行程序,请在包含可执行文件的目录中运行:

$ ./<binary_file>
Copy to Clipboard Toggle word wrap
  • <binary_file > 替换为可执行文件的名称。

2.4. 将目标文件链接在一起

通过将目标文件链接在一起,您只能编译包含更改的源文件,而不是整个项目。
当您处理由多个源文件组成的项目时,请使用 Clang 编译器为每个源文件编译对象文件。在下一步中,将这些目标文件链接在一起。clang 会自动生成一个包含链接目标文件的可执行文件。编译后,将目标文件再次链接在一起。

注意

要编译 C++ 程序,请使用 clang++ 而不是 clang

流程

  1. 要将源文件编译到目标文件中,请运行以下命令:

    • 在 Red Hat Enterprise Linux 7 上:

      $ scl enable llvm-toolset-15.0 'clang -o <object_file> -c <source_file>'
      Copy to Clipboard Toggle word wrap
      • <object_file > 替换为对象文件所需的名称,<source_file > 替换为您的源文件的名称。
    • 在 Red Hat Enterprise Linux 8 中:

      $ clang -o <object_file> -c <source_file>
      Copy to Clipboard Toggle word wrap
      • <object_file > 替换为对象文件所需的名称,<source_file > 替换为您的源文件的名称。
    • 在 Red Hat Enterprise Linux 9 中:

      $ clang -o <object_file> -c <source_file>
      Copy to Clipboard Toggle word wrap
      • <object_file > 替换为对象文件所需的名称,<source_file > 替换为您的源文件的名称。
  2. 要将目标文件链接在一起,请运行以下命令:

    • 在 Red Hat Enterprise Linux 7 上:

      $ scl enable llvm-toolset-15.0 'clang -o <output_file> <object_file_0> <object_file_1>'
      Copy to Clipboard Toggle word wrap
      • <output_file > 替换为输出文件所需的名称,将 &lt ;object_file > 替换为您要链接的对象文件的名称。
    • 在 Red Hat Enterprise Linux 8 中:

      $ clang -o <output_file> <object_file_0> <object_file_1>
      Copy to Clipboard Toggle word wrap
      • <output_file > 替换为输出文件所需的名称,将 &lt ;object_file > 替换为您要链接的对象文件的名称。
    • 在 Red Hat Enterprise Linux 9 中:

      $ clang -o <output_file> <object_file_0> <object_file_1>
      Copy to Clipboard Toggle word wrap
      • <output_file > 替换为输出文件所需的名称,将 &lt ;object_file > 替换为您要链接的对象文件的名称。
重要

目前,某些库功能被静态链接到使用 LLVM Toolset 构建的应用程序,以支持它们在多个 Red Hat Enterprise Linux 版本上执行。这会造成小的安全风险。如果由于这个风险,红帽会在需要重建应用程序时发布安全勘误。

红帽建议不要静态地链接整个应用程序。

第 3 章 LLDB 调试器

LLDB 调试器是用于调试 C 和 C++ 程序的命令行工具。使用 LLDB 检查正在调试的代码中的内存,控制代码的执行状态,并检测代码的特定部分的执行。

LLVM 工具集随 LLDB 15.0.7 一起分发。

3.1. 先决条件

3.2. 启动调试会话

使用 LLDB 启动交互式调试会话。

流程

  • 要在您要调试的程序上运行 LLDB,请使用以下命令:

    • 在 Red Hat Enterprise Linux 7 上:

      $ scl enable llvm-toolset-15.0 'lldb <binary_file>'
      Copy to Clipboard Toggle word wrap
      • <binary_file > 替换为编译的程序的名称。

        您已以交互模式启动 LLDB 调试会话。您的命令行终端现在显示默认提示 (lldb)

    • 在 Red Hat Enterprise Linux 8 中:

      $ lldb <binary_file_name>
      Copy to Clipboard Toggle word wrap
      • <binary_file > 替换为编译的程序的名称。

        您已以交互模式启动 LLDB 调试会话。您的命令行终端现在显示默认提示 (lldb)

    • 在 Red Hat Enterprise Linux 9 中:

      $ lldb <binary_file>
      Copy to Clipboard Toggle word wrap
      • <binary_file > 替换为编译的程序的名称。

        您已以交互模式启动 LLDB 调试会话。您的命令行终端现在显示默认提示 (lldb)

  • 要退出调试会话并返回到 shell 提示符,请运行以下命令:
(lldb) quit
Copy to Clipboard Toggle word wrap

3.3. 在调试会话中执行程序

使用 LLDB 在调试会话期间执行您的程序。当达到第一个断点、发生错误或程序终止时,您的程序的执行会停止。

先决条件

流程

  • 要执行您要调试的程序,请运行:

    (lldb) run
    Copy to Clipboard Toggle word wrap
  • 要执行使用特定参数调试的程序,请运行:

    (lldb) run <argument>
    Copy to Clipboard Toggle word wrap
    • &lt; parameter > 替换为您要使用的命令行参数。

3.4. 使用断点

使用断点在源代码中的集合点暂停程序的执行。

先决条件

流程

  • 要在特定行中设置一个新的断点,请运行以下命令:

    (lldb) breakpoint set --file <source_file_name> --line <line_number>
    Copy to Clipboard Toggle word wrap
    • <source_file_name > 替换为源文件的名称,将 < line_number > 替换为您要设置断点的行号。
  • 要在特定功能上设置断点,请运行以下命令:

    (lldb) breakpoint set --name <function_name>
    Copy to Clipboard Toggle word wrap
    • 将 < function_name > 替换为您要设置断点的函数名称。
  • 要显示当前设置的断点列表,请运行以下命令:

    (lldb) breakpoint list
    Copy to Clipboard Toggle word wrap
  • 要删除断点,请运行:

    (lldb) breakpoint clear -f <source_file_name> -l <line_number>
    Copy to Clipboard Toggle word wrap
    • <source_file_name > 替换为源文件的名称,将 &lt ;line_number > 替换为您要删除的断点的行号。
  • 要在到达断点后恢复程序的执行,请运行:

    (lldb) continue
    Copy to Clipboard Toggle word wrap
  • 要跳过特定数量的断点,请运行以下命令:

    (lldb) continue -i <breakpoints_to_skip>
    Copy to Clipboard Toggle word wrap
    • <breakpoints_to_skip > 替换为您要跳过的断点数。

      注意

      要跳过循环,请设置 < breakpoints_to_skip& gt;,以匹配循环迭代计数。

3.5. 逐步浏览代码

您可以使用 LLDB 逐步执行程序代码,以在行指针后仅执行一行代码。

先决条件

流程

  • 逐步执行一行代码:

    1. 将行指针设置为您要执行的行。
    2. 运行以下命令:

      (lldb) step
      Copy to Clipboard Toggle word wrap
  • 要逐步执行特定数量的代码:

    1. 将行指针设置为您要执行的行。
    2. 运行以下命令:

      (lldb) step -c <number>
      Copy to Clipboard Toggle word wrap
      • &lt;number> 替换为您要执行的行数。

3.6. 列出源代码

在执行您要调试的程序前,LLDB 调试器会自动显示源代码的前 10 行。每次停止程序执行时,LLDB 会显示它停止的源代码行及其周带的行。您可以使用 LLDB 在调试会话期间手动触发源代码的显示。

先决条件

流程

  • 要列出您要调试的程序的源代码的前 10 行,请运行:

    (lldb) list
    Copy to Clipboard Toggle word wrap
  • 要从特定行显示源代码,请运行:

    (lldb) list <source_file_name>:<line_number>
    Copy to Clipboard Toggle word wrap
    • <source_file_name > 替换为源文件的名称,将 &lt ;line_number > 替换为您要显示的行号。

3.7. 显示当前程序数据

LLDB 调试器为任意复杂性、任何有效表达式和函数调用返回值的变量提供数据。您可以使用 LLDB 显示与程序状态相关的数据。

先决条件

流程

要显示特定变量、表达式或返回值的当前值,请运行:

(lldb) print <data_name>
Copy to Clipboard Toggle word wrap
  • <data_name > 替换为您要显示的数据。

在 RHEL 8 中,您可以使用 Containerfiles 在 Red Hat Universal Base Images (UBI)容器之上构建自己的 LLVM Toolset 容器镜像。

4.1. 在 RHEL 8 上创建 LLVM Toolset 的容器镜像

在 RHEL 8 中,LLVM Toolset 软件包是 Red Hat Universal Base Images (UBI)存储库的一部分。要保持容器镜像大小小,请只安装单独的软件包而不是整个 LLVM Toolset。

先决条件

  • 现有的 Containerfile。
    有关创建 Containerfiles 的详情,请参考 Dockerfile 参考 页面。

流程

  • 访问 红帽容器目录
  • 选择 UBI。
  • Get this image 并按照说明进行操作。
  • 要创建包含 LLVM Toolset 的容器镜像,请在 Containerfile 中添加以下行:
FROM registry.access.redhat.com/ubi8/ubi:latest

RUN yum module install -y llvm-toolset
Copy to Clipboard Toggle word wrap
  • 要创建仅包含单个软件包的容器镜像,请在 Containerfile 中添加以下行:

    RUN yum install -y <package-name>
    Copy to Clipboard Toggle word wrap
    • 将 < package-name > 替换为您要安装的软件包的名称。

第 5 章 LLVM Toolset 中的更改

LLVM Toolset 已从 RHEL 7、RHEL 8 和 RHEL 9 版本 14.0.6 更新至 15.0.7。主要变更包括:

  • -Wimplicit-function-declaration-Wimplicit-int 警告在 C99 及更新版本中默认启用。在 Clang 16 及更高版本中,这些警告将默认出现错误。

法律通告

Copyright © 2023 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat