编译内核模块时

时间:2018-07-03 11:16:39

标签: linux kernel-module

我正在尝试在Fedora 28中编译内核模块。我当前的内核是4.17.3-200.fc28.x86_64。我的hello.c是

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
static int __init hello_2_init(void)
{
 printk(KERN_INFO "Hello, world 2\n");
 return 0;
}
static void __exit hello_2_exit(void)
{
 printk(KERN_INFO "Goodbye, world 2\n");
}
module_init(hello_2_init);
module_exit(hello_2_exit);

我的Makefile是

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) modules

尝试编译时出现此错误。最初我认为该错误是由于ssl库引起的。因此,我为fedora安装了openssl-devel。但是我仍然遇到同样的错误。

make -C /lib/modules/4.17.3-200.fc28.x86_64/build M= modules
make[1]: Entering directory '/usr/src/kernels/4.17.3-200.fc28.x86_64'
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
make[2]: *** No rule to make target 'lib/sha256.c', needed by 'arch/x86/purgatory/sha256.o'.  Stop.
make[1]: *** [arch/x86/Makefile:263: archprepare] Error 2
make[1]: Leaving directory '/usr/src/kernels/4.17.3-200.fc28.x86_64'
make: *** [Makefile:4: all] Error 2

如何解决此错误?

1 个答案:

答案 0 :(得分:1)

obj-m   := FILENAME.o

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

all: 
      default

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


clean:
      rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions 



尝试使用此Makefile或在控制台中编写:

make -C /lib/modules/`uname -r`/build M=`pwd` modules obj-m=FILENAME.o

用文件名替换FILENAME!