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:

arm cross development toolchain

There are basically two main options you might want to consider: 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 Once built, the toolchain can be used with:
export CROSS_COMPILE=/opt/armv7/bin/arm-buildroot-linux-gnueabihf-

kernel matching exactly the one used in Raspbian

This is a manual process to find the exact version of the kernel used in Raspbian...

First run this command:
pi@raspberrypi:~ $ uname -a
Linux raspberrypi 4.14.79-v7+ #1159 SMP Sun Nov 4 17:50:20 GMT 2018 armv7l GNU/Linux
pi@raspberrypi:~ $
Then go to this url where the kernel for Rasberry PI is stored:
https://github.com/raspberrypi/linux

Select the branch 4.14:
https://github.com/raspberrypi/linux/tree/rpi-4.14.y

Search for the file Makefile in the root directory for 4.14 branch:
https://github.com/raspberrypi/linux/blob/rpi-4.14.y/Makefile

You should see three buttons:

Select the button

Search for: 4.14.79

Click then on the short commit ID - In my case this was this url

Please not the commit id. In my case this was:
50961e4888a1d53544ac4ea6f185fc27ee4fee4f

You can now get the kernel for the Raspbian:
~/rpi-kernel/linux-test$ git clone -b rpi-4.14.y git@github.com:raspberrypi/linux.git
~/rpi-kernel/linux-test/linux$ git checkout 50961e4888a1d53544ac4ea6f185fc27ee4fee4f
Note: checking out '50961e4888a1d53544ac4ea6f185fc27ee4fee4f'.

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.

minimal kernel module