5.6.2 - Makefile to Build the Module
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Interactive Audio Lesson
Listen to a student-teacher conversation explaining the topic in a relatable way.
Introduction to Makefiles
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Today, we're learning about Makefiles, which are essential for building kernel modules. Can anyone tell me what a Makefile is?
Is it like a list of commands that helps compile the code?
Exactly! It simplifies the compilation process. In our case, it will automate building our kernel module. What do we typically include in a Makefile?
We declare object files, right?
Good point! We use lines such as `obj-m += example.o` to declare which files will be compiled as modules. Can anyone recall what `obj-m` signifies?
It means that the files are object files for a module.
Correct! Now, let's recap the importance of Makefiles. What is one major advantage they provide in kernel development?
They automate the building process!
Absolutely! Great job, everyone.
Commands in the Makefile
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Let's break down the commands in our Makefile. Who can explain the command we use for the 'all' target?
It uses `make -C /lib/modules/...` to compile the module.
Correct! This command tells the system to compile our module in accordance with the current kernel version. What does the `$(shell uname -r)` part do?
It retrieves the kernel version we're currently using, right?
Exactly! This ensures compatibility. Now, who remembers the purpose of the 'clean' target?
It cleans the build directory by removing unnecessary files.
That's right! This keeps our environment organized. Why is that important in module development?
To avoid confusion and errors from leftover files!
Perfectly stated! Understanding these commands is vital for effective kernel programming.
Steps to Compile the Module
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Now let's go through the steps to compile our kernel module. Who can tell me the first step?
We need to save our kernel module code in a file like `example.c`.
Correct! What's next?
We create the Makefile with the proper structure.
That's right! Finally, can anyone tell me what command we run to start the building process?
We run `make` in the terminal!
Excellent! Once we've done that, what file do we receive?
.ko file, which we can load into the kernel.
Exactly! Understanding these steps makes kernel module development systematic. Great collaboration today!
Introduction & Overview
Read summaries of the section's main ideas at different levels of detail.
Quick Overview
Standard
In this section, we cover the structure of a Makefile used to compile a kernel module, with specific examples. You'll learn the commands to utilize during the build process and the significance of each part of the Makefile.
Detailed
Makefile to Build the Module
This section focuses on how to create a Makefile to facilitate the compilation of Linux kernel modules. A Makefile is a special file that contains a set of instructions used by the make utility to automate the building process. Here's how you can build a kernel module using a Makefile:
Structure of a Makefile
The provided Makefile consists of two main sections:
1. Object files declaration: This section declares which object files should be built. The format is straightforward: obj-m += example.o specifies that example.o (the object file corresponding to your module source code) needs to be compiled.
- Targets: The
allandcleantargets use themakecommand: - all: This target compiles the module. It invokes
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules, which tells the kernel build system to compile the modules defined in the current directory, ensuring compatibility with the current kernel version. - clean: This target cleans up by removing build files using
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean, ensuring your development environment is tidy.
Steps to Build the Module
- Save your kernel module source code (e.g.,
example.c). - Create a Makefile with the content described above.
- In your terminal, navigate to the directory containing your module and Makefile, and run
make. This will produce a compiled.ko(kernel object) file ready for loading into the kernel.
Understanding this process is crucial for kernel module development, allowing you to manage compilation efficiently and integrate with the Linux kernel seamlessly.
Youtube Videos
Audio Book
Dive deep into the subject with an immersive audiobook experience.
Makefile Structure
Chapter 1 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
obj-m += example.o
This line adds the example.o object file to the list of modules to be built.
Detailed Explanation
The Makefile starts with the line obj-m += example.o. This line indicates that example.o is an object file that needs to be compiled into a kernel module. The obj-m prefix specifies that it's a module object file, and the += operator appends example.o to the list of module files that will be built. Essentially, this tells the build system what we want to compile into a module.
Examples & Analogies
Think of the Makefile as a recipe for baking cookies. The example.o is an ingredient in that recipe. Just as a recipe lists the ingredients needed to make your cookies, the Makefile lists the object files required to build the module.
Build Target
Chapter 2 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
Detailed Explanation
The all: line defines a build target named 'all'. Under this target, the command tells the build system to compile the kernel module using the make command. The -C /lib/modules/$(shell uname -r)/build part navigates into the kernel source directory for the currently running kernel version, which is obtained by running uname -r. The M=$(PWD) specifies the module's source code directory as the current working directory (PWD). Finally, modules tells it to build the modules specified in the Makefile.
Examples & Analogies
Imagine you're participating in a community bake sale. You need to fetch the ingredients from the pantry (the kernel source) and put together your cookies (the module) in your kitchen (the current working directory). By invoking an all-encompassing command, you're assembling everything you need in one go.
Clean Target
Chapter 3 of 3
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Detailed Explanation
The clean: line creates another build target that handles cleanup. When invoked, this target executes a command to clean up the build artifacts, which typically includes removing the compiled object files and any other generated files. Just like the all: target, it uses the make -C command to go to the kernel source directory. The clean command tells it to remove the files created during the build process.
Examples & Analogies
Cleaning up after a bake sale is important! Just as you would tidy up your kitchen by putting away ingredients and washing utensils after baking cookies, the clean target ensures that your module building area is free of any leftover files that were created during the compilation process.
Key Concepts
-
Makefile: A special file used to automate the build process of software, including kernel modules.
-
obj-m: A directive in a Makefile indicating object files are meant to be compiled as modules.
-
Kernel Object file: The resultant file from the compilation process that can be loaded into the kernel.
-
make command: A utility that reads the Makefile and compiles the module accordingly.
-
Target: Sections in a Makefile, such as 'all' and 'clean', that define specific actions.
Examples & Applications
The obj-m += example.o line indicates that the module example is to be treated as a loadable module.
A complete Makefile typically includes both all and clean targets for module compilation and cleanup.
Memory Aids
Interactive tools to help you remember key concepts
Rhymes
To build a module with grace, a Makefile sets the place. Compile and clean, it's such a dream, in a kernel module's space.
Stories
Imagine a chef (the Makefile) directing their kitchen (the kernel) to prepare dishes (kernel modules). The chef gives instructions to gather ingredients (source files) and create delicious meals (compiled modules) without chaos by cleaning afterward.
Memory Tools
Remember 'M O K' for Makefile: M - Module directive (obj-m), O - Output file (.ko), K - Kernel context.
Acronyms
M.A.K.E - Modules As Kernel Extensions.
Flash Cards
Glossary
- Makefile
A file containing definitions and instructions for building software, commonly used to automate the compilation process.
- objm
A directive in a Makefile indicating that the specific object files are to be built as loadable modules.
- Kernel Object (.ko) file
The compiled output of a kernel module that can be loaded into the Linux kernel.
- make command
A utility that automates the compilation of software and executes instructions defined in a Makefile.
- Target
A label in a Makefile indicating a specific action to be executed by the
makecommand.
Reference links
Supplementary resources to enhance your learning experience.