Dev Environment
Linux kernel module development requires a properly configured build environment. This section guides you through installing kernel headers, setting up the build configuration, and preparing for cross-compilation scenarios.
Installing Kernel Headers¶
Kernel headers provide the necessary system call definitions and architecture-specific details for module compilation. Install headers matching your running kernel version:
Debian/Ubuntu:
Red Hat/CentOS:
sudo yum install kernel-devel-$(uname -r)
# or for newer versions:
sudo dnf install kernel-devel-$(uname -r)
Verify installation:
arch, include, and kernel.
Configuring the Build Environment¶
The kernel configuration (/.config) dictates build options. If you're compiling for the host kernel:
-
Generate initial config:
This interactive tool lets you enable/disable features like
CONFIG_MODULESand architecture-specific options. -
Alternative: Use existing config
This preserves existing settings while adding new options from the current kernel source.
If you have a.configfile from a previous build, use:
Setting Up Cross-Compilation Tools¶
For cross-compilation (e.g., building modules for an ARM device on x86), install the target-specific toolchain:
Example: ARM target
Set environment variables to specify the cross-compiler prefix:
Verify toolchain:
Verifying the Setup¶
Ensure all components are correctly installed:
1. Check headers:
module.h and kernel.h.
-
Validate compiler:
-
Test module build:
This builds the module using the current kernel's build system.
Create a simple module (e.g.,hello.c) and attempt compilation:
Key takeaways¶
- Install kernel headers matching your running kernel version.
- Use
make menuconfigormake oldconfigto configure the build. - Install target-specific toolchains for cross-compilation and set
CROSS_COMPILE. - Verify headers, compiler, and build process with targeted commands.
- Always ensure the kernel source tree and headers are aligned with your target kernel.