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:
- The latest Ubuntu server installed;
- An account with administrator privileges.
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.