The GNU Compiler Collection (GCC) is a collection of compilers, including C, C++, Objective-C, Fortran, Ada, Go, and D languages. Many open source projects including GNU tools and Linux Kernel are compiled with GCC.
This guide explains how to install the GCC compiler on CentOS 7. We will explain how to install the stable version and install the updated version from the SCL source.
To add new software sources and install software packages on your CentOS system, you must log in to the system as root or a user with sudo privileges.
The default CentOS software source contains a package group named Development Tools
. This combination includes the GCC compiler and a series of library files, as well as other tools needed to compile the software.
To install Development Tools including the GCC compiler, run:
sudo yum group install "Development Tools"
This command installs a new set of packages, including gcc
, g++
, and make
.
You may also want to install a user manual on developing with GNU/Linux:
sudo yum install man-pages
Use gcc -version
to verify whether the GCC compiler is installed successfully, it will print the GCC version:
gcc --version
On the CentOS 7 software source, the default GCC available version is 4.8.5
:
gcc(GCC)4.8.520150623(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 has been successfully installed on your system, and you can start using it.
Compiling a C or C++ program with GCC is a very simple task. Open your text editor and create the following files:
nano hello.c
# include <stdio.h>
int main(){printf("Hello World!\n");return0;}
Save the file and compile it into an executable file with the following command:
gcc hello.c -o hello
This will create a binary file named hello
in the same folder.
Execute this hello
program:
. /hello
The program should output:
Hello World!
In this section, we will provide instructions on how to install and use multiple versions of GCC on CentOS 7. The updated version of GCC compiler includes support for new languages, better performance, and additional features.
Software Collections, as we know it, SCL is a community project that allows you to compile, install, and use multiple versions of software on the same system. By enabling software collections, you can install newer versions of programming languages and services that are not on the core software source.
The SCL software source provides a package called Developer Toolset
, which contains an updated version of the GNU Compiler Collection, as well as other development and debugging tools.
First, install the CentOS SCL file. It is part of the CentOS additional source, you can install it with the following command:
sudo yum install centos-release-scl
Now, the following collection of development tools are available:
In this example, we install Developer Toolset 7. To do this, enter the following command in your CentOS terminal
sudo yum install devtoolset-7
To access GCC 7, you need to use the software collection tool scl
to start a new shell:
scl enable devtoolset-7 bash
Now, if you check your GCC version, you can notice that GCC7 is already the default version of your current shell:
gcc --version
gcc(GCC)7.3.120180303(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 new version of GCC, just like other tools.
You have successfully installed GCC on your CentOS 7. Now you can browse Official GCC Documentation Page, and learn how to use GCC and G++ to compile your C and C++ programs.
Recommended Posts