Reputation: 2949
I have build a Linux kernel for the beaglebone black using buildroot. Now I would like to develop a hello world Linux kernel module application:
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
The problem is I still keep missing some header files. After finally gathering them all, I get an error that the code is not compilable (many errors, I don't want to paste them all). What I was wondering is either I am really including the right files?
At the moment I have:
/home/lukasz/brl/Machine/beaglebone/build/linux-headers-a75d8e93056181d512f6c818e8627bd4554aaf92/include
/home/lukasz/brl/Machine/beaglebone/build/uboot-2018.01/arch/x86/include
/home/lukasz/brl/Machine/beaglebone/build/linux-headers-a75d8e93056181d512f6c818e8627bd4554aaf92/arch/arm/include/generated
/home/lukasz/brl/Machine/beaglebone/build/linux-headers-a75d8e93056181d512f6c818e8627bd4554aaf92/arch/arm/include
/home/lukasz/brl/Machine/beaglebone/build/linux-a75d8e93056181d512f6c818e8627bd4554aaf92/include
Its a bit odd to me that the C include files and asm files are so scattered around within the directory. Are there some mistakes in my understanding of the topic here?
My Linux version:
# uname -a
Linux buildroot 4.9.59 #1 SMP Fri Oct 5 11:55:54 CEST 2018 armv7l GNU/Linux
Upvotes: 1
Views: 3599
Reputation: 3464
To compile a kernel module, you need the real kernel sources, not just the kernel header files. You have to build from the kernel source directory with M=
pointing to the source of your modules. And together with your module source, you of course also need a working Makefile. These steps are explained in any of the dozens of how-to-write-a-kernel-module guides, e.g. this one.
For cross-compilation, you also need to pass the appropriate arguments so that the kernel knows for which architecture to build and which cross-compiler to use. At the very least, this means you have to give the ARCH=
and CROSS_COMPILE=
options when building. Sometimes you need additional options (e.g. to point to the appropriate depmod tool).
To simplify this, Buildroot offers kernel module infrastructure. In the simplest case, you can just create a Config.in
file containing
config BR2_PACKAGE_HELLOMOD
bool "hellomod"
depends on BR2_LINUX_KERNEL
and a hellomod.mk
file containing
HELLOMOD_SITE = /path/to/hellomod/source
$(eval $(kernel-module))
$(eval $(generic-package))
You also have to source the Config.in
from package/Config.in
in the Buildroot tree. Or better yet, use an external tree so you don't have to modify Buildroot itself.
Upvotes: 3