Skip to contentSkip to conversation

problems compiling kernel module

DISCUSSION Fernando Barsoba 3 participants 4 messages Apr 19, 2006

lore
Share & tools
lore
#0 lore raw

Hi,

I am really stuck with this thing.. For couple of days i have been trying to compile a kernel module. I have been following the info in http://www.faqs.org/docs/kernel/x204.html. But no success... i recompiled the latest kernel version, and i think i trying to compile the module against the source code for that kernel.. however, strange errors appear.

Please any help will be appreciated.

Here's the error:

Code:

[fbarsoba@localhost ~]$ cd workspace/kernel_tests/
[fbarsoba@localhost kernel_tests]$ make
gcc -O2 -DMODULE -D__KERNEL__ -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/uname -r/build/include -c -o hello-1.o hello-1.c
In file included from /lib/modules/2.6.16.7/build/include/linux/spinlock.h:87, from /lib/modules/2.6.16.7/build/include/linux/capability.h:45, from /lib/modules/2.6.16.7/build/include/linux/sched.h:7, from /lib/modules/2.6.16.7/build/include/linux/module.h:10, from hello-1.c:3:
/lib/modules/2.6.16.7/build/include/asm/spinlock.h: In function ‘__raw_spin_lock’: /lib/modules/2.6.16.7/build/include/asm/spinlock.h:42: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’ /lib/modules/2.6.16.7/build/include/asm/spinlock.h: In function ‘__raw_read_lock’: /lib/modules/2.6.16.7/build/include/asm/spinlock.h:96: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’ /lib/modules/2.6.16.7/build/include/asm/spinlock.h:96: error: expected expression before ‘else’ /lib/modules/2.6.16.7/build/include/asm/spinlock.h: In function ‘__raw_write_lock’: /lib/modules/2.6.16.7/build/include/asm/spinlock.h:101: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’ /lib/modules/2.6.16.7/build/include/asm/spinlock.h:101: error: expected expression before ‘else’ In file included from /lib/modules/2.6.16.7/build/include/linux/sched.h:20, from /lib/modules/2.6.16.7/build/include/linux/module.h:10, from hello-1.c:3: /lib/modules/2.6.16.7/build/include/asm/semaphore.h: In function ‘down’: /lib/modules/2.6.16.7/build/include/asm/semaphore.h:112: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’ /lib/modules/2.6.16.7/build/include/asm/semaphore.h: In function ‘down_interruptible’: /lib/modules/2.6.16.7/build/include/asm/semaphore.h:137: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’ /lib/modules/2.6.16.7/build/include/asm/semaphore.h: In function ‘down_trylock’:/lib/modules/2.6.16.7/build/include/asm/semaphore.h:161: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’ /lib/modules/2.6.16.7/build/include/asm/semaphore.h: In function ‘up’: /lib/modules/2.6.16.7/build/include/asm/semaphore.h:184: error: expected ‘:’ or ‘)’ before ‘KBUILD_BASENAME’ make: *** [hello-1.o] Error 1

And here are the files:

Code:

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

int init_module(void) {
printk("<1>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");
}

Code:

TARGET := hello-1
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes INCLUDE := -isystem /lib/modules/uname -r/build/include CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE} CC := gcc ${TARGET}.o: ${TARGET}.c
.PHONY: clean
clean: rm -rf {TARGET}.o

#1 lore raw
Quoted 6 lines · Fernando Barsoba

I am really stuck with this thing.. For couple of days i have been trying to compile a kernel module. I have been following the info in
http://www.faqs.org/docs/kernel/x204.html. But no success... i recompiled the latest kernel version, and i think i trying to compile the module against the source code for that kernel.. however, strange errors appear.

That way just doesn't work. Use kbuild instead of brewing your own Makefiles. See http://lwn.net/Articles/21823/ .

Quoted 6 lines

And here are the files:

Code:

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

Not necessary, IIRC.

Quoted 1 line

*/ #include <linux/kernel.h> /* Needed for KERN_ALERT */

OK...

Quoted 2 lines

int init_module(void) {
printk("<1>Hello world 1.\n"); // A non 0 return means init_module

... so why don't you use KERN_ALERT instead of <1>?

Make that printk(KERN_ALERT "Hello, world!\n");

Quoted 11 lines

failed; module can't be loaded.
return 0;
}

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

Code:

Quoted 7 lines

TARGET := hello-1
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes INCLUDE := -isystem /lib/modules/uname -r/build/include CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE} CC := gcc ${TARGET}.o: ${TARGET}.c
.PHONY: clean
clean: rm -rf {TARGET}.o

You want something like:

ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

default:

	$(MAKE) -C $(KDIR) M=$(PWD) modules

endif

Erik

Signature · 2 lines
+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands
#3 lore raw
In reply to Fernando Barsoba
Quoted 6 lines · Fernando Barsoba

I am really stuck with this thing.. For couple of days i have been trying to compile a kernel module. I have been following the info in
http://www.faqs.org/docs/kernel/x204.html. But no success... i recompiled the latest kernel version, and i think i trying to compile

your makefile is bust; please read Documentation/kbuild for a 2.6 level makefile (yours is a bad job even for 2.4 kernels but it'll more or less work there)

#2 lore raw
In reply to Erik Mouw

Thanks a lot for your makefile. It worked nicely. I wasn't sure if the question was too basic for this mailing list.

Thanks also for Arjan's reference to http://fxr.watson.org/fxr/source/Documentation/kbuild/makefiles.txt?v=linux-2.6.9

I am new in kernel programming...

tnx,
Fernando

Quoted 79 lines

From: Erik Mouw <[email protected]>
To: Fernando Barsoba <[email protected]>
CC: [email protected]
Subject: Re: problems compiling kernel module
Date: Wed, 19 Apr 2006 16:24:42 +0200

Quoted 6 lines · Fernando Barsoba

I am really stuck with this thing.. For couple of days i have been trying to compile a kernel module. I have been following the info in
http://www.faqs.org/docs/kernel/x204.html. But no success... i recompiled the latest kernel version, and i think i trying to compile the module against the source code for that kernel.. however, strange errors appear.

That way just doesn't work. Use kbuild instead of brewing your own Makefiles. See http://lwn.net/Articles/21823/ .

Quoted 6 lines

And here are the files:

Code:

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

Not necessary, IIRC.

Quoted 1 line

*/ #include <linux/kernel.h> /* Needed for KERN_ALERT */

OK...

Quoted 2 lines

int init_module(void) {
printk("<1>Hello world 1.\n"); // A non 0 return means init_module

... so why don't you use KERN_ALERT instead of <1>?

Make that printk(KERN_ALERT "Hello, world!\n");

Quoted 11 lines

failed; module can't be loaded.
return 0;
}

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

Code:

Quoted 7 lines

TARGET := hello-1
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes INCLUDE := -isystem /lib/modules/uname -r/build/include CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE} CC := gcc ${TARGET}.o: ${TARGET}.c
.PHONY: clean
clean: rm -rf {TARGET}.o

You want something like:

ifneq ($(KERNELRELEASE),)
obj-m := hello.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)

default:

	$(MAKE) -C $(KDIR) M=$(PWD) modules

endif

Erik

--

+-- Erik Mouw -- www.harddisk-recovery.com -- +31 70 370 12 90 --
| Lab address: Delftechpark 26, 2628 XH, Delft, The Netherlands

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/