Introduction

The compilation of programs in programming language C is a fundamental process in software development. It allows the source code written by the programmers to be translated into computer-friendly instructions. One of the challenges associated with the compilation is the efficient management of the project files and units, which becomes even more crucial when working on large and complex projects.

This work aims to explore one of the most used mechanisms to compile projects in C: the use of a Makefile file. A Makefile is a configuration file that describes how to compile the source files of a project and how to manage the units between them. In addition, it allows to automate the compilation process and facilitates the task of maintaining and updating the code.

Throughout this work, we will examine in detail the process of compiling a program in C using a Makefile. We will start by understanding the structure of a Makefile and how the compilation flow is defined. Then we will explore the advantages of its use, such as automatic detection of changes in the source files and selective collection. We will also address best practices to create effective and easy-to-maintain Makefilds.

In addition, we will analyze a practical example of a C program compilation, detailing the steps needed to create a Makefile and compile the project. We will also discuss the installation of the resulting program in a personal directory, avoiding interference with the package system.

Ultimately, we will learn how to clean up the program and understand the importance of proper software management in Unix / Linux systems.

As we progress in this work, we will gain a deeper understanding of the C-compilation and the usefulness of a Makefile as an essential tool in software development.

Compilation of a C-program using a Makefile

The first thing we’re going to install is fundamental for the compilation of C packages in Debian.

The building-essential package provides the necessary compilation tools, and dpkg-dev facilitates package management and source code delivery. You can install other specific development packages according to your needs, but these two are the most essential to start compiling C software in Debian.

Now let’s download the source files from the package that we want to compile in my case I will make it samba:

Once this is done we will see in our directory that a series of files and directories have been created:

    • samba-4.17.12 + dfsg: * * This is the directory that contains the Samba source code, which was downloaded from Debian repositories. It is the source that will be used to compile and install Samba in your system.
    • samba\ _ 4.17.12 + dfsg-0 + deb12u1.debian.tar.xz: * * This compressed file contains the specific Debian modifications for the Samba package. You can include patches, construction scripts and other files related to package construction.
    • samba\ _ 4.17.12 + dfsg-0 + deb12u1.dsc: * * This file is the Debian Source Control. It contains information about the source and the versions of the package, as well as links to other files related to the source.
    • samba\ _ 4.17.12 + dfsg.orig.tar.xz: * * This compressed file is the original source of Samba as published on the official Samba website. It is used as a basis for building the Debian package.

Let’s get in the main directory and we can see the source code of the package:

The first thing we will do is install the units of the package that we want to compile in my case samba:

Now let’s launch the script configure. This script is used to configure samba compilation according to your needs and system characteristics:

Now let’s compile the source code using the make command. This will be in charge of compiling the code and creating the executables and libraries that the script has created configure:

This will take a while depending on the size of the package in my case, it took me 8 minutes:

Once the code is compiled to install it we will use make install:

Once this is done we will have our package installed on the route / usr / local:

Now to start the samba service we will run the nmbd file and make sure it is working:

We see that the installed version is the one we have compiled:

Uninstall the compiled package

To remove the package we have compiled:

We see that it only leaves us the service configuration files:

They may remain in our samba trace system as the makefile file is not configured in this case to completely remove it, run whereis and remove the content of the routes:

Once this has been done, we will have removed the samba trail: