7
votes

I am going to write a Hello World module in Ubuntu 10.10 (with the kernel 2.6.35-28-generic). Headers are located:

/usr/src/linux-headers-2.6.35-28-generic

hello.c:

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

int init_module(void)
{
    printk("Hello, world\n");
    return 0;
}

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

and Makefile:

CC = gcc
CFLAGS = -Wall -DMODULE -D__KERNEL__
hello.o: hello.c
    $(CC) $(CFLAGS) -c hello.c
    echo insmod hello.o to install
    echo rmmod to delete

There is an error while make:

hello.c:1: fatal error: linux/kernel.h : No such file or directory compilation terminated.

How do I solve this?

4
guy, is this work? i've done all that is written in answer that you chose as solution. but i still get "linux/kernel.h nu such file". i've browsed whole internet, but nothing successful. i'm using ubuntu 10.04Tebe
Yes it is. The code in the question at the moment of publishing had a simple mistake: there was space characters (' ') inside the #include <linux/kernel.h> because of I had just copied the code from HTML page with the simple driver example. The second problem is solved by an answer I marked as solution. Check the link you have create is valid and follows to the right path, check if the path /usr/src/linux-headers-$(uname -r) exists.user663896

4 Answers

7
votes

You can't just use a traditional-style Makefile with Linux kernel modules; while you might be able to force something to work, it'll be a painful experience.

Start by reading the Documentation/kbuild/modules.txt file; it'll describe exactly what you need to do when writing a module Makefile so that it can hook neatly into the kernel's Kbuild environment. Your Makefile will probably look something like this:

ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
obj-m  := 8123.o
8123-y := 8123_if.o 8123_pci.o 8123_bin.o

else
# normal makefile
KDIR ?= /lib/modules/`uname -r`/build

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

# Module specific targets
genbin:
    echo "X" > 8123_bin.o_shipped

endif

Please trust me on this; while you might think you're "just one small change" from getting your own Makefile to work, even minor changes in kernel version will completely destroy your build all over again. Just take the hour now to write a Kbuild-compatible Makefile for your module. I wasted weeks of my life trying to maintain a pre-existing Makefile when the Kbuild infrastructure was introduced. Every new kernel caused me to lose hours of productivity.

3
votes

For me this file ("linux/kernel.h") is in the package linux-libc-dev (Kubuntu 10.10).

2
votes

Do you have /usr/src/linux symbolic link to your /usr/src/linux-headers-2.6.35-28-generic ? If not then create one using following commands

# cd /usr/src
# ln -sfn linux-headers-2.6.35-28-generic linux
2
votes

just as what @sarnold said , you should use the different Makefile.Just as following:

obj-m += hello.o

all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

and use the command:

insmod hello.ko

to install this module.