2.2. GNU C++ Compiler

download PDF

2.2.1. Installing the C++ Compiler

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

2.2.2. Using the C++ Compiler

To compile a C++ program on the command line, run the g++ compiler as follows:
scl enable devtoolset-6 '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-6 '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-6 '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-6 '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 2.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-6 'g++ -o hello hello.cpp'
This creates a new binary file called hello in the current working directory.

2.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 2.4. Running a C++ Program on the Command Line

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

2.2.4. C++ Compatibility

All compilers from Red Hat Enterprise Linux versions 5, 6, and 7 and from Red Hat Developer Toolset versions 1, 2, 3, 4, and 6 in any -std mode are compatible with any other of those compilers in C++98 mode.
A compiler in C++11 or C++14 mode is only guaranteed to be compatible with another compiler in C++11 or C++14 mode if they are from the same release series (for example from Red Hat Developer Toolset 6.x).


All compatibility information mentioned in this section is relevant only for Red Hat-supplied versions of the GCC C++ compiler. C++ ABI

Any C++98-compliant binaries or libraries built explicitly with -std=c++98 or -std=gnu++98 can be freely mixed with binaries and shared libraries built by the Red Hat Enterprise Linux 5, 6 or 7 system toolchain GCC. Red Hat recommends use of the -std=c++98 or -std=gnu++98 modes for production software development.
The default language standard setting for Red Hat Developer Toolset is C++14 with GNU extensions, equivalent to explicitly using option -std=gnu++14.
Using and mixing the C++11 and C++14 language versions is supported in Red Hat Developer Toolset only when all C++ objects compiled with the respective flag have been built using the same major version of GCC. As later major versions of Red Hat Developer Toolset may use a later major release of GCC, forward-compatibility of objects, binaries, and libraries built with the options -std=c++11, -std=gnu++11, -std=c++14, and -std=gnu++14 options cannot be guaranteed, and so is not supported.


Use of C++11 and C++14 features in your application requires careful consideration of the above ABI compatibility information.
The mixing of objects, binaries and libraries, built by the Red Hat Enterprise Linux 6 or 7 system toolchain GCC using the -std=c++0x or -std=gnu++0x flags, with those built with the -std=c++11 or -std=gnu++11 or -std=c++14 or -std=gnu++14 flags using the GCC in Red Hat Developer Toolset is explicitly not supported.
Aside from the C++11 and C++14 ABI, discussed above, the Red Hat Enterprise Linux Application Compatibility Specification is unchanged for Red Hat Developer Toolset. When mixing objects built with Red Hat Developer Toolset with those built with theRed Hat Enterprise Linux 6 or 7 toolchain (particularly .o/.a files), the Red Hat Developer Toolset toolchain should be used for any linkage. This ensures any newer library features provided only by Red Hat Developer Toolset are resolved at link-time.
A new standard mangling for SIMD vector types has been added to avoid name clashes on systems with vectors of varying length. By default the compiler still uses the old mangling, but emits aliases with the new mangling on targets that support strong aliases. -Wabi will now display a warning about code that uses the old mangling.
Red Hat logoGithubRedditYoutubeTwitter


Try, buy, & sell


About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.