Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Setting Up the Kernel Build Environment | Writing Your First Character Driver
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C Device Drivers Basics

bookSetting Up the Kernel Build Environment

Before you can begin writing and testing your own character device drivers, you must set up a suitable kernel build environment. Building kernel modules differs from compiling user-space programs: you interact directly with the kernel’s build system and require a set of specific files to ensure compatibility and successful compilation. The essential files you need are:

  • The kernel source code or at least its headers, matching the running kernel version;
  • A Makefile that instructs the kernel build system how to build your module;
  • Your kernel module source file, usually with a .c extension.

These files form the foundation for building and testing any kernel module.

Makefile.

Makefile.

copy
1234567
obj-m += simple_module.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

The Makefile shown above is a minimal example tailored for building a kernel module. It uses the obj-m variable to specify the object files to be built as modules. When you run make, it invokes the kernel’s own build system by calling make -C /lib/modules/$(shell uname -r)/build, passing the current directory as the module source location with M=$(PWD). This approach ensures your module is built against the correct kernel version and configuration, leveraging all the compatibility and dependency management provided by the kernel’s infrastructure.

simple_module.c

simple_module.c

copy
1234567891011121314151617181920
#include <linux/module.h> #include <linux/kernel.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple minimal kernel module"); static int __init simple_init(void) { printk(KERN_INFO "Simple module loaded\n"); return 0; } static void __exit simple_exit(void) { printk(KERN_INFO "Simple module unloaded\n"); } module_init(simple_init); module_exit(simple_exit);
question mark

What is the purpose of the obj-m variable in a kernel module Makefile?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookSetting Up the Kernel Build Environment

Swipe to show menu

Before you can begin writing and testing your own character device drivers, you must set up a suitable kernel build environment. Building kernel modules differs from compiling user-space programs: you interact directly with the kernel’s build system and require a set of specific files to ensure compatibility and successful compilation. The essential files you need are:

  • The kernel source code or at least its headers, matching the running kernel version;
  • A Makefile that instructs the kernel build system how to build your module;
  • Your kernel module source file, usually with a .c extension.

These files form the foundation for building and testing any kernel module.

Makefile.

Makefile.

copy
1234567
obj-m += simple_module.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

The Makefile shown above is a minimal example tailored for building a kernel module. It uses the obj-m variable to specify the object files to be built as modules. When you run make, it invokes the kernel’s own build system by calling make -C /lib/modules/$(shell uname -r)/build, passing the current directory as the module source location with M=$(PWD). This approach ensures your module is built against the correct kernel version and configuration, leveraging all the compatibility and dependency management provided by the kernel’s infrastructure.

simple_module.c

simple_module.c

copy
1234567891011121314151617181920
#include <linux/module.h> #include <linux/kernel.h> MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("A simple minimal kernel module"); static int __init simple_init(void) { printk(KERN_INFO "Simple module loaded\n"); return 0; } static void __exit simple_exit(void) { printk(KERN_INFO "Simple module unloaded\n"); } module_init(simple_init); module_exit(simple_exit);
question mark

What is the purpose of the obj-m variable in a kernel module Makefile?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt