Featured image for: Value range propagation in GCC with Project Ranger.

In this tutorial, you will install the GNU Compiler Collection 12 from the Red Hat Developer Toolset and build a simple C++ Hello World application. This tutorial should take less than 30 minutes to complete.

Prerequisites

Before you begin, you will need a current Red Hat Enterprise Linux 8 workstation or server subscription that allows you to download software and get updates from Red Hat. If you don’t have an active subscription, you can register and obtain the Red Hat Enterprise Linux Developer Suite (including the RHEL server).

Step 1. Discovering GCC-related repositories in RHEL 8

Managing software repositories in Red Hat Enterprise Linux 8 is essential for developers and system administrators. To find the necessary GCC-related repositories, you can use the following command. This command helps you discover the repositories containing GCC-related tools, ensuring you have access to the latest and most up-to-date resources for your development needs. This is a valuable feature for simplifying repository management.

Check the version of RHEL on the server:

#  cat /etc/redhat-release
Red Hat Enterprise Linux release 8.8 (Ootpa)

List the GCC-related repositories using the following command:

$ sudo dnf provides '*bin/gcc'

Step 2. Set up your development environment

In this step, you will use a single command to download and install GCC 12.x and other development tools that are part of the Red Hat Developer Toolset. The length of time this step takes depends on the speed of your Internet connection and system. With a reasonably fast connection, this step should take only a minute.

Red Hat Enterprise Linux provides a comprehensive set of tools for developing C and C++ applications.

  1. Download and install the development tools package group, which includes GNU Compiler Collection (GCC), GNU Debugger (GDB), and other development tools:
$ sudo dnf group install "Development Tools"
  1. Install the LLVM-based toolchain including the clang compiler and lldb debugger:
$ sudo dnf install llvm-toolset
  1. If Fortran is needed, install the GNU Fortran compiler:
$ sudo dnf install gcc-gfortran

Step 3. Build a Hello World application

Use DTS C++ from the command line

The GNU C++ compiler runs with the gcc command. Add DTS to your environment with scl enable in a terminal window. You can also refer to Permanently adding DTS to your Development Environment for more information.

$ scl enable gcc-toolset-12 bash

1. Create a directory hello-cpp and change to it:

$ mkdir hello-cpp
$ cd hello-cpp

2. Create a file hello.cpp with the following contents:

hello.cpp

#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
  cout << "Hello, Red Hat Developer Program World!" << endl;
  return 0;
}

3. Now compile and run the program:

$ gcc -o hello hello.cpp

This will compile the code.

4. Create the object the hello.cpp and link the executable hello file from the object file.

$./hello
Hello, Red Hat Developer Program World!

For more information, read the GNU C++ Compiler section of the Red Hat GCC Toolset 12 User Guide.

Docker-formatted container images

You can use Docker-formatted container images to run Red Hat Developer Toolset components inside virtual software containers, thus isolating them from the host system and allowing rapid deployment. For a detailed description of the Developer Toolset docker-formatted container images and dockerfiles, refer to Using Red Hat Software Collections Container Images.

Working with the GCC Toolset

The GCC Toolset in Red Hat Enterprise Linux 8 remains supported, allowing developers to access cutting-edge development and performance analysis tools. This toolset is similar to the Red Hat Developer Toolset for RHEL 7 and is conveniently available as a software collection in the AppStream repository. Notably, it is fully supported under Red Hat Enterprise Linux Subscription Level Agreements, making it suitable even for production purposes.

Developers using Red Hat Enterprise Linux 8 can leverage the GCC Toolset to benefit from more advanced and up-to-date tools that enhance efficiency and productivity. This includes a wide range of features such as improved optimizations, enhanced diagnostics, better performance analysis tools, and increased compatibility with modern programming languages.

By providing a software collection within the AppStream repository, Red Hat ensures that developers have easy access to the GCC Toolset while maintaining consistent system stability and security standards. Moreover, its inclusion under Subscription Level Agreements reflects Red Hat's dedication to offering dependable products that meet enterprise requirements.

Permanently adding DTS to your development environment

To make DTS a permanent part of your development environment, you can add it to the login script for your specific user ID. This is the recommended approach for development because only processes run under your user ID will be affected.

1. Using your preferred text editor, add the following line to the end of ~/.bashrc:

$ source scl_source enable gcc-toolset-12

2. After logging out and logging back in again, you can verify that the DTS GCC is in your path by running which gcc or gcc --version.

$ which gcc
/opt/rh/gcc-toolset-12/root/usr/bin/gcc
​​​​​​​$ gcc -v
gcc (GCC) 12.2.1 20221121 (Red Hat 12.2.1-7)

Troubleshooting and FAQ

1. As a developer, how can I get a Red Hat Enterprise Linux subscription that includes the Clang/LLVM, Go, or Rust Compilers?

Developers can get a no-cost Red Hat Enterprise Linux Developer subscription for development purposes by registering and downloading through developers.redhat.com. We recommend you follow our Getting Started Guide which covers downloading and installing Red Hat Enterprise Linux on a physical system or virtual machine (VM) using your choice of VirtualBox, VMware, Microsoft Hyper-V, or Linux KVM/Libvirt. For more information, see Frequently asked questions: no-cost Red Hat Enterprise Linux Developer subscription.

2. When I run the dnf install package (gcc-toolset-12, llvm-toolset-9, rust-toolset-9), it fails due to a missing dependency.

Packages from the devtools RPMs repository, which is not by default enabled, are needed by several software collections. For instructions on how to enable the relevant RPMs and repositories.

3. How can I view the Go manual pages?

The Go compiler help command provides information on its usage. To show the help index:

$ man go

4. How can I find out what RHSCL packages are installed?

The scl list-collections will show the list of RHSCL packages that have been installed, whether they are enabled or not.

$ scl list-collections

Find more resources

In this article, we installed GCC development tools on RHEL 8 and created a simple cpp application that printed hello-world and executed by the GCC command line tool.

For a deeper and practical understanding of Red Hat Enterprise Linux, you can engage in thoughtfully curated hands-on labs. Red Hat Universal Base Images (UBI) are container-based and operating system images with complementary runtime languages and packages. Try the Red Hat UBI hands-on lab.

You can also obtain tailored Red Hat Enterprise Linux images designed for AWS, Google Cloud Platform, Microsoft Azure, and VMware, facilitating their seamless deployment on your chosen platform.