How to compile Linux kernel modules?

The Linux kernel is implemented as a large, standalone and modular software solution that contains different hierarchically organized configuration and build files. An extension  of Linux kernel functionalities is possible thanks to the kernel’s modules that represent a piece of code which can be loaded into or unloaded from the kernel. 

There are several advantages that come with using kernel modules:

– We do not need to rebuild an entire kernel so often

– Easier to diagnose, fix and remove potential error or bug

– We can save memory by loading module, and unloading it after the job is finished

– In project development phase, we are given the opportunity to observe and debug module as independent unit, while building it out-of-tree

The modules compilation process differs significantly from the one used for user space applications. There are two possible ways to build Linux kernel modules:

  1. In kernel source code
  2. Out-of-tree

Within this example, cross compilation steps will be presented in order to successfully compile and load kernel modules for ARM based TI BeagleBone Black development platform. Firstly, it will be described how to cross compile Linux kernel modules within Linux kernel source code and after that it will be described how to compile it out-of-tree and load successfully within a running Linux OS. 

I – Compile module within Linux kernel source code

  1. Download source code in your preferred location. There are a number of ways we can do this for the BeagleBone platform, but here we will concentrate on the official kernel release which can be found on the BeagleBoard github repo. After that, we can just checkout the kernel source version.
Vtool_How_to compile_Linux_kernel_modules
  1. In kernel source directory, create your driver’s subdirectory:
Vtool_How_to compile_Linux_kernel_modules
  1. Copy module source code in your driver’s directory.
  2. In driver’s directory create Makefile with content:
Vtool_How_to compile_Linux_kernel_modules
  1. In same directory create Kconfig file with configuration data for your driver:
Vtool_How_to compile_Linux_kernel_modules
  1. Append global driver Kconfig file, <kernel-src>/drivers/Kconfig, with content:
Vtool_How_to compile_Linux_kernel_modules
  1. Append global driver Makefile, <kernel-src>/drivers/Makefile, with content:
Vtool_How_to compile_Linux_kernel_modules

Config files for ARM architecture can be found at <kernel-src>/arch/arm/configs/*defconfig

Out-of-tree

In the following demo, we will show how to compile vtool-module out of kernel source code for ARM platform. Before that, some preparation is required. Because we will compile a module for the ARM platform, a few variables in our Makefile must be set. Those variables are ARCH (for target cpu architecture), CROSS_COMPILE (for arm cross compiler) and KERNEL_DIR (path to kernel source directory). Before running the build process, the directory of cross compiler binaries must be exported in the PATH variable. Make build system changes working directory to directory specified after -C flag, right before starting any kind of compilation. With option M=${PWD}, we made sure compilation files will be placed in the same, working directory.

  1. Create and enter working directory:
Vtool_How_to compile_Linux_kernel_modules
  1. Copy module source code in your directory.
  2. Create Makefile with following content:
Vtool_How_to compile_Linux_kernel_modules
  1. Initiate build process of your module by typing:

Contact Us

Please, enter your details below. We will get back to you as fast as we can.

This website uses cookies to ensure you get the best experience on our website.

Get Cogita

Please, enter your details below. We will get back to you as fast as we can.

Thank you!

We will get in touch soon.

Get a Quote

Please, enter your details below. We will get back to you as fast as we can.