In this lab, we will be utilizing make
/ makefiles
to get ourselves comfortable with compiling files for large software.
The GCC (GNU Compiler Collection) will be used for building as it is the standard compiler for Unix-like operating systems. The GCC compiler refers specifically to the compiler that compiles C programs.
Please note that I will be executing instructions in x86-001
and aarch64-002
servers, as they are the working environments of this SPO600 course.
I will be following the lab instructions step-by-step, which will be easy to follow.
📌 0. Login to Servers
First, we will login to both x86-001
and aarch64-002
servers. Please note that I have set up my account and passphrase before starting this lab.
ssh user-name@aarch64-002.spo600.cdot.systems
ssh user-name@x86-001.spo600.cdot.systems
📌 1. Obtain the Source Code
We will be cloning the source code from the official Git repository. Please refer to gcc.gnu.org.
On my servers, I will clone it to a new directory ~/git/gcc
. This makes sure that the gcc
source code is stored in a separate directory. The whole process could take around 3-6 minutes.
git clone git://gcc.gnu.org/git/gcc.git ~/git/gcc
📌 2. Configure Build
We will have to configure the build before installing it.
👉 Make a new build directory:
It is recommended to create a new separate directory for your build. From this build directory, we will invoke the configure script, which is in the root of the gcc
source repository.
mkdir ~/gcc-build-001
cd ~/gcc-build-001
👉 Run the configure script
We will run the configure script using the --prefix
option. This option makes sure to set our custom installation directory: $HOME/gcc-test-001
.'
~/git/gcc/configure --prefix=$HOME/gcc-test-001
After running the script, you will see that there are new files in the ~/gcc-build-001
, including a Makefile
.
[user-name@aarch64-002 gcc-build-001]$ ls
config.log config.status Makefile
📌 3. Build GCC Compiler
Now that we have confirmed there is a Makefile in the directory, we will run the build command.
👉 screen
Utility
However, it is important to note that the build can take from 20 minutes to several hours. We will use the screen
utility before running any command to make sure the operation is not forced to start over even if we disconnect from our system or there is a break in the internet connection.
screen -RaD
After this, we will run our build command below. Ctrl A + D will be pressed to disconnect while our build is running so we can safely exit.
We can check on progress by running the screen
command again.
👉 Record Build Time
The built-in time
command can be used to record the time taken. We will also use the tee
command to redirect our stdin
and stdout
to a log file (build.log
) and the screen.
With all of these in mind, the final command to build becomes:
time make -j 24 |& tee build.log
📌 3. Install GCC Compiler
Next, we will install the compiler using this command.
time make install |& tee install.log
At the end of the process, you will see this text, which records the time taken for the process to finish. In my case, this is what it shows to me:
Validating My GCC Build
Now that we have successfully built and installed our gcc
compiler. We will check 3 things:
✅ 1. It is different from the system's C compiler
Check the system's GCC version
which gcc
gcc --version
Check your newly installed GCC version
$HOME/gcc-test-001/bin/gcc --version
As you can see, both the paths and *versions * are different!
✅ 2. It is the current development version
From the command above you can see that the version number is higher than the Red Hat version. This is true as we got our compiler from the git repo.
✅ 3. It can successfully build a C program
- In your root directory, create a
hello.c
file with this code inside.
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
- Compile the program using your new GCC
$HOME/gcc-test-001/bin/gcc hello.c -o hello
- Run the compiled program
./hello
📌 4. Update Timestamp on passes.cc
Locate the file passes.c
which is located in the gcc
subdirectory of your source tree.
cd ~/git/gcc/gcc
Update the timestamp by running this command
touch passes.cc
📌 5. Rebuild
We will now go to our build directory to rebuild the compiler after changing passes.c
.
Return to build directory
cd ~/gcc-build-001
Rebuild using make
time make |& tee rebuild.log
We can see that the build time is significantly less than the initial full build since only passes.cc
has a changed timestamp.
📌 6. Perform a Null Rebuild
Without making any additional changes, run make
again. This is called a null rebuild.
time make |& tee null_rebuild.log
This null rebuild completed almost immediately, as make
determined no source files require rebuilding.
Conclusion
After completing this lab, I have gotten useful insights about building and installing a complex codebase like GCC. Obtaining the latest development source and configuring a build environment is actually very useful in helping me understand how GCC compilers work. Not only that, I learnt the process of incremental builds where subsequent rebuilds do not take as much time as the initial build, making make
very efficient.
Thank you for reading and see you next time! 😄
Top comments (0)