LKM - 无法编译模块 - 缺少头文件但安装了头包
LKM - can't compile module - missing headers files but header packages are installed
所以我一直在尝试在我的 Ubuntu 18.04 运行 4.15.0-43-generic 和 linux-headers- 上编译一个简单的 "Hello world" LKM 4.15.0-43 已安装。
这是代码:
#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");
这是我的 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)
这些是/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
我得到了一个 fatal error: asm/barrier.h: No such file or directory
,这是可以预料的,因为 asm
目录不存在。请参阅下面的完整输出:
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
对...所以我想如果我创建一个 symlink 的 asm-generic 到 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
确认 link 在那里:
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
一切正常吗?不 :( 现在它抱怨 asm/thread_info.h
找不到(在我做了 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
我从一开始就知道 symlink 是个坏主意。
我也尝试包含 /usr/src/linux-headers-4.15.0-43/include
而不是 /lib/modules/...
但长话短说,"asm" 目录不在 /usr/src/linux-headers-4.15.0-43/include/
和 symlink 中将 /usr/src/linux-headers-4.15.0-43/include/asm-generic
转换为 /usr/src/linux-headers-4.15.0-43/include/asm
会产生与上述相同的问题。
我知道问题出在哪里,显然头文件丢失了,但问题是我找不到它们,我从哪里得到它们?软件包已安装:
$ 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
我都试过了,似乎都有同样的问题。
好的,多亏了 Tsyvarev,我在进一步阅读后发现了问题 :)
问题的主要原因是我的 Makefile,以下 对我有用:
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
这解决了我遇到的原始问题,弹出了与此线程无关的新问题,但都已解决。
所以我一直在尝试在我的 Ubuntu 18.04 运行 4.15.0-43-generic 和 linux-headers- 上编译一个简单的 "Hello world" LKM 4.15.0-43 已安装。
这是代码:
#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");
这是我的 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)
这些是/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
我得到了一个 fatal error: asm/barrier.h: No such file or directory
,这是可以预料的,因为 asm
目录不存在。请参阅下面的完整输出:
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
对...所以我想如果我创建一个 symlink 的 asm-generic 到 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
确认 link 在那里:
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
一切正常吗?不 :( 现在它抱怨 asm/thread_info.h
找不到(在我做了 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
我从一开始就知道 symlink 是个坏主意。
我也尝试包含 /usr/src/linux-headers-4.15.0-43/include
而不是 /lib/modules/...
但长话短说,"asm" 目录不在 /usr/src/linux-headers-4.15.0-43/include/
和 symlink 中将 /usr/src/linux-headers-4.15.0-43/include/asm-generic
转换为 /usr/src/linux-headers-4.15.0-43/include/asm
会产生与上述相同的问题。
我知道问题出在哪里,显然头文件丢失了,但问题是我找不到它们,我从哪里得到它们?软件包已安装:
$ 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
我都试过了,似乎都有同样的问题。
好的,多亏了 Tsyvarev,我在进一步阅读后发现了问题 :)
问题的主要原因是我的 Makefile,以下 对我有用:
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
这解决了我遇到的原始问题,弹出了与此线程无关的新问题,但都已解决。