Este contenido no está disponible en el idioma seleccionado.
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 12.2.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-12-gcc package and is automatically installed with devtoolset-12-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-12 '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-12 '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-12 '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-12 'bash'
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-12 '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-12-gcc-c++ package and is automatically installed with the devtoolset-12-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-12 '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-12 '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-12 '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-12 'bash'
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-12 '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 to 10 in any -std
mode are compatible with any other of those compilers in C++98 mode.
A compiler in C++11, C++14, or C++17 mode is only guaranteed to be compatible with another compiler in those same modes if they are from the same release series.
Supported examples:
- C++11 and C++11 from Red Hat Developer Toolset 6.x
- C++14 and C++14 from Red Hat Developer Toolset 6.x
- C++17 and C++17 from Red Hat Developer Toolset 10.x
- The GCC compiler in Red Hat Developer Toolset 10.x can build code using C++20 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 12.1 is C++17 with GNU extensions, equivalent to explicitly using option -std=gnu++17
.
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.
Starting with Red Hat Developer Toolset 10.x, using the C++17 language version is no longer experimental and is supported by Red Hat. All C++ objects compiled with C++17 must be built using Red Hat Developer Toolset 10.x or later.
Use of C++11, C++14, and C++17 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 7 system toolchain GCC using the -std=c++0x
or -std=gnu++0x
flags, with those built with the C++11 or later language versions using the GCC in Red Hat Developer Toolset is explicitly not supported.
Aside from the C++11, C++14, and C++17 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 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.
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-12-gcc-gfortran package and is automatically installed with devtoolset-12-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-12 '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-12 '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-12 '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-12 'bash'
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-12 '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.
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-12 '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-12 '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 asgcc
. To display the manual page for the version included in Red Hat Developer Toolset:$
scl enable devtoolset-12 '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-12 'man gfortran'
C++ Standard Library Documentation — Documentation on the C++ standard library can be optionally installed:
# yum install devtoolset-12-libstdc++-docs
Once installed, HTML documentation is available at
/opt/rh/devtoolset-12/root/usr/share/doc/devtoolset-12-libstdC++-docs-12.2.1/html/index.html
.
Online Documentation
- Red Hat Enterprise Linux 7 Developer Guide — The Developer Guide for Red Hat Enterprise Linux 7 provides in-depth information about GCC.
- Using the GNU Compiler Collection — The upstream GCC manual provides an in-depth description of the GNU compilers and their usage.
- The GNU C++ Library — The GNU C++ library documentation provides detailed information about the GNU implementation of the standard C++ library.
-
The GNU Fortran Compiler — The GNU Fortran compiler documentation provides detailed information on
gfortran
's usage.
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 8, GNU Debugger (GDB) — Instructions on debugging programs written in C, C++, and Fortran.