In Chapter 3, Building the 5.x Linux Kernel from Source - Part 2, in the Kernel build for the Raspberry Pi section, we showed how you can cross-compile the Linux kernel for a "foreign" target architecture (such as ARM, 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 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 AArch32 (ARM-32) CPU. Why not take an actual example. Let's cross-compile our Hello, world kernel module for the Raspberry Pi 3 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. Why? Read on...