How to install Clang/LLVM 5 and GCC 7 on RHEL

Red Hat Developer

If you are developing with C/C++, Clang tools and newer versions of GCC can be quite helpful for checking your code and giving you better warnings and error messages to help avoid bugs. The newer compilers have better optimizations and code generation.

You can easily install the latest-supported Clang and GCC compilers for C, C++, Objective-C, and FORTRAN using yum on Red Hat Enterprise Linux.  These compilers are available as software collections that are typically updated twice a year. The May 2018 update included Clang/LLVM 5 and GCC 7.3, as well as Go and Rust.

If you want your default gcc to always be GCC 7, or you want clang to always be in your path, this article shows how to permanently enable a software collection by adding it to the profile (dot files) for your user account. A number of common questions about software collections are also answered.

TL;DR

How to install Clang/LLVM 5 and GCC 7

  1. Become root.
  2. Enable the rhscl, devtools, and optional software repos.
  3. Use yum to install devtoolset7 (GCC 7) and llvm-toolset-7 (Clang 5).
  4. Optional: Install the Clang static analysis tools scan-build and clang-tidy
  5. Under your normal user ID, run scl enable to add devtoolset-7 and llvm-toolset-7 to your path(s)
  6. Optional: Permanently enable GCC 7 and Clang 5 by adding scl_source to your .bashrc
$ su -
# subscription-manager repos --enable rhel-7-server-optional-rpms \
    --enable rhel-server-rhscl-7-rpms \
    --enable rhel-7-server-devtools-rpms \
# yum install devtoolset-7 llvm-toolset-7
# yum install llvm-toolset-7-clang-analyzer llvm-toolset-7-clang-tools-extra # optional 
# exit

$ scl enable devtoolset-7 llvm-toolset-7 bash
$ gcc --version
gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)

$ clang --version
clang version 5.0.1 (tags/RELEASE_501/final)

$ # Optionally permanently enable GCC 7 / Clang 5
$ echo "source scl_source enable devtoolset-7 llvm-toolset-7" >> ~/.bashrc

 

 

About software collections

Using software collections takes an extra step because you have to enable the collection you want to use. Enabling just adds the necessary paths (PATH, MANPATH, LD_LIBRARY_PATH) to your environment. Once you get the hang of it, they are pretty ease to use. It really helps to understand the way that environment variable changes work in Linux/UNIX. Changes can only be made to the current process. When a child process is created it inherits the environment of the parent. Any environment changes made in the parent after the child has been created will have no effect on the child.

The benefit of software collections is that you can have GCC 5, 6, 7 installed at the same time along with the base GCC 4 that shipped with Red Hat Enterprise Linux. You can easily switch between versions with scl enable.  Note: The latest interpreted languages like Python 3, PHP 7, and Ruby 2.5 are also available via Red Hat Software Collections. The .NET Core packages that include the C# compiler are also distributed as software collections. So you should take the time to get comfortable with software collections.

 

Enable repos with additional developer tools

While the default/base Red Hat Enterprise Linux software repos have a lot of development tools, these are the older versions that are shipped with the OS and are supported for the full 10-year life of the OS. Packages that are updated more frequently and have a different support life cycle are distributed in other repos that aren’t enabled by default.

To enable the additional repos, run the following command as the root user:

$ su -
# subscription-manager repos --enable rhel-7-server-optional-rpms \
  --enable rhel-server-rhscl-7-rpms \
  --enable rhel-7-server-devtools-rpms \

Notes:

  • You can enter the above all on one line without the backslashes.  The backslashes are needed if you want to use multiple lines for readability.
  • If you are using the workstation variant of Red Hat Enterprise Linux, change -server- to -workstation-.
  • This command only needs to be run once.  The repos will stay enabled.  All of the enabled repos will be searched by yum when installing or updating software.
  • The No-cost Red Hat Enterprise Linux subscription for developers includes access to all of these repos and the server variant of Red Hat Enterprise Linux.  The server variant is a superset.
  • For more information, see the FAQ for the No-cost subscription.

To see which repos are available for your current subscription, run the following command:

# subscription-manager repos --list

To see which repos are enabled, use --list-enabled:

# subscription-manager repos --list-enabled

 

Install GCC 7 and Clang/LLVM 5

You can install GCC 7 and Clang/LLVM 5 with one command:

# yum install devtoolset-7 llvm-toolset-7

Notes:

  • If you only want to install GCC 7, install just devtoolset-7.
  • If you only want to install Clang/LLVM 5, install just llvmtoolset-7.
  • These packages will install in /opt/rh.
  • They will not be added to your path until you do an scl enable.  See below.

View additional packages, see other available versions

You can use yum search to search for additional packages and see the other versions that are available:

GCC:

# yum search devtoolset

Clang/LLVM:

# yum search llvm-toolset

If you’d like to install Clang’s static analysis tools scan-build and clang-tidy, run the following command:

# yum install llvm-toolset-7-clang-analyzer llvm-toolset-7-clang-tools-extra

 

How to install the Eclipse IDE with C/C++ Development Tooling

Red Hat Developer Tools includes the Eclipse IDE with C/C++ Development Tooling (CDT).  The version as of the May 2018 update is 4.7.3 Oxygen.  To install the IDE, run the following command:

# yum install rh-eclipse47

As above, if you want to search for additional packages, or see the other versions that are available, you can use yum search. There are a lot of Eclipse packages, so you might want to redirect the output to a file and then use grep, less, or your favorite text editor.

# yum search rh-eclipse

 

How to use GCC 7 or Clang (scl enable)

GCC 7 and Clang/LLVM 5 are now installed. You no longer need to run under the root user ID. The rest of the commands should be executed using your normal user account.

As previously mentioned, software collections are installed under /opt/rh and aren’t automatically added to your PATH, MANPATH, and LD_LIBRARY_PATH. The command scl enable will make the necessary changes and run a command. Because of the way environment variables work in Linux (and UNIX), the changes will only take affect for the command run by scl enable. You can use bash as the command to start an interactive session. This is one of the most common ways (but not the only way) of working with software collections.

$ scl enable devtoolset-7 bash
$ gcc --version
gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5) 

You can use the which command to see where gcc is found in your PATH.  Note: Once you exit out of the bash shell that was started by scl enable, you will revert to your original PATH, which no longer has the software collection in it:

$ which gcc
/opt/rh/devtoolset-7/root/usr/bin/gcc
$ exit
$ which gcc
/usr/bin/gcc
$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28)

For Clang/LLVM, use llvm-toolset-7 as the collection to enable:

$ scl enable llvm-toolset-7 bash
$ clang --version
clang version 5.0.1 (tags/RELEASE_501/final)
$ which clang
/opt/rh/llvm-toolset-7/root/usr/bin/clang

You can also enable both collections at the same time. The order will determine the order for the directories in the PATH. If there are any commands that are in both collections, the last collection added will take precedence.

$ scl enable devtoolset-7 llvm-toolset-7 bash
$ echo $PATH
/opt/rh/llvm-toolset-7/root/usr/bin:/opt/rh/llvm-toolset-7/root/usr/sbin:/opt/rh/devtoolset-7/root/usr/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin

 

How to permanently enable a software collection

To permanently enable GCC 7 and/or Clang/LLVM 5, you can add an scl_source command to the “dot files” for your specific user ID. This is the recommend approach for development, as only processes that run under your user ID will be affected.  The benefit of this approach is if you are using a graphical desktop, anything that you start from the menu will already have the collection enabled.

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

# Add Red Hat Developer Toolset (GCC 7 and Clang 5) to my login environment
source scl_source enable devtoolset-7 llvm-toolset-7

Note: you could also add the line to the start of a build script to select the desired compiler for the build.  If your build script isn’t written as a shell/bash script you could just wrap it in a shell script that has the source scl_source collection-name command and then runs your build script.

A caveat with permanently enabling a software collection is that there is no disable command. Everything is in environment variables, so you can work around it, but it would be a manual process.

 

But I need GCC 6, how do I install it?

If you need GCC 6 instead of GCC 7, you can follow these same instructions.  Installing devtoolset-6 will install GCC 6.3.

Note: There is no devtoolset-5. GCC 5 was included in devtoolset-4, which you could also install if needed. The version number was bumped to 6, skipping 5, in order to sync with GCC major versions.

Using software collections you can have GCC 4, 5, 6, and 7 all installed at the same time and easily switch between them.

 

How to look at the manual page for a specific version

Enabling a software collection will prepend that software collection’s manual pages to the MANPATH, which is the search path for manual pages. Just like commands, any existing manual pages in later directories with the same name will be hidden.  This is one of the times when it can be useful to run a single command, like man with scl enable, instead of starting a bash shell.

$ scl enable devtoolset-7 man gcc  # see the GCC-7 man page
$ scl enable devtoolset-6 man gcc  # see the GCC-6 man page

Since there is no disable command, you need a work around to see the man page for GCC-4:

$ (MANPATH=/usr/share/man && man gcc)

The above command just changes MANPATH in a subshell so it won’t affect anything else.

Note: The same techniques will work for other commands like GNU info and INFOPATH.

 

How to see which software collections are installed

You can use the command scl -l to see what software collections are installed.  This will show all software collections that are installed, whether they are enabled or not.

$ scl -l
devtoolset-7
llvm-toolset-7
rh-python36

 

How to tell which software collections are enabled

The environment variable X_SCLS contains a list of the software collections that are currently enabled. This is handy to use in shell scripts.

$ echo $X_SCLS
$ for scl in $X_SCLS; do echo $scl; done
llvm-toolset-7
devtoolset-7

 

Compiler tips

There are a number of helpful articles on the Red Hat Developer blog:

Getting started with Clang/LLVM

See Tom Stellard‘s article:

Getting started with llvm-toolset

Recommended compiler and linker flags for GCC

See Florian Weimer‘s article:

Recommended compiler and linker flags for GCC

 

Implicit fall through detection with GCC 7

Marek Polacek‘s article describes a GCC 7 warning you can enable to detect missing break statements inside of a switch block. The warning is enabled with -Wimplicit-fallthrough.  It is also one of the warnings that will be enabled if you use -Wextra.

-Wimplicit-fallthrough in GCC 7

Memory error detection using GCC 7

See Martin Sebor’s article:

Memory Error Detection Using GCC

 

 

 

Product information and documentation

For information and documentation about these Red Hat products:

 

Upstream community documentation

 

 

Join the Red Hat Developer Program (it’s free) and get access to related cheat sheets, books, and product downloads.

Take advantage of your Red Hat Developers membership and download RHEL today at no cost.

Share