PDA

View Full Version : errors compiling modules



fr0ike
October 6th, 2007, 12:52 PM
hi,

i want to learn how to build device drivers (kernel modules). so i bought a book and started to read...
i`ve copied some piece of code from the book and tried to compile them,
but there are some errors popped up i can`t solve myself. i hope you can help me out...

*******************************
module source
*******************************

#define _LINUX_TIME_H 1 //thats to solve the "Redefinition von »struct timespec«" problem

#include <linux/version.h>
#include <linux/module.h>


int init_module(void){
printk("init_module called\n");
return 0;
}

void cleanup_module(void){
printk("cleanup_module called\n");
}

MODULE_LICENSE("GPL");
*******************************

*******************************
Makefile (changed, perhaps its a piece of crap, i`m not very familiar with makefiles)
*******************************
obj-m := mod1.o
mod1.o: mod1.c
*******************************


*******************************
output
*******************************
$make
cc -c -o mod1.o mod1.c
In Datei, eingefügt von /usr/include/linux/sched.h:16,
von /usr/include/linux/module.h:9,
von mod1.c:4:
/usr/include/linux/signal.h:2:2: Warnung: #warning "You should include <signal.h>. This time I will do it for you."
In file included from mod1.c:4:
/usr/include/linux/module.h:41: Fehler: Feld »attr« hat unvollständigen Typen
/usr/include/linux/module.h:49: Fehler: Feld »kobj« hat unvollständigen Typen
make: *** [mod1.o] Fehler 1
*******************************

*******************************
Makefile from book
*******************************
ifneq ($(KERNELRELEASE),)
obj-m := mod1.o

else:
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

default:
$(MAKE) -C $(KDIR) M=$(PWD) modules
endif
*******************************

*******************************
output
*******************************
$make
make: *** Keine Targets. Schluss.
*******************************


some info:

linux-headers-2.6.15-29-686
linux-source-2.6.15
ubuntu 6.06 "dapper drake"
gcc-Version 4.0.3 (Ubuntu 4.0.3-1ubuntu5)


Thanks in advance!
fr0ike

moma
October 6th, 2007, 08:16 PM
Hello,

Try this

File:mod1.c


/*
#define _LINUX_TIME_H 1
thats to solve the "Redefinition von »struct timespec«" problem
*/

#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

#include <linux/time.h>

#include <linux/init.h>
#include <linux/stat.h>


MODULE_LICENSE("GPL");
MODULE_AUTHOR("Kalle & Olga Kuusamolainen");

static int __init init_this_module(void)
{
printk(KERN_INFO "mod1: init_this_module() called.\n");
return 0;
}

static void __exit shutdown_this_module(void)
{
printk(KERN_INFO "mod1: shutdown_this_module() called.\n");
}


module_init(init_this_module);
module_exit(shutdown_this_module);

File:Makefile

obj-m := mod1.o

KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

Note:
In the Makefile, there must be a TAB (tabulator) character before the
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
line.


Compile it (using the Makefile):
$ make

Load the module mod1.ko:
$ sudo insmod mod1.ko

Now check the system messages
$ dmesg | tail -10

[13408.233936] mod1: init_this_module() called.

Remove the module:
$ sudo rmmod mod1

Check again the system messages
$ dmesg | tail -10
or
$ dmesg | grep "mod1"

[13408.233936] mod1: init_this_module() called.
[13408.233937] mod1: shutdown_this_module() called.

Very important: Study these kernel developers' guides
http://www.futuredesktop.org/kernel.html Section: Driver and module programming.

Good luck ;-)
-----------------------

EDIT: Prerequisites to compile a kernel-module are:
$ sudo aptitude install build-essential gdb linux-headers-$(uname -r)

You may also need:
$ sudo aptitude install automake autoconf libtool
-----------------------

ps. This is howto install VMware Player and VirtualBox in/on Ubuntu 7.10 beta (gutsy gibbon).
http://www.futuredesktop.org/vmware/vmware-player-gutsy.html (just in case you need some love)

fr0ike
October 6th, 2007, 11:00 PM
wow...
works perfectly ! thanks a lot !