Makefile
Compiling and linking are two critical steps in the process of turning human-readable source code into an executable program. Here's an overview of what each step involves:
1. Compilation
Compilation is the process of transforming source code (written in languages like C, C++, etc.) into machine code or an intermediate format like object code. The compiler reads your source code and checks it for errors, syntax correctness, and other issues. If everything is correct, it translates the code into machine-level instructions that the computer's CPU can understand.
- The input is source code (e.g.,
.c
,.cpp
files). - The output is object files (e.g.,
.o
or.obj
files), which contain machine code but are not yet complete programs.
Example:
For a C program with the following source code file main.c
:
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
The compiler (gcc
, clang
, etc.) will transform main.c
into an object file (main.o
), which contains compiled code but isn't yet executable.
Key Points:
- Compilers translate high-level code into lower-level object code.
- Object files are incomplete and need linking to be fully functional
2. Linking
Linking is the process that combines object files and libraries into a complete, runnable program. This step is necessary because most programs are made up of multiple parts, often spread across several files. Additionally, programs rely on external code from libraries (like the C standard library for functions like printf
).
There are two types of linking:
- Static linking: The linker copies the required code from libraries into the executable itself.
- Dynamic linking: The program is linked to shared libraries (e.g.,
.so
or.dll
files) that are loaded at runtime. This reduces the executable size but requires the shared libraries to be available when the program runs.
The output of the linking step is an executable file (e.g., a.out
on Unix-based systems or my_program.exe
on Windows).
Example:
Suppose main.o
needs to be linked with a standard library like libc
to resolve the printf
function. The linker combines main.o
with the necessary libraries, producing an executable my_program
.
Key Points:
- Linkers resolve references to external symbols (e.g., functions or variables) that are not defined within the same object file.
- The linker combines the necessary pieces to create a final executable program.
Summary of the Build Process
- Compilation: Converts source files into object files (machine code).
- Example:
main.c
→main.o
- Example:
- Linking: Combines object files and libraries to create an executable.
- Example:
main.o + standard library
→my_program
- Example:
In a project with multiple source files, like main.c
, foo.c
, and bar.c
, each source file would first be compiled into separate object files (main.o
, foo.o
, and bar.o
), and then the linker would combine them into the final executable.
Why do Makefiles exist?
Makefiles automate this process by managing the compilation and linking of multiple files in large projects.
A Makefile is a special file used by the make
utility to automate the build process of software projects. It defines a set of rules that dictate how to compile and link programs. When working on a project with multiple source files and dependencies, a Makefile can greatly simplify the build process by specifying:
-
Targets: These are typically files that you want to generate, like executables or object files. A target can also represent a task, like running tests or cleaning up compiled files.
-
Dependencies: Files or targets that must be up-to-date before a target can be built. For example, a
.c
file depends on a corresponding.h
header file. -
Commands: Shell commands to be executed to build the target. These typically include commands like
gcc
for compiling org++
for linking.
Here's an example dependency graph that you might build with Make. If any file's dependencies changes, then the file will get recompiled:
Interpreted languages like Python, Ruby, and raw Javascript don't require an analogue to Makefiles. The goal of Makefiles is to compile whatever files need to be compiled, based on what files have changed. But when files in interpreted languages change, nothing needs to get recompiled. When the program runs, the most recent version of the file is used. However, it can automate tasks in other environments, like managing dependencies in a Python or JavaScript project.
Make file Basic Structure:
target: dependencies
command
Example Make file for Simple C++ project
# Compiler and flags
CC = gcc
CFLAGS = -Wall -g
# Target program
TARGET = my_program
# Source files
SRCS = main.c foo.c bar.c
# Object files (generated from source files)
OBJS = $(SRCS:.c=.o)
# Default target
all: $(TARGET)
# Rule to build the target program
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS)
# Rule to build object files from source files
%.o: %.c
$(CC) $(CFLAGS) -c $<
# Clean rule to remove generated files
clean:
rm -f $(OBJS) $(TARGET)
Key Points:
$(CC)
and$(CFLAGS)
: Variables for the compiler and flags.$<
and$@
: Special automatic variables where$<
refers to the first dependency, and$@
refers to the target.clean
target: Commonly used to delete object files and executables after building.
Why Use Makefiles?
- Efficiency: Only recompiles files that have changed.
- Automation: Simplifies repetitive tasks like compilation, linking, or testing.
- Portability: Works across different systems without needing to write custom build scripts.