Introduction
In this lab, you will learn how to use the GCC (GNU Compiler Collection) compiler to compile and optimize C programs on a Linux system. You will start by understanding the basics of the GCC compiler, including its common command-line options. Then, you will compile a simple C program and explore the GCC optimization flags to improve the performance of your code.
The lab covers the following steps:
- Understand the Basics of GCC Compiler
- Compile a Simple C Program with GCC
- Explore GCC Compiler Optimization Flags
The GCC compiler is a crucial tool for building C and C++ programs on Linux, and this lab will provide you with practical examples to help you become more proficient in using it.
Understand the Basics of GCC Compiler
In this step, you will learn the basics of the GCC (GNU Compiler Collection) compiler, which is a widely used compiler for C, C++, and other programming languages on Linux systems.
First, let's check the version of GCC installed in the environment:
gcc --version
Example output:
gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.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.
The GCC compiler is a crucial tool for compiling and building C and C++ programs on Linux. It provides various options and flags to control the compilation process, optimize the generated code, and handle different aspects of the build process.
Some of the common GCC command-line options include:
-c: Compile and assemble, but do not link-o <output>: Specify the output file name-g: Generate debugging information-Wall: Enable all warning messages-Werror: Treat all warnings as errors-O<n>: Optimization level, wherenis 0, 1, 2, 3, ors
To understand these options better, we'll explore them in the following steps.
Compile a Simple C Program with GCC
In this step, you will learn how to compile a simple C program using the GCC compiler.
First, let's create a new directory for our project and navigate to it:
mkdir ~/project/simple-c-program
cd ~/project/simple-c-program
Now, create a new file named hello.c with the following content:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
This is a simple C program that prints "Hello, World!" to the console.
To compile this program using GCC, run the following command:
gcc -o hello hello.c
This command will compile the hello.c file and generate an executable file named hello.
You can now run the compiled program:
./hello
Example output:
Hello, World!
The gcc command used above has the following options:
-o hello: Specifies the output file name ashellohello.c: The input C source file to be compiled
GCC provides many other options to control the compilation process, which we will explore in the next step.
Explore GCC Compiler Optimization Flags
In this step, you will learn about the different optimization flags available in the GCC compiler and how they can be used to improve the performance of your C programs.
Let's start by compiling the hello.c program we created in the previous step with different optimization levels:
## Compile with no optimization
gcc -O0 -o hello_no_opt hello.c
./hello_no_opt
Example output:
Hello, World!
## Compile with optimization level 1 (default)
gcc -O1 -o hello_opt1 hello.c
./hello_opt1
Example output:
Hello, World!
## Compile with optimization level 2
gcc -O2 -o hello_opt2 hello.c
./hello_opt2
Example output:
Hello, World!
## Compile with optimization level 3 (aggressive)
gcc -O3 -o hello_opt3 hello.c
./hello_opt3
Example output:
Hello, World!
The optimization levels in GCC are:
-O0: No optimization (default when no optimization level is specified)-O1: Moderate optimization, a good balance between compilation time and performance-O2: Aggressive optimization, focuses on performance-O3: Even more aggressive optimization, may result in larger code size and longer compilation time
In addition to the general optimization levels, GCC also provides more specific optimization flags:
-Os: Optimize for size, rather than for speed-Ofast: Enable aggressive optimizations that may violate strict standards compliance
It's important to note that while higher optimization levels can improve performance, they may also introduce unexpected behavior or bugs in your code. It's generally a good practice to start with a lower optimization level, such as -O1, and gradually increase the optimization level while testing your program thoroughly.
Summary
In this lab, you first learned the basics of the GCC (GNU Compiler Collection) compiler, including how to check the installed version and the common command-line options such as -c, -o, -g, -Wall, -Werror, and -O<n>. You then practiced compiling a simple C program using the gcc command, learning how to create a source file, compile it, and run the resulting executable. Finally, you explored GCC's optimization flags, which allow you to control the level of code optimization during the compilation process.



