Cross-compiling a kernel module
In Chapter 3, Building the 6.x Linux Kernel from Source – Part 2, in the Kernel build for the Raspberry Pi section, we showed you how we can cross-compile the Linux kernel for a “foreign” target architecture (such as ARM[64], PowerPC, MIPS, and so on). Essentially, the same can be done for a kernel module as well; you can easily cross-compile a kernel module by setting up a cross-toolchain as well as the “special” ARCH
and CROSS_COMPILE
environment variables appropriately.
For example, let’s imagine we are working on an embedded Linux product; the target device on which our code will run has an AArch64 (ARM-64) CPU. Why not take an actual example: let’s cross-compile our ch5/lkm_template
kernel module for the Raspberry Pi 4 Single-Board Computer (SBC)!
This is interesting. You will find that although it appears simple and straightforward, we will end up taking four iterations before we succeed...