PDA

View Full Version : [SOLVED] writing a trivial kernel module - help



sonicbs
May 28th, 2010, 04:53 PM
Hi, I am trying to learn how to write a kernel module.
I am following the excellent guide from The Linux Documentation Project (http://tldp.org/) called The Linux Kernel Module Programming Guide v.2.6.4 (http://tldp.org/LDP/lkmpg/2.6/html/index.html).

My machine is running Ubuntu Lucid Lynx (10.04)

uname -r
2.6.32-22-generic

I installed the corresponding linux headers and just to make sure I also installed the linux source and extracted it in /usr/src

I am trying to run the following trivial kernel module

/*
* hello-1.c - The simplest kernel module.
*/
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */

int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");

/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}

void cleanup_module(void)
{
printk(KERN_ALERT "Goodbye world 1.\n");
}

with the following Makefile

obj−m += hello−1.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
as it appears in pages 5 and 6 of the pdf version of the tutorial above.
I got the following error from make

uname: extra operand `−r'
Try `uname --help' for more information.
make −C /lib/modules//build M=/home/sonny/km modules
make[1]: Entering directory `/home/sonny/km'
make[1]: *** No rule to make target `−C'. Stop.
make[1]: Leaving directory `/home/sonny/km'
make: *** [all] Error 2
So, from the first and third lines I saw there was a problem with uname -r
so I changed the Makefile to

obj−m += hello-1.o
KVERSION = $(shell uname -r)
all:
make −C /lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
make −C /lib/modules/$(KVERSION)/build M=$(PWD) clean
and then running make corrected the first error, but I still get the following:

make −C /lib/modules/2.6.32-22-generic/build M=/home/sonny/km modules
make[1]: Entering directory `/home/sonny/km'
make[1]: *** No rule to make target `−C'. Stop.
make[1]: Leaving directory `/home/sonny/km'
make: *** [all] Error 2

What am I doing wrong? All the headers are in place and the source is there as well.
lib/modules/2.6.32-22-generic links to (what I think are) the right places.
Any help would be much appreciated.

Thanks a lot!

dwhitney67
May 28th, 2010, 05:19 PM
Your latest version of the Makefile looks good.

Make sure that you have the proper build tools installed on your system:


sudo apt-get install build-essential

Reiger
May 28th, 2010, 07:34 PM
You'll need to have kernel headers as well:


sudo apt-get install linux-header-`uname -r`

sonicbs
May 28th, 2010, 07:47 PM
You'll need to have kernel headers as well:


sudo apt-get install linux-header-`uname -r`


Reiger, thanks a lot for the help, but I already have the linux-header package installed.

sonicbs
May 28th, 2010, 07:48 PM
Your latest version of the Makefile looks good.

Make sure that you have the proper build tools installed on your system:


sudo apt-get install build-essential


dwhitney67, thanks a lot for the prompt reply, but I already have the build-essential package installed.
I also tried reinstalling it.

sonicbs
May 29th, 2010, 01:03 AM
Unbelievable... after 48 hours of headbanging it turns out the the hyphens I have in this file are not the right hypens. This messed everything. Let this be a warning to you all... don't copy&paste from a PDF!