Hi,
I have a newer BBB with Debian and am trying to build a hello world kernel module. Does anyone have the recipe for that? I also have an older BBB with 2GB Angstrom. To build the hello world module on that one, do this:
Create a directory and save a “Makefile” with this text. Note: indentations must be tabs and not spaces:
obj-m += hello.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Now make a file called “hello.c”:
/* Source file .c /
/ hello.c */
#include <linux/module.h> // Needed by all modules
#include <linux/kernel.h> // Needed for KERN_ALERT
#include <linux/init.h> // Needed for the macros
static int hello_init(void)
{
printk(KERN_ALERT “Hello, My module!\n”);
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT “Goodbye, My module!\n”);
}
save both files. Run “make”
Won’t compile? It’s because the sock distro doesn’t have the kernel headers and development environment installed and set up. Here’s what you have to do. This will take about 45 minutes:
opkg update
opkg upgrade
opkg install kernel-headers
opkg install kernel-dev
reboot the BBB
cd /usr/src/kernel
make scripts
ln -s /usr/src/kernel /lib/modules/uname -r
/build
re-run:
opkg upgrade // completes silently
run make in the folder with your hello.c and Makefile.
It should build in about 1/2 second and the last line output will be something like “leaving kernel…”
now run:
ldmod hello.ko
dmesg // you should see the Hello text from the printk()
rmmod hello.ko
dmesg // you should see the Goodbye text from printk()