There has been a lot of work to improve C/C++ compilers in recent years. A number of articles have been posted by Red Hat engineers working on the compilers themselves covering usability improvements, features to detect possible bugs, and security issues in your code.
Red Hat Enterprise Linux 8 Beta ships with GCC 8 as the default compiler. This article shows you how to install GCC 8 as well as Clang/LLVM 6 on Red Hat Enterprise Linux 7. You'll be able to use the same updated (and supported) compilers from Red Hat on both RHEL 7 and 8.
If you want your default gcc
to always be GCC 8, 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.
Note: The package naming conventions have changed to be more consistent with compiler major version numbers. For LLVM/Clang 6, install llvm-toolset-6.0
. Make sure you include the "dot zer0"! Do not install llvm-toolset-7
unless what you want is the previous major version, LLVM/Clang 5.
TL;DR
How to install GCC 8 and Clang/LLVM 6.0
- Become root.
- Enable the
rhscl
,devtools
, andoptional
software repos. - Add the Red Hat Developer Tools key to your system.
- Use
yum
to installdevtoolset-8
(GCC 8) andllvm-toolset-6.0
(Clang 6). - Optional: Install the Clang static analysis tools
scan-build
andclang-tidy
. - Under your normal user ID, run
scl enable
to adddevtoolset-8
andllvm-toolset-6.0
to your path(s). - Optional: Permanently enable GCC 8 and Clang 6 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-8 llvm-toolset-6.0
# yum install llvm-toolset-6.0-clang-analyzer llvm-toolset-6.0-clang-tools-extra # optional
# exit
$ scl enable devtoolset-8 llvm-toolset-6.0 bash
$ gcc --version
gcc (GCC) 8.2.1 20180905 (Red Hat 8.2.1-3)
$ clang --version
clang version 6.0.1 (tags/RELEASE_601/final)
$ # Optionally permanently enable GCC 8 / Clang/LLVM 6.0
$ echo "source scl_source enable devtoolset-8 llvm-toolset-6.0" >> ~/.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 easy 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 6, 7, and 8 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, such as 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 commands 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
subscription-manager
command 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-
. - The above 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 8 and Clang/LLVM 6.0
You can install GCC 8 and Clang/LLVM 6.0 with one command:
# yum install devtoolset-8 llvm-toolset-6.0
Notes:
- If you only want to install GCC 8, install just
devtoolset-8
. - If you only want to install Clang/LLVM 6, install just
llvmtoolset-6.0
. - 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 and 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-6.0-clang-analyzer llvm-toolset-6.0-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 November 2018 update is 4.8 Photon. To install the IDE, run the following command:
# yum install rh-eclipse48
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 8 or LLVM/Clang 6 (scl enable)
GCC 8 and Clang/LLVM 6 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 effect 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-8 bash
$ gcc --version
gcc (GCC) 8.2.1 20180905 (Red Hat 8.2.1-3)
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-8/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-6.0
as the collection to enable:
$ scl enable llvm-toolset-6.0 bash
$ clang --version
clang version 6.0.1 (tags/RELEASE_601/final)
$ which clang
/opt/rh/llvm-toolset-6.0/root/usr/bin/clang
You can also enable both collections at the same time. The order you use 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-8 llvm-toolset-6.0 bash
$ echo $PATH
/opt/rh/llvm-toolset-6.0/root/usr/bin:/opt/rh/llvm-toolset-6.0/root/usr/sbin:/opt/rh/devtoolset-8/root/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
How to permanently enable a software collection
To permanently enable GCC 8 and/or Clang/LLVM 6.0, you can add an scl_source
command to the "dot files" for your specific user ID. This is the recommended 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.
Note: 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.
Using your preferred text editor, add the following line to your ~/.bashrc
:
# Add Red Hat Developer Toolset (GCC 8 and Clang 6) to my login environment
source scl_source enable devtoolset-8 llvm-toolset-6.0
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.
But I need GCC 7 or LLVM 5; how do I install those?
See the previous article: How install Clang/LLVM 5 and GCC 7 on Red Hat Enterprise Linux.
In general, you can follow the instructions in this article to install the compilers distributed as Red Hat Software Collections. Use devtoolset-6
or devtoolset-7
instead of devtoolset-8
, or use llvm-toolset-7
instead of llvm-toolset-6.0
.
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 multiple versions of GCC and/or LLVM 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-8 man gcc # see the GCC-8 man page
$ scl enable devtoolset-7 man gcc # see the GCC-7 man page
Since there is no disable command, you need a workaround 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-8
llvm-toolset-6.0
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-6.0
devtoolset-8
Articles for C/C++ developers
There are a number of articles on the Red Hat Developer blog that are helpful to C/C++ developers:
- Recommended compiler and linker flags for GCC—Improve warnings and code generation with the right flags.
- Usability improvements in GCC 8
- Getting started with Clang/LLVM
- Detecting String Truncation with GCC 8
- Implicit fall through detection with GCC 7—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
. - Memory error detection using GCC 7
- Diagnosing Function Pointer Security Flaws with a GCC plugin
- Toward a Better Use of C11 Atomics – Part 1
Product information and documentation
For information and documentation about these Red Hat products:
- GCC: Red Hat Developer Toolset
- Clang/LLVM, Go, and Rust compilers: Red Hat Developer Tools
- Support Lifecycle for Clang/LLVM, Go, and Rust
- Dynamic languages and many other updated packages: Red Hat Software Collections
Upstream community documentation
- GCC 8 Release Series: Changes, New Features, and Fixes
- Clang.llvm.org—Clang pages on the LLVM project site
- Clang 6.0 Release Notes
- LLVM 6.0 Release Notes
Last updated: November 1, 2023