【问题标题】:Error compiling kernel module linux/module.h: No such file or directory found编译内核模块 linux/module.h 时出错:找不到这样的文件或目录
【发布时间】:2015-05-04 01:38:13
【问题描述】:

我正在使用一个简单的示例来创建一个 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 已安装。

EDIT1:更多信息 这是生成文件:

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

【问题讨论】:

  • 好的!完毕!和教程链接一样

标签: c linux makefile linux-kernel kernel-module


【解决方案1】:

我使用这个 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'

【讨论】:

  • 我刚尝试过这个,我收到以下错误:'make: Circular hello.o
  • 你安装了kmod 包吗?
  • @meleod_ideafix 是的,我刚刚检查并安装了 kmod。
【解决方案2】:

好吧,我想通了,现在我觉得很傻......

我正在运行$ make,但出现错误

make: `all' 无事可做。

我认为这意味着我没有正确调用 make,所以我开始调用 $ make hello 来“尝试制作模块”这是混乱开始的地方,因为试图使这样的文件运行不正确在我的系统上制作文件。

相反,真正的解决方案是编辑 Makefile 以在构建调用之前取出空格并用制表符替换它们。这完全解决了我的问题,我能够顺利构建。

【讨论】:

    猜你喜欢
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多