Read in 4 minutes

last updated 

How to Install GCC Compiler on CentOS 7

The GNU Compiler Collection (GCC) is a collection of compilers and libraries for C, C++, Objective-C, Fortran, Ada, Go, and D, programming languages. Many open source projects including the GNU tools and the Linux kernel are compiled with GCC.

This tutorial explains how to install GCC compiler on CentOS 7. We will show you how to install the distro stable version and the newer version of GCC available from the SCL repository.

Prerequisites

To be able to add new repositories and install packages on your CentOS system, you must be logged in as a user with sudo privileges.

Installing GCC on CentOS

The default CentOS repositories contain a package group named Development Tools that contains the GCC compiler and a lot of libraries and other utilities required for compiling software.

To install the Development Tools including the GCC Compiler run:

sudo yum group install "Development Tools"

The command will install a bunch of new packages including gcc, g++ and make.

You may also want to install the manual pages about using GNU/Linux for development:

sudo yum install man-pages

Validate that the GCC compiler is successfully installed by using the gcc --version command which will print the GCC version:

gcc --version

The default version of GCC available in the CentOS 7 repositories is 4.8.5:

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

That’s it. GCC is now installed on your system and you can start using it.

Compiling a Hello World Example

Compiling a basic C or C++ program using GCC is an easy task. Open your text editor and create the following file:

nano hello.c
hello.c
#include <stdio.h>
int main()
{
  printf ("Hello World!\n");
  return 0;
}

Save the file and compile it into an executable by running the following command:

gcc hello.c -o hello

This will create a binary file named hello in the same directory where you run the command.

Execute the hello program with:

./hello.

The program will display:

Hello World!

Installing Multiple GCC Versions

In this section, we will provide instructions about how to install and use multiple versions of GCC on CentOS 7. The newer versions of the GCC compiler include support for new languages, better performance, extended features.

Software Collections, also known as SCL is a community project that allows you to build, install, and use multiple versions of software on the same system, without affecting system default packages. By enabling Software Collections you will gain access to the newer versions of programming languages and services which are not available in the core repositories.

The SCL repositories provide a package named Developer Toolset which includes newer versions of the GNU Compiler Collection, and other development and debugging tools.

First, install the CentOS SCL release file. It is part of the CentOS extras repository and can be installed by running the following command:

sudo yum install centos-release-scl

Currently, the following Developer Toolset collections are available:

  • Developer Toolset 7
  • Developer Toolset 6

In this example we will install the Developer Toolset version 7. To do so type the following command on your CentOS 7 terminal:

sudo yum install devtoolset-7

To access GCC version 7 you need to launch a new shell instance using the Software Collection scl tool:

scl enable devtoolset-7 bash

Now if you check the GCC version, you’ll notice that GCC 7 is the default version in your current shell:

gcc --version
gcc (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

At this point, you can use the newer GCC version just as any other normal tool.

Conclusion

You have successfully installed GCC on your CentOS 7. You can now visit the official GCC Documentation page and learn how to use GCC and G++ to compile your C and C++ programs.

If you hit a problem or have feedback, leave a comment below.