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:
- In kernel source code
- 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
- 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.

- In kernel source directory, create your driver’s subdirectory:

- Copy module source code in your driver’s directory.
- In driver’s directory create Makefile with content:

- In same directory create Kconfig file with configuration data for your driver:

- Append global driver Kconfig file, <kernel-src>/drivers/Kconfig, with content:

- Append global driver Makefile, <kernel-src>/drivers/Makefile, with content:

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.
- Create and enter working directory:

- Copy module source code in your directory.
- Create Makefile with following content:

- Initiate build process of your module by typing:
