Skip to content

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:

sudo apt-get install linux-headers-$(uname -r)

Red Hat/CentOS:

sudo yum install kernel-devel-$(uname -r)
# or for newer versions:
sudo dnf install kernel-devel-$(uname -r)

Verify installation:

ls /usr/src/linux-headers-$(uname -r)/
This should show directories like arch, include, and kernel.


Configuring the Build Environment

The kernel configuration (/.config) dictates build options. If you're compiling for the host kernel:

  1. Generate initial config:

    make menuconfig
    
    This interactive tool lets you enable/disable features like CONFIG_MODULES and architecture-specific options.

  2. Alternative: Use existing config
    If you have a .config file from a previous build, use:

    make oldconfig
    
    This preserves existing settings while adding new options from the current kernel source.


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

sudo apt-get install gcc-arm-linux-gnueabi

Set environment variables to specify the cross-compiler prefix:

export CROSS_COMPILE=arm-linux-gnueabi-

Verify toolchain:

$CROSS_COMPILE-gcc --version
This should display the ARM compiler version.


Verifying the Setup

Ensure all components are correctly installed:
1. Check headers:

ls /usr/src/linux-headers-$(uname -r)/include/linux
Look for files like module.h and kernel.h.

  1. Validate compiler:

    gcc --version
    # Check for cross-compiler if applicable
    $CROSS_COMPILE-gcc --version
    

  2. Test module build:
    Create a simple module (e.g., hello.c) and attempt compilation:

    make -C /usr/src/linux-headers-$(uname -r) M=$(pwd) modules
    
    This builds the module using the current kernel's build system.


Key takeaways

  • Install kernel headers matching your running kernel version.
  • Use make menuconfig or make oldconfig to 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.