How to cross-compile a simple kernel module for Raspbian (RPI3)
Summary
This guide explains how to cross-compile from a Linux host a kernel module that can be used from Raspian on the Rasperry PI 3. The Rasperry PI3 has enough resource (cpu, memory, storage) to compile natively a kernel module. This being said, in some cases, you might want or prefer to develop from a Linux host and cross-compile your kernel module.
You will need two things:
an arm cross development toolchain
a version of the kernel matching exactly the one used in Raspbian.
arm cross development toolchain
There are basically two main options you might want to consider:
use an arm cross-development toolchain provided by your Linux distribution (Debian9, Fedora, etc.)
build your own
For the first option, simply do a Google search how to install the arm toolchain, and you will be quickly set.
I personally prefer to use a toolchain that I build myself. buildroot make things so easy and convenient, that would be almost a shame to not take advantage of it ;-)
Once you have unarchived buildroot, you can use this configuration file which I used with buildroot-2018.02.10
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b
HEAD is now at 50961e4888a1 Linux 4.14.79
~/rpi-kernel/linux-test/linux$ git branch
* (HEAD detached at 50961e4888a1)
rpi-4.14.y
~/rpi-kernel/linux-test/linux$
The next step will be to cross-compile the kermel used by Raspbian.
In order to be 100% sure I was using the exact kernel configuration, I downloaded the one used on Raspbian:
pi@raspberrypi:~ $ sudo modprobe configs
pi@raspberrypi:~ $ ls -la /proc/config.gz
-r--r--r-- 1 root root 36601 Feb 24 21:54 /proc/config.gz
Copy this configuration file and use it to cross-compile the kernel.