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

标签 c linux makefile linux-kernel kernel-module

我正在使用一个简单示例来创建一个 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/

相关文章:

linux - Bash - if 语句中的错误代码不起作用

linux - ubuntu 的 UWSGI https 配置

linux - 如何测量硬件中断和相关系统调用之间的延迟?

c++ - 通过 Makefile 的预处理器宏定义在 C++ 中无法正常工作

makefile - 如何将makefile目标中的 '$*'值大写?

c - 警告 : [-Wformat=] and strcmp() not comparing in C

无法找出C应用程序中的内存泄漏

android - AOSP构建错误

c - 我如何定义两个结构,每个都在第二个结构中使用? C语言

visual-studio - 为什么在 Visual Studio 中连续的 int 数据类型变量位于 12 字节偏移处?