A Makefile is a special file used by the make build automation tool to manage the build process of a project. It defines a set of tasks to be executed, typically to compile and link a program. Here are some key functions of a Makefile:
-
Compilation Instructions: It specifies how to compile and link the program. This includes defining the source files, the compiler to use, and any necessary flags or options.
-
Dependencies: Makefiles list dependencies between files, ensuring that changes in source files trigger recompilation of only the necessary parts of the program.
-
Automation: It automates repetitive tasks, such as cleaning up build artifacts, running tests, or deploying software.
-
Targets and Rules: A Makefile consists of targets, dependencies, and rules. A target is usually a file to be generated, dependencies are files that the target depends on, and rules are the commands to create the target from the dependencies.
-
Variables: Makefiles can use variables to simplify and manage complex build processes, making it easier to maintain and modify.
Example
# Compiler
CXX = g++
# Compiler flags
CXXFLAGS = -Wall -g
# Target executable
TARGET = myprogram
# Source files
SRCS = main.cpp utils.cpp
# Object files
OBJS = $(SRCS:.cpp=.o)
# Default target
all: $(TARGET)
# Rule to build the target executable
$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJS)
# Rule to build object files
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
# Clean up build artifacts
clean:
rm -f $(OBJS) $(TARGET)
# Phony targets
.PHONY: all cleanExplanation:
- CXX and CXXFLAGS: These variables define the compiler and the flags used during compilation.
- TARGET: The name of the final executable.
- SRCS and OBJS: Lists of source and object files. The
OBJSvariable is automatically generated by replacing.cppwith.oin theSRCSlist. - all: The default target that builds the executable.
- $(TARGET): This rule specifies how to link object files into the final executable.
- %.o: %.cpp: A pattern rule to compile each
.cppfile into a.oobject file. - clean: A target to remove all object files and the executable, useful for cleaning up the build directory.
- .PHONY: Declares
allandcleanas phony targets, meaning they are not actual files but just names for commands to run.
Running it
If you run this Makefile using the make command in a terminal, here’s what would happen:
-
Compilation: The
maketool will look for a file namedMakefilein the current directory. It will then execute the default target, which isallin this case. -
Building the Executable:
makewill check if the target executablemyprogramneeds to be built. It does this by comparing the timestamps of the source files (main.cpp,utils.cpp) and the corresponding object files (main.o,utils.o).- If any of the source files are newer than their corresponding object files, or if the object files do not exist,
makewill compile the source files into object files using the rule%.o: %.cpp. - Once the object files are up-to-date,
makewill link them together to create themyprogramexecutable using the rule for$(TARGET).
-
Output: During this process, you’ll see the compilation and linking commands being executed in the terminal. If there are any errors in the source code, the compiler will output error messages.
-
Clean Up: If you run
make clean, it will execute thecleantarget, which removes the object files and the executable, cleaning up the build directory.