Chapter 3. 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 4.8. This version is more recent than the version included in Red Hat Enterprise Linux and provides numerous bug fixes and enhancements, including optimization for various new Intel and AMD processors, support for OpenMP 3.1 and link-time optimization. This version also includes experimental support for the C++11 standard, C++11 atomic types, and Transactional Memory. For a detailed list of changes, see Section B.2, “Changes in GCC”.

Note

The Red Hat Developer Toolset version of the GNU Debugger is available for both Red Hat Enterprise Linux 5 and Red Hat Enterprise Linux 6 on 32-bit and 64-bit Intel and AMD architectures. Notable differences are as follows:
  • On Red Hat Enterprise Linux 6, GCC uses the STT_GNU_UNIQUE symbol. On Red Hat Enterprise Linux 5, it does not.
  • On Red Hat Enterprise Linux 6, GCC uses the -march=i686 command line option by default. On Red Hat Enterprise Linux 5, it uses the -march=i586 option.
  • On Red Hat Enterprise Linux 6, GCC uses the -gdwarf-3 command line option by default. On Red Hat Enterprise Linux 5, it uses the -gdwarf-2 option.
  • The -fgnu89-inline command line option has different defaults on Red Hat Enterprise Linux 6 and Red Hat Enterprise Linux 5.

3.1. GNU C Compiler

3.1.1. Installing the C Compiler

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

3.1.2. Using the C Compiler

To compile a C program on the command line, run the gcc compiler as follows:
scl enable devtoolset-2 '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, run the following command:
scl enable devtoolset-2 '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, run:
scl enable devtoolset-2 '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-2 'bash'

Note

To verify the version of gcc you are using at any point, type the following at a shell prompt:
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

Important

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

3.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 type:
./file_name

Example 3.2. Running a C Program on the Command Line

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