-
Language:
English
-
Language:
English
Red Hat Training
A Red Hat training course is available for Red Hat Developer Toolset
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 5.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.
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-4-gcc package and is automatically installed with devtoolset-4-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-4 '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-4 '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-4 '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-4 '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.
Note
The Red Hat Developer Toolset 4.1 version of GCC supports Cilk+, an extension to the C and C++ languages for parallel programming, which can be enabled using the
-fcilkplus
option. A runtime library, libcilkrts
, is included in this release to support Cilk+. The libcilkrts
library has been a part of Red Hat Enterprise Linux since version 7.2, but the package is not included in all supported Red Hat Enterprise Linux releases. To enable dynamic linkage of binaries and libraries built with Red Hat Developer Toolset 4.1 GCC using Cilk+ features on supported Red Hat Enterprise Linux releases that do not contain libcilkrts
, install the libcilkrts.so
shared library from Red Hat Developer Toolset 4.1 with such binaries or libraries.
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-4 '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!