Red Hat Training

A Red Hat training course is available for Red Hat Developer Toolset

Chapter 2. GNU Compiler Collection (GCC)

The GNU Compiler Collection, commonly abbreviated GCC, is a portable compiler suite with support for a wide selection of programming languages.

Red Hat Developer Toolset is distributed with GCC 8.3.1. This version is more recent than the version included in Red Hat Enterprise Linux and provides a number of bug fixes and enhancements.

2.1. GNU C Compiler

2.1.1. Installing the C Compiler

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

2.1.2. Using the C Compiler

To compile a C program on the command line, run the gcc compiler as follows:

$ scl enable devtoolset-8 'gcc -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 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,:

$ scl enable devtoolset-8 'gcc -o object_file -c source_file'

This creates an object file named object_file. If the -o option is omitted, the compiler creates a file named after the source file with the .o file extension. To link object files together and create a binary file:

$ scl enable devtoolset-8 'gcc -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 gcc as default:

$ scl enable devtoolset-8 'bash'
Note

To verify the version of gcc you are using at any point:

$ which gcc

Red Hat Developer Toolset’s gcc 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 gcc:

$ gcc -v

Example 2.1. Compiling a C Program on the Command Line

Consider a source file named hello.c with the following contents:

#include <stdio.h>

int main(int argc, char *argv[]) {
  printf("Hello, World!\n");
  return 0;
}

Compile this source code on the command line by using the gcc compiler from Red Hat Developer Toolset:

$ scl enable devtoolset-8 'gcc -o hello hello.c'

This creates a new binary file called hello in the current working directory.

2.1.3. Running a C Program

When gcc 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 run it:

$ ./file_name

Example 2.2. Running a C Program on the Command Line

Assuming that you have successfully compiled the hello binary file as shown in Example 2.1, “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. GNU C++ Compiler

2.2.1. Installing the C++ Compiler

In Red Hat Developer Toolset, the GNU C++ compiler is provided by the devtoolset-8-gcc-c++ package and is automatically installed with the devtoolset-8-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-8 '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:

$ scl enable devtoolset-8 '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:

$ scl enable devtoolset-8 '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-8 'bash'
Note

To verify the version of g++ you are using at any point:

$ 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

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;
}

Compile this source code on the command line by using the g++ compiler from Red Hat Developer Toolset:

$ scl enable devtoolset-8 '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 run it:

$ ./file_name

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:

$ ./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).

Important
  • Compilers in Red Hat Developer Toolset 7.x and 8.x can build code using C++17 but this capability is experimental and not supported by Red Hat.
  • All compatibility information mentioned in this section is relevant only for Red Hat-supplied versions of the GCC C++ compiler.

2.2.4.1. C++ ABI

Any C++98-compliant binaries or libraries built by the Red Hat Developer Toolset toolchain 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 GCC.

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 the C++14 language version is supported in Red Hat Developer Toolset when all C++ objects compiled with the respective flag have been built using Red Hat Developer Toolset 6 or later. Objects compiled by the system GCC in its default mode of C++98 are also compatible, but objects compiled with the system GCC in C++11 or C++14 mode are not compatible.

Using the C++17 language version is experimental and possible 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. Because later major versions of Red Hat Developer Toolset may use a later major release of GCC, forward compatibility of objects, binary files, and libraries built with the -std=c++17 and -std=gnu++17 options cannot be guaranteed.

Important

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 the Red 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 lengths. The compiler in Red Hat Developer Toolset uses the new mangling by default. It is possible to use the previous standard mangling by adding the -fabi-version=2 or -fabi-version=3 options to GCC C++ compiler calls. To display a warning about code that uses the old mangling, use the -Wabi option.

On Red Hat Enterprise Linux 7, the GCC C++ compiler still uses the old mangling by default, but emits aliases with the new mangling on targets that support strong aliases. It is possible to use the new standard mangling by adding the -fabi-version=4 option to compiler calls. To display a warning about code that uses the old mangling, use the -Wabi option.

On Red Hat Enterprise Linux 6, the GCC C++ compiler supports only the previous standard mangling.

2.3. GNU Fortran Compiler

2.3.1. Installing the Fortran Compiler

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

2.3.2. Using the Fortran Compiler

To compile a Fortran program on the command line, run the gfortran compiler as follows:

$ scl enable devtoolset-8 'gfortran -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 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:

$ scl enable devtoolset-8 'gfortran -o object_file -c source_file'

This creates an object file named object_file. If the -o option is omitted, the compiler creates a file named after the source file with the .o file extension. To link object files together and create a binary file:

$ scl enable devtoolset-8 'gfortran -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 gfortran as default:

$ scl enable devtoolset-8 'bash'
Note

To verify the version of gfortran you are using at any point:

$ which gfortran

Red Hat Developer Toolset’s gfortran 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 gfortran:

$ gfortran -v

Example 2.5. Compiling a Fortran Program on the Command Line

Consider a source file named hello.f with the following contents:

program hello
  print *, "Hello, World!"
end program hello

Compile this source code on the command line by using the gfortran compiler from Red Hat Developer Toolset:

$ scl enable devtoolset-8 'gfortran -o hello hello.f'

This creates a new binary file called hello in the current working directory.

2.3.3. Running a Fortran Program

When gfortran 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 run it:

$ ./file_name

Example 2.6. Running a Fortran Program on the Command Line

Assuming that you have successfully compiled the hello binary file as shown in Example 2.5, “Compiling a Fortran Program on the Command Line”, you can run it:

$ ./hello
 Hello, World!

2.4. Specifics of GCC in Red Hat Developer Toolset

Static linking of libraries

Certain more recent 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 creates an additional minor security risk as standard Red Hat Enterprise Linux errata do not change this code. If the need arises for developers to rebuild their applications due to this risk, Red Hat will communicate this using a security erratum.

Important

Because of this additional security risk, developers are strongly advised not to statically link their entire application for the same reasons.

Specify libraries after object files when linking

In Red Hat Developer Toolset, libraries are linked using linker scripts which might specify some symbols through static archives. This is required to ensure compatibility with multiple versions of Red Hat Enterprise Linux. However, the linker scripts use the names of the respective shared object files. As a consequence, the linker uses different symbol handling rules than expected, and does not recognize symbols required by object files when the option adding the library is specified before options specifying the object files:

$ scl enable devtoolset-8 'gcc -lsomelib objfile.o'

Using a library from the Red Hat Developer Toolset in this manner results in the linker error message undefined reference to symbol. To prevent this problem, follow the standard linking practice, and specify the option adding the library after the options specifying the object files:

$ scl enable devtoolset-8 'gcc objfile.o -lsomelib'

Note that this recommendation also applies when using the base Red Hat Enterprise Linux version of GCC.

2.5. Additional Resources

For more information about the GNU Compiler Collections and its features, see the resources listed below.

Installed Documentation

  • gcc(1) — The manual page for the gcc compiler provides detailed information on its usage; with few exceptions, g++ accepts the same command line options as gcc. To display the manual page for the version included in Red Hat Developer Toolset:

    $ scl enable devtoolset-8 'man gcc'
  • gfortran(1) — The manual page for the gfortran compiler provides detailed information on its usage. To display the manual page for the version included in Red Hat Developer Toolset:

    $ scl enable devtoolset-8 'man gfortran'
  • C++ Standard Library Documentation — Documentation on the C++ standard library can be optionally installed:

    # yum install devtoolset-8-libstdc++-docs

    Once installed, HTML documentation is available at /opt/rh/devtoolset-8/root/usr/share/doc/devtoolset-8-libstdC++-docs-8.3.1/html/index.html.

Online Documentation

See Also

  • Chapter 1, Red Hat Developer Toolset — An overview of Red Hat Developer Toolset and more information on how to install it on your system.
  • Chapter 4, binutils — Instructions on using binutils, a collection of binary tools to inspect and manipulate object files and binaries.
  • Chapter 5, elfutils — Instructions on using elfutils, a collection of binary tools to inspect and manipulate ELF files.
  • Chapter 6, dwz — Instructions on using the dwz tool to optimize DWARF debugging information contained in ELF shared libraries and ELF executables for size.
  • Chapter 7, GNU Debugger (GDB) — Instructions on debugging programs written in C, C++, and Fortran.