How to Install GNU GCC (C and C++ Compiler) and Development Tools in RHEL/CentOS and Fedora

Nowadays, as a system administrator or engineer you can’t feel satisfied by knowing how to use the CLI and troubleshoot GNU/Linux servers, but will need to go one step further into the development area as well to stay at the top of your game. If you’re considering a career in kernel development or applications for Linux, then C or C++ is the best place to start.

Install C C++ Compiler and Development Tools

Install C C++ Compiler and Development Tools in RHEL/CentOS and Fedora

Read Also: Install C, C++ and Build Essential Tools in Debian/Ubuntu/Mint

In this article we will explain how to install Gnu C and C++ compilers and it’s related Development tools such as automake, autoconf, flex, bison, etc. in Fedora and CentOS / RHEL systems.

What is Compiler?

In simple words, a compiler is a software program that transforms statements written in a source language into a target language that the machine’s CPU can understand and execute.

In Fedora and derivatives (actually, that is true for the entire Linux distro ecosystem as well), the most well-known C and C++ compilers are gcc and g++, respectively, both developed and supported actively by the Free Software Foundation as part of the GNU project.

Installing GCC (C++ Compiler and Development Tools

If gcc and / or g++ and it’s related Development Tools are not installed in your system by default, you can install the latest available from the repositories as follows:

# yum groupinstall 'Development Tools'		[on CentOS/RHEL 7/6]
# dnf groupinstall 'Development Tools'		[on Fedora 22+ Versions]

Before we dive into writing C or C++ code, there’s another tool to boost your development toolset that we want to show you.

Speeding up C and C++ Compilations in Linux

When you as part of the development process, have to recompile several times after making changes to the source code it’s great to have a compiler cache to speed up future recompilations.

In Linux, there’s an utility called ccache, which speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again. Besides C and C++, it also supports Objective-C and Objective-C++.

Ccache has only a few limitations: it’s only useful while recompiling a single file. For other types of compilations, the process will end up running the actual compiler. The same thing happens if a compiler flag is not supported. The bright side is that in any event it will not interfere with the actual compilation and will not throw an error – just fall back to the actual compiler.

Let’s install this tool:

# yum install ccache 

Speed Up C and C++ Compilations in Linux

and see how it works with an example.

Testing GNU C Compiler with a simple C++ Program

As an example, let’s use a simple C++ program that calculates the area of a rectangle after its length and width have been provided as inputs.

Open your favorite text editor and enter the following code, then save as area.cpp:

#include <iostream> 
using namespace std;  

int main() 
{ 
float length, width, area; 

cout << "Enter the length of the rectangle: "; 
cin >> length; 
cout << "Now enter the width: "; 
cin >> width; 
area = length*width; 

cout <<"The area of the rectangle is: "<< area << endl;

return 0; 
} 

To compile the above code into an executable named area in the current working directory use the -o switch with g++:

# g++ area.cpp -o area

If you want to take advantage of ccache, just prepend the above command with ccache, as follows:

# ccache g++ area.cpp -o area 

Then run the binary:

./area
Sample Output
Enter the length of the rectangle: 2.5
Now enter the width: 3.7
The area of the rectangle is: 9.25

Compile C++ Code in Linux

Don’t let this simple example make you think that ccache is not useful. You will come to know what a great tool ccache is when recompiling a large source code file. The same principle applies for C programs as well.

Summary

In this article we have explained how to install and use the GNU compilers for C and C++ in Fedora-based distributions.

In addition, we showed how to use a compiler cache to speed up recompilations of the same code. While you can refer to the online man pages for gcc and g++ for further options and examples, we look forward to hearing from you if you have any questions or comments.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Gabriel Cánepa

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

11 Responses

  1. Hugh Anglin says:

    yum groupinstall ‘Development Tools’ (no space between group and install)

  2. Spork Schivago says:

    Could someone please explain how to install the various programming man pages? For example, on OpenSuSE, I can type something like:

    # man 3 printf
    

    And see the man page for the printf function. After installing the Development Tools in CentOS, I cannot do this.

    Thank you!

    • Spork Schivago says:

      I believe I figured it out.

      # yum install man-pages libstdc++-docs
      

      The man-pages package provides the man pages for the standard C library, and the libstdc++-docs provides the man pages for C++ and the HTML documentation.

      Thanks!

  3. lmorales says:

    very useful. thanks

  4. Sumeet says:

    Thanks man, great article

  5. Jalal Hajigholamali says:

    Hi,
    Very nice article…
    Thanks a lot

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.