Installing and Using the GCC Command: Linux User Guide

Illustration of a Linux terminal displaying the installation of the gcc command the GNU Compiler Collection for C

Are you planning to compile your C programs in Linux? You might find the task daunting, especially if you’re a beginner. However, the ‘GCC’ command, akin to a skilled translator, converts your C code into an executable program. It’s a tool that’s worth learning to install and use. GCC is readily available on most package management systems, making the installation process fairly straightforward.

In this guide, we will navigate you through the process of installing and using the GCC command in Linux. We will provide you with instructions for both APT and YUM-based distributions, delve into compiling GCC from source, installing a specific version, and finally, how to use the GCC command and ensure it’s installed correctly.

So, let’s dive in and start installing GCC on your Linux system!

TL;DR: How Do I Install and Use the GCC Command in Linux?

In most Linux distributions, the GCC command comes pre-installed. However, if it’s not, you can install it in Debian based distributions like Ubuntu, by running the command sudo apt-get install gcc. For RPM-based distributions like CentOS, you would run the command sudo yum install gcc.

# For Debian based distributions like Ubuntu
sudo apt-get install gcc

# For RPM-based distributions like CentOS
sudo yum install gcc

# Output:
# 'gcc is already the newest version (4:7.4.0-1ubuntu2.3).'
# OR
# 'Package gcc is not available, but is referred to by another package...'

This is just a basic way to install the GCC command in Linux, but there’s much more to learn about installing and using GCC. Continue reading for more detailed information and advanced usage scenarios.

Getting Started with GCC Command in Linux

If you’re new to Linux or programming, you might be wondering what GCC is. GCC, short for GNU Compiler Collection, is a compiler system that supports various programming languages, including C, C++, Objective-C, Fortran, Ada, and more. It’s an essential tool for developers as it translates your source code into an executable program that your machine can understand.

Installing GCC with APT

If you’re using a Debian-based distribution like Ubuntu, you’ll likely be using the APT package manager. Here’s how you can install GCC:

sudo apt update
sudo apt upgrade
sudo apt install build-essential

# Output:
# 'Reading package lists... Done'
# 'Building dependency tree'
# 'Reading state information... Done'
# 'gcc is already the newest version (4:7.4.0-1ubuntu2.3).'
# OR
# 'Setting up gcc (4:7.4.0-1ubuntu2.3) ...'

In this code block, we first update the package list and upgrade the system. Then, we install build-essential which includes GCC and other necessary packages for building software.

Installing GCC with YUM

For RPM-based distributions like CentOS, the YUM package manager is commonly used. Here’s how you can install GCC with YUM:

sudo yum check-update
sudo yum upgrade
sudo yum install gcc

# Output:
# 'Loaded plugins: fastestmirror'
# 'Loading mirror speeds from cached hostfile'
# 'Package gcc is not available, but is referred to by another package...'
# OR
# 'Installed: gcc.x86_64 0:4.8.5-44.el7'

Here, we’re doing something similar to the APT commands: updating the package list, upgrading the system, and then installing GCC.

Now, you have GCC installed on your Linux system and you’re ready to compile your C programs!

Installing GCC from Source Code

If you need a specific version of GCC or want to customize the build, you might prefer to install GCC from source code. Here’s a basic example of how you might do this:

# Download the source code
wget http://ftp.gnu.org/gnu/gcc/gcc-9.2.0/gcc-9.2.0.tar.gz

# Extract the tarball
tar xf gcc-9.2.0.tar.gz

# Navigate into the directory
cd gcc-9.2.0

# Configure the build
./configure --disable-multilib --enable-languages=c,c++

# Build and install
make -j$(nproc)
sudo make install

# Output:
# 'gcc-9.2.0.tar.gz 100%[===================>] 104.50M  1.94MB/s    in 54s'
# 'gcc-9.2.0/configure: creating ./config.status'
# 'gcc-9.2.0/configure: creating ./config.status'

This will download the GCC 9.2.0 source code, configure the build, and then compile and install GCC. The --disable-multilib flag is used to only support 64-bit software, and --enable-languages=c,c++ is used to only build the C and C++ compilers.

Installing Different Versions of GCC

Different versions of GCC have different features and support for different language standards. You might need a specific version of GCC for a certain codebase. Here’s how you can install different versions of GCC from source and using package managers.

Installing Different Versions from Source

You can replace gcc-9.2.0 in the previous example with the version you need. The GCC mirrors page lists all available versions.

Installing Different Versions with APT

On Debian-based distributions, you can install a specific GCC version with the apt package manager:

sudo apt install gcc-8

# Output:
# 'Reading package lists... Done'
# 'Building dependency tree'
# 'Reading state information... Done'
# 'gcc-8 is already the newest version (4:7.4.0-1ubuntu2.3).'

Installing Different Versions with YUM

On RPM-based distributions, you can use the yum package manager to install a specific GCC version:

sudo yum install gcc48

# Output:
# 'Loaded plugins: fastestmirror'
# 'Loading mirror speeds from cached hostfile'
# 'Package gcc48 is not available, but is referred to by another package...'

Here’s a summary of the key changes in the last few GCC versions:

VersionKey Changes
GCC 7Full C++14 support, initial C++17 support
GCC 8Full C++17 support, initial C++2a support
GCC 9Full C++2a support, new optimizations
GCC 10New features and optimizations, initial C++20 support

Using and Verifying GCC Installation

You can compile a C program with GCC using the following command:

gcc hello.c -o hello

# Output:
# 'hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=5c9f85a8aa9bfa58c86e64e0d10aa8afb4a1c5e8, not stripped'

This will compile the hello.c file into an executable named hello. You can run it with ./hello.

You can verify your GCC installation and version with the following command:

gcc --version

# Output:
# 'gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0'

This will print the GCC version, which can be useful for troubleshooting or verifying that you have the correct version installed.

Exploring Alternatives to GCC

While GCC is a popular choice for compiling C programs, it’s not the only option. There are other compilers and techniques you can use to accomplish the same task. Let’s explore some of these alternatives, their benefits, drawbacks, and situations where they might be a better choice.

Clang: A Modern Compiler

Clang is a compiler front end for the C, C++, and Objective-C programming languages. It uses LLVM as its back end and has been part of the LLVM release cycle since LLVM 2.6.

It’s known for providing expressive diagnostics, a modular library-based architecture, and strong adherence to standards.

You can install Clang on Ubuntu like this:

sudo apt-get install clang

# Output:
# 'Reading package lists... Done'
# 'Building dependency tree'
# 'Reading state information... Done'
# 'clang is already the newest version (1:6.0-41~exp5~ubuntu1).'

And here’s how you compile a program:

clang hello.c -o hello

# Output:
# 'hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=5c9f85a8aa9bfa58c86e64e0d10aa8afb4a1c5e8, not stripped'

Intel C++ Compiler

The Intel C++ Compiler, also known as icc, is a group of C and C++ compilers from Intel. If you’re developing software specifically for Intel hardware, you might find icc more suitable.

TinyCC

If you’re working on a project where binary size matters more than speed, TinyCC (tcc) might be worth considering. As the name suggests, it’s a very lightweight C compiler, but it’s not as fast or fully-featured as GCC.

Using Makefiles

Makefiles can simplify the build process, especially for larger projects. They let you specify how to derive the executable from source files without having to list all the commands every time.

Here’s a basic Makefile for a C program:

# Makefile

hello: hello.c
    gcc -o hello hello.c

# Output:
# 'gcc -o hello hello.c'

You can then compile the program with just make.

Using IDEs

Integrated Development Environments (IDEs) like Eclipse, NetBeans, and CLion can manage the build process for you. They often include features like automatic build configuration, a graphical debugger, and integrated version control.

Remember, the best tool depends on your specific needs. GCC is a great general-purpose compiler, but there are situations where alternatives might be more suitable. By understanding these alternatives, you can make a more informed decision and choose the best tool for your needs.

Navigating Common GCC Errors

While GCC is a powerful tool, you might encounter some errors or obstacles along the way. Here are some common issues and how to resolve them.

‘gcc: Command not found’

If you see this error, it means that GCC is not installed or not available in your PATH.

gcc --version

# Output:
# 'bash: gcc: command not found'

To resolve this, you can install GCC using the methods we discussed earlier. If GCC is installed but not in your PATH, you’ll need to add it. This process varies depending on your shell and system configuration.

‘fatal error: stdio.h: No such file or directory’

This error occurs when GCC can’t find the stdio.h header file, which is part of the standard library. Here’s what the error might look like:

gcc hello.c -o hello

# Output:
# 'hello.c:1:10: fatal error: stdio.h: No such file or directory'
# ' #include '
# '          ^~~~~~~~~'
# 'compilation terminated.'

To fix this, you need to install the standard library development files. On Debian-based distributions, you can do this with sudo apt install libc6-dev. On RPM-based distributions, you can use sudo yum install glibc-devel.

‘undefined reference to `main’

This error occurs when GCC can’t find the main function. In C, every program must have a main function as the entry point.

gcc hello.c -o hello

# Output:
# '/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
# '(.text+0x20): undefined reference to `main'
# 'collect2: error: ld returned 1 exit status'

To resolve this, make sure your program has a main function. If it’s intended to be a library, you should compile it with -c to produce an object file.

Best Practices and Optimization

While GCC will optimize your code by default, you can control the optimization level with the -O option. For example, -O0 (no optimization), -O1 (optimize minimally), -O2 (optimize more), -O3 (optimize even more), -Ofast (optimize very aggressively to the point of breaking standards), and -Og (optimize debugging experience).

GCC also includes features for warning about common mistakes (-Wall) or even turning warnings into errors (-Werror). Using these options can help catch issues early.

Remember to always keep your system and GCC installation updated to the latest version to benefit from the latest features and bug fixes.

Understanding Compilers and Their Role

To fully grasp the importance of the ‘install gcc command linux’, it’s crucial to understand what compilers do and their role in software development.

What is a Compiler?

A compiler is a special program that processes statements written in a particular programming language and turns them into machine language or ‘code’ that a computer’s processor uses. In essence, it translates the human-readable code into a format that your machine can understand and execute.

# Example of a C program
#include <stdio.h>

int main() {
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}

# Output:
# 'Hello, World!'

In this example, the C compiler takes the source code and translates it into machine code. The printf() function is a C library function that sends formatted output to stdout (your screen, by default).

The Role of GCC in Software Development

GCC, the GNU Compiler Collection, includes front ends for C, C++, Objective-C, Fortran, Ada, and Go, as well as libraries for these languages. It’s a key tool in a developer’s arsenal, playing a pivotal role in converting your source code into executable programs.

GCC offers various benefits, including support for multiple languages, optimization, portability, and the ability to work on multiple projects, making it a preferred choice for many developers.

The Importance of Installing GCC in Linux

Linux is a popular choice for developers. It’s open-source, reliable, and supports a wide array of development tools like GCC. Having GCC installed on your Linux system allows you to compile and run your programs right from the terminal, making it an essential tool for Linux-based development.

Installing GCC on Linux is as simple as running a few commands. However, understanding what these commands do and how GCC works can help you troubleshoot issues and optimize your development workflow.

Delving Deeper: GCC in Larger Projects

Understanding how to install and use GCC in Linux is just the start. As you work on larger projects, you’ll find that the GCC command often works in tandem with other commands and functions. Let’s explore some of these related tools and how they can benefit your development process.

Make: Automating Your Build Process

As your projects grow, compiling all your source files with individual commands can become tedious. That’s where Make comes in. Make is a build automation tool that automatically builds executable programs and libraries from source code by reading files called Makefiles.

Here’s a simple Makefile for a C program:

# Makefile

all: hello

hello: hello.c
    gcc -o hello hello.c

clean:
    rm hello

# Output:
# 'gcc -o hello hello.c'

In this Makefile, all is the default target, hello compiles the program, and clean removes the executable.

GDB: Debugging Your Programs

Even the best developers write code with bugs. GDB, the GNU Debugger, can help you debug your programs. It lets you see what is going on ‘inside’ another program while it executes.

Here’s a basic example of using GDB:

gcc -g hello.c -o hello
gdb hello

# Output:
# 'GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git'
# 'Reading symbols from hello...done.'

In this example, we first compile the program with -g to include debugging information. Then, we run gdb hello to start debugging the program.

Valgrind: Checking Memory Leaks

Memory leaks can be a serious issue in C programs. Valgrind is a tool that can help you find memory leaks, among other things.

Here’s a basic example of using Valgrind:

valgrind --leak-check=yes ./hello

# Output:
# '==2092== Memcheck, a memory error detector'
# '==2092== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.'

In this example, we’re running the hello program with Valgrind to check for memory leaks.

Further Resources for Mastering GCC in Linux

To further your understanding of GCC and related tools, you can refer to the following resources:

  1. GCC and Make – A Tutorial on how to compile, link and build C/C++ applications

  2. A Tutorial on How to Compile C Program in Linux

  3. GCC: The Complete Reference

These resources offer in-depth information about GCC and related topics, helping you to expand your knowledge and skills.

Wrapping Up: Mastering the GCC Command in Linux

In this comprehensive guide, we’ve navigated the process of installing and using the GCC command in Linux, a key tool for compiling C programs.

We began with the basics, learning how to install GCC using package managers like APT for Debian-based distributions and YUM for RPM-based distributions. We then delved into more advanced territory, exploring how to install GCC from source code, how to install different versions of GCC, and how to verify your GCC installation.

Along the way, we tackled common issues you might encounter when using GCC, such as ‘command not found’, ‘no such file or directory’, and ‘undefined reference to main’, providing you with solutions for each issue. We also looked at alternative approaches to compiling C programs, exploring compilers like Clang, Intel C++ Compiler, and TinyCC, and techniques like using Makefiles and IDEs.

Here’s a quick comparison of these methods:

MethodProsCons
GCCRobust, supports many languagesMay require troubleshooting for some programs
ClangModern, expressive diagnosticsLess robust than GCC
Intel C++ CompilerOptimized for Intel hardwareProprietary, not open source
TinyCCLightweight, small binary sizeLess fully-featured than GCC

Whether you’re just starting out with GCC or you’re looking to level up your C programming skills, we hope this guide has given you a deeper understanding of GCC and its capabilities.

With its balance of power, flexibility, and broad language support, GCC is a vital tool for any Linux-based developer. Happy coding!