So I have been trying to compile a simple "Hello world" LKM on my Ubuntu 18.04 running 4.15.0-43-generic and the linux-headers-4.15.0-43 are installed.
This is the code:
#define MODULE
#define LINUX
#define __KERNEL__
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
printk(KERN_DEBUG "Hello World!");
return 0;
}
module_init( init_module );
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Hello world");
This is my Makefile:
CC := gcc
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS := -O2 -c $(WARN) $(INCLUDE)
SOURCES := helloworld.c
TARGET := helloworld.ko
all:
$(CC) $(CFLAGS) $(SOURCES) -o $(TARGET)
These are the contents of /lib/modules/$(uname -r)/build/include
acpi asm-generic clocksource config crypto drm dt-bindings generated keys kvm linux math-emu media memory misc net pcmcia ras rdma scsi soc sound target trace uapi video xen
and I got a fatal error: asm/barrier.h: No such file or directory
which is to be expected because the asm
dir isn't there. See below the full output:
gcc -O2 -c -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include helloworld.c -o helloworld.ko
In file included from /usr/src/linux-headers-4.15.0-43/include/linux/init.h:5:0,
from helloworld.c:5:
/usr/src/linux-headers-4.15.0-43/include/linux/compiler.h:247:10: fatal error: asm/barrier.h: No such file or directory
#include <asm/barrier.h>
^~~~~~~~~~~~~~~
compilation terminated.
Makefile:9: recipe for target 'all' failed
make: *** [all] Error 1
Right...so I thought what if I create a symlink of asm-generic to asm?
sudo ln -s /lib/modules/$(uname -r)/build/include/asm-generic /lib/modules/$(uname -r)/build/include/asm
ls -ld /lib/modules/$(uname -r)/build/include/asm
confirms the link is there:
lrwxrwxrwx 1 root root 56 Jan 24 19:51 /lib/modules/4.15.0-43-generic/build/include/asm -> /lib/modules/4.15.0-43-generic/build/include/asm-generic
Everything should work right? No :( Now it's complaining asm/thread_info.h
cannot be found (after I did the symlink).
gcc -O2 -c -W -Wall -Wstrict-prototypes -Wmissing-prototypes -isystem /lib/modules/`uname -r`/build/include helloworld.c -o helloworld.ko
In file included from /lib/modules/4.15.0-43-generic/build/include/asm/preempt.h:5:0,
from /usr/src/linux-headers-4.15.0-43/include/linux/preempt.h:81,
from /usr/src/linux-headers-4.15.0-43/include/linux/spinlock.h:51,
from /usr/src/linux-headers-4.15.0-43/include/linux/seqlock.h:36,
from /usr/src/linux-headers-4.15.0-43/include/linux/time.h:6,
from /usr/src/linux-headers-4.15.0-43/include/linux/stat.h:19,
from /usr/src/linux-headers-4.15.0-43/include/linux/module.h:10,
from helloworld.c:6:
/usr/src/linux-headers-4.15.0-43/include/linux/thread_info.h:38:10: fatal error: asm/thread_info.h: No such file or directory
#include <asm/thread_info.h>
^~~~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:9: recipe for target 'all' failed
make: *** [all] Error 1
I knew from the start that the symlink was a bad idea.
I also tried including /usr/src/linux-headers-4.15.0-43/include
instead of /lib/modules/...
but long story short, the "asm" dir isn't there in /usr/src/linux-headers-4.15.0-43/include/
and symlinking /usr/src/linux-headers-4.15.0-43/include/asm-generic
to /usr/src/linux-headers-4.15.0-43/include/asm
produced the same issues as above.
I know what the problem is, obviously the header files are missing but the problem is I cannot find them, where do I get them from? The packages are installed:
$ dpkg -l | grep linux-headers-
ii linux-headers-4.15.0-23
ii linux-headers-4.15.0-43
ii linux-headers-4.15.0-43-generic
ii linux-headers-generic
I tried all of those, all appear to have the same issue.
Ok I found the problem after some further reading thanks to Tsyvarev :)
The main cause of the problem was with my Makefile, the following does work for me:
obj-m += helloworld.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
That fixed the original issue I was having, new one popped up not related to this thread but all fixed.