Linux / kernel.h: No such file or directory

I am going to write a Hello World module in Ubuntu 10.10 (with kernel 2.6.35-28-generic). Headings 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 

Make error:

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

How to solve this?

+6
source share
4 answers

Do you have a / usr / src / linux symlink to your / usr / src / linux -headers-2.6.35-28-generic? If not, create it using the following commands

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

You cannot use the traditional Makefile style with Linux kernel modules; while you may be able to make something work, it will be a painful experience.

Start by reading the file Documentation/kbuild/modules.txt ; it describes exactly what you need to do when writing the Makefile module so that it can conveniently connect to the Kbuild core. 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 believe me about it; while you might think that you are β€œjust one small change” to get your own Makefile , even minor changes in the kernel version will completely ruin your assembly again and again. Just take an hour to write a Kbuild -compatible Makefile for your module. I spent several weeks of my life trying to preserve a pre-existing Makefile when the Kbuild framework was introduced. Each new core made me lose hours of performance.

+7
source

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

+2
source

as @sarnold said, you should use another Makefile.Just as below:

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.

+2
source

Source: https://habr.com/ru/post/885602/


All Articles