How to Install GCC on Ubuntu 22.04 [and Compile a C Program]

November 21st, 2023
How to Install GCC on Ubuntu 22.04 [and Compile a C Program]

Software development plays an important role in the economic landscape. At the core of every software lies a crucial element – the compiler. Enter the GCC (GNU Compiler Collection), or GCC for short. In this article, we'll show you how to install GCC on Ubuntu 22.04 and compile a C program.

What is GCC?

GCC is a widely used compiler for various programming languages - a compiler is a software tool that translates the code you write for computer programs, commonly known as the "source code," into instructions a computer can understand. This translation process is known as compilation.

As an important part of free and open-source software development, the GCC (GNU Compiler Collection) was developed by the Free Software Foundation (FSF) through a collaborative effort of developers led by Richard Stallman.

How to use GCC?

GCC is widely used in creating software for Unix-like systems, and it has been adapted for various platforms, demonstrating its adaptability and flexibility in the realm of open-source programming.

The GCC (GNU Compiler Collection) supports various programming languages for compilation. Although initially developed in C, GCC can now compile the following prominent languages, such as C++, Fortran, Ada, and Go.

Prerequisites

To follow along this tutorial and install GCC on Ubuntu 22.04, you will need:

1. How to install GCC on Ubuntu 22.04

In the first part of this tutorial, we will show you how to install GCC on Ubuntu in three steps, and in the second part, how to compile a C program.

We will see the different methods of installing GCC and compile a C program, exploring various options.

Step 1: Update Ubuntu package list

To update the package list, use the following command:

sudo apt update

Step 2: Install GCC on Ubuntu

We now install GCC with the following command.

sudo apt install gcc

If GCC is already installed on your system, the command will list the version installed.

You can install GCC with the build-essential package. This will install GCC as well as other popular packages such as make, which is often used with GCC to automate the compilation process of bigger software.

To install build-essentials, use the following command:

sudo apt install build-essential

Step 3: Test GCC installation

Let's now check if GCC has been installed on Ubuntu:

gcc --version

This should return the GCC version and license as follows:

gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 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.

2. How to compile a C program

Now that we’ve installed GCC on Ubuntu and everything is correctly set up, we can continue to compile a C program.

Step 1: Write a basic C program

Open your favorite code editor and write in the following C source code:

#include <stdio.h>
int main() {
   printf("Welcome to Cherry Servers\n");
   return 0;
}

Save the file as example1.c

Step 2: Compile example1.c using GCC

Now that we have our source code, we can compile it with GCC using the following command:

gcc example1.c

By default, GCC will output the compiled source code in a file called a.out

Let's execute it and check the results:

didier@lab:~$ ./a.out
Welcome to Cherry Servers

You can also specify the name of the output with the switch -o followed by the desired name:

gcc example1.c -o example1

Step 3: Generate an assembly listing of the C program

You can also use GCC to generate an assembly listing of the C source code. This can be done using the -S switch:

 gcc -S example1.c

This will generate a file called example1.s. You can then display its content using:

cat example1.s

The output should be similar to the following code:

        .file   "example1.c"
        .text
        .section        .rodata
.LC0:
        .string "Welcome to Cherry Servers"
        .text
        .globl  main
        .type   main, @function
main:
.LFB0:
        .cfi_startproc
        endbr64
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        leaq    .LC0(%rip), %rax
        movq    %rax, %rdi
        call    puts@PLT
        movl    $0, %eax
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   main, .-main
        .ident  "GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
        .section        .note.GNU-stack,"",@progbits
        .section        .note.gnu.property,"a"
        .align 8
        .long   1f - 0f
        .long   4f - 1f
        .long   5
0:
        .string "GNU"
1:
        .align 8
        .long   0xc0000002
        .long   3f - 2f
2:
        .long   0x3
3:
        .align 8
4:

Step 4: Generate debug information

Another useful option of GCC is to generate debug information and store it in the compiled program. While this option is very useful to debug programs, it should not be used in the final released version as it makes the program bigger.

To generate debug information that can be used by gdb - the GNU C debugger, use the following switch -ggdb as follows:

gcc example1.c -o example1 -ggdb

We can now access the debug information using gdb:

gdb example1

Let's now enter a breakpoint at the start of the program:

break main

We can now start the debugging with the run command:

run

This will debug the program and display the debugging information generated by GCC:

Starting program: /home/didier/example1
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, main () at example1.c:3
3          printf("Welcome to Cherry Servers\n");
(gdb)

You can then exit gdb by typing the exit command in the gdb shell.

Conclusion

This tutorial covered what a compiler is and how GCC plays a big part in software development. We learned how to install GCC on Ubuntu 22.04, compile a C program, generate an assembly list, and debug information.

After 36 years, GCC is still maintained and is one of the most used software in the world, from home users to big corporations. You can learn more about GCC by accessing its manual page: man gcc or reading the various online manuals.

With over 20 years in IT, Didier has been creating technical documentation for companies, catering to both technical and non-technical audiences. Didier is an expert in Linux system administration, DevOps, cloud computing, cybersecurity, IT consulting, management consulting, technical writing, Diataxis framework, Doc-as-Code, UX Writing, Jamstack, MkDocs, Docker, containers, open-source, SDLC, and Python programming. His hands-on technical expertise, coupled with his strong communication skills, enables him to bridge the gap between developers and end-users. Didier creates user guides, API References, end-user documentation, how-tos, and tutorials. He is an expert in authoring using modern technologies such as Markdown, Mermaid, and static-site generators. Didier also utilizes frameworks and methodologies such as Diaxiatis and Doc-as-code, applying structured writing techniques. Currently, Didier works as a freelance technical writer and documentation consultant, assisting organizations in creating comprehensive and easy-to-understand documentation for their software and web applications. In his previous roles, Didier worked as a system and applications engineer and implemented style guides, tone and voice best practices, and documentation processes that streamline their release cycles. Didier resides in Vacoas, Mauritius.

Start Building Now

Deploy your new Cloud VPS server in 5 minutes starting from $5.83 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 8e62bac5.755