gpt4 book ai didi

c - 错误编译内核模块 linux/module.h : No such file or directory found

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:16:00 24 4
gpt4 key购买 nike

我正在使用一个简单示例来创建一个 Hello World 内核模块。下方链接: http://www.thegeekstuff.com/2013/07/write-linux-kernel-module/

当尝试在与 hello.c 相同的目录中使用“make hello”运行 makefile 时。我收到以下错误:

hello.c:1:64: fatal error: linux/module.h: No such file or directory

我已经成功安装了 linux 头文件:

$ sudo apt-get install linux-headers-$(uname -r)
$ sudo apt-get install linux-source

我什至可以导航到 /usr/src/linux-headers-3.13.0-51/include/linux/module.h 中的正确文件,但同样的错误仍然存​​在.

reddit 上的一篇类似帖子给了我希望,但我不确定我是否在 makefile 中使用了正确的 -I dir 语法。 http://www.reddit.com/r/linux4noobs/comments/2o4u1r/linuxmoduleh_no_such_file_or_directory_found_help/

如有任何帮助,我们将不胜感激!谢谢!

我正在使用 Linux 版本 3.13.51 运行 Xubuntu 14.04。安装了 kmod。

编辑 1:更多信息这是生成文件:

obj-m += hello.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

最佳答案

我使用这个 Makefile:

TARGET = hello
OBJS = hello.o
MDIR = drivers/misc

EXTRA_CFLAGS = -DEXPORT_SYMTAB
CURRENT = $(shell uname -r)
KDIR = /lib/modules/$(CURRENT)/build
PWD = $(shell pwd)
DEST = /lib/modules/$(CURRENT)/kernel/$(MDIR)

obj-m := $(TARGET).o

default:
make -C $(KDIR) SUBDIRS=$(PWD) modules

$(TARGET).o: $(OBJS)
$(LD) $(LD_RFLAG) -r -o $@ $(OBJS)

ifneq (,$(findstring 2.4.,$(CURRENT)))
install:
su -c "cp -v $(TARGET).o $(DEST) && /sbin/depmod -a"
else
install:
su -c "cp -v $(TARGET).ko $(DEST) && /sbin/depmod -a"
endif

clean:
-rm -f *.o *.ko .*.cmd .*.flags *.mod.c

-include $(KDIR)/Rules.make

我将它用于 3.2 内核。我认为关键是最后一行,其中一些内核相关的规则被添加到 Makefile 中。

root@devmachine:~/hello# make
make -C /lib/modules/3.2.0-4-amd64/build SUBDIRS=/root/hello modules
make[1]: Entering directory '/usr/src/linux-headers-3.2.0-4-amd64'
Makefile:10: *** mixed implicit and normal rules: deprecated syntax
CC [M] /root/hello/hello.o
/root/hello/hello.c: In function 'init_driver':
/root/hello/hello.c:25:18: warning: initialization makes pointer from integer without a cast [enabled by default]
Building modules, stage 2.
MODPOST 1 modules
CC /root/hello/hello.mod.o
LD [M] /root/hello/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-3.2.0-4-amd64'

关于c - 错误编译内核模块 linux/module.h : No such file or directory found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30021405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com