无法编译内核模块

无法编译内核模块

我尝试编译我的第一个可加载内核模块但失败了:(

错误如下:

r@ubuntu:/$ sudo make -C /lib/modules/`uname -r`/build/ M=`pwd`
[sudo] password for r: 
make: Entering directory `/usr/src/linux-headers-2.6.38-8-generic'
scripts/Makefile.build:76: kbuild: Makefile.build is included improperly
make[1]: *** No rule to make target `kernel/bounds.c', needed by `kernel/bounds.s'.  Stop.
make: *** [_module_] Error 2
make: Leaving directory `/usr/src/linux-headers-2.6.38-8-generic'

我需要帮助了解问题出在哪里。

下面是我正在使用的 makefile:

obj-m += sample_module_for_techblog.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 
clean-files := Module.symvers

我发现了以下一段代码(打印我得到的错误片段)来自这里

ifndef obj
$(warning kbuild: Makefile.build is included improperly)
endif

我希望这有助于解决问题。

答案1

尝试一下这个,根据需要进行调整。我不熟悉您链接的教程,但假设您创建了尝试编译的最基本的“hello world”样式模块:

ifeq ($(KERNELRELEASE),)

    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)

modules:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

# adjust this by removing what applies to you
clean:
        rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions \
        *.symvers  *.order 

.PHONY: modules modules_install clean

else
obj-m := hello.o 

endif

相关内容