3.2. GNU C++ Compiler

3.2.1. Installing the C++ Compiler

In Red Hat Developer Toolset, the GNU C++ compiler is provided by the devtoolset-3-gcc-c++ package and is automatically installed with the devtoolset-3-toolchain package as described in Section 1.5, “Installing Red Hat Developer Toolset”.

3.2.2. Using the C++ Compiler

To compile a C++ program on the command line, run the g++ compiler as follows:
scl enable devtoolset-3 'g++ -o output_file source_file...'
This creates a binary file named output_file in the current working directory. If the -o option is omitted, the g++ 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, run the following command:
scl enable devtoolset-3 'g++ -o object_file -c source_file'
This creates an object file named object_file. If the -o option is omitted, the g++ compiler creates a file named after the source file with the .o file extension. To link object files together and create a binary file, run:
scl enable devtoolset-3 'g++ -o output_file object_file...'
Note that you can execute any command using the scl utility, causing it to be run with the Red Hat Developer Toolset binaries used in preference to the Red Hat Enterprise Linux system equivalent. This allows you to run a shell session with Red Hat Developer Toolset g++ as default:
scl enable devtoolset-3 'bash'


To verify the version of g++ you are using at any point, type the following at a shell prompt:
which g++
Red Hat Developer Toolset's g++ executable path will begin with /opt. Alternatively, you can use the following command to confirm that the version number matches that for Red Hat Developer Toolset g++:
g++ -v


Some newer library features are statically linked into applications built with Red Hat Developer Toolset to support execution on multiple versions of Red Hat Enterprise Linux. This adds a small additional security risk as normal Red Hat Enterprise Linux errata would not change this code. If the need for developers to rebuild their applications due to such an issue arises, Red Hat will signal this via a security erratum. Developers are strongly advised not to statically link their entire application for the same reasons.

Example 3.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;
To compile this source code on the command line by using the g++ compiler from Red Hat Developer Toolset, type:
~]$ scl enable devtoolset-3 'g++ -o hello hello.cpp'
This creates a new binary file called hello in the current working directory.

3.2.3. Running a C++ Program

When g++ 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 type:

Example 3.4. Running a C++ Program on the Command Line

Assuming that you have successfully compiled the hello binary file as shown in Example 3.3, “Compiling a C++ Program on the Command Line”, you can run it by typing the following at a shell prompt:
~]$ ./hello
Hello, World!

3.2.4. C++ Compatibility

Objects compiled with Red Hat Developer Toolset 2 and Red Hat Developer Toolset 3 in C++98 mode (the default mode) are compatible with each other, and with objects compiled with the Red Hat Enterprise Linux system compilers in C++98 mode. Objects compiled in C++11 mode (that is, with -std=c++11) are compatible with all of the above C++98 objects, but are not guaranteed to be compatible with C++11 objects compiled with a different major version of the compiler. That is, if some C++11 objects previously compiled with Red Hat Developer Toolset 2 are rebuilt with Red Hat Developer Toolset 3, all C++11 objects should be rebuilt. For the same reasons, C++11 objects built with this release of Red Hat Developer Toolset may need to be rebuilt to be compatible with objects built by future releases of Red Hat Developer Toolset or Red Hat Enterprise Linux.
C++ Compatibility Matrix

Figure 3.1. C++ Compatibility Matrix