开发者

linux/kernel.h : No such file or directory

开发者 https://www.devze.com 2023-02-23 16:56 出处:网络
I am going to write a Hello World module in Ubuntu 10.10 (with the kernel 2.6.35-28-generic). Headers are located:

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(v开发者_StackOverflow中文版oid)
{
    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?


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号