首页 > 解决方案 > 内核演示构建错误

问题描述

在我有限的内核程序经验中,我的操作系统是 Ubuntu16.04,使用 sudo apt-get install linux-headers-`uname -r` 安装内核头文件,kernel_hello.c 文件是

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("DUAL BSD/GPL")

MODULE_AUTHOR("YANQIN")

MODULE_DESCRIPTION("kernel module hello")

MODULE_VERSION("1.0")


static int hello_init(void)

{
    printk(KERN_ALERT "hello_init() start\n");
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT "hello_exit() start\n");
}

module_init(hello_init);
module_exit(hello_exit);

Makefile 是

KERNAL_DIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
obj-m := kernel_hello.o
default:
    $(MAKE) -C $(KERNAL_DIR) M=$(PWD) modules

但构建结果是这样的

yq@ubuntu:~/kernelProgram$ make
make -C /lib/modules/4.10.0-28-generic/build M=/home/yq/kernelProgram modules
make[1]: Entering directory '/usr/src/linux-headers-4.10.0-28-generic'
  CC [M]  /home/yq/kernelProgram/kernel_hello.o
In file included from ./include/linux/module.h:18:0,
                 from /home/yq/kernelProgram/kernel_hello.c:2:
./include/linux/moduleparam.h:21:1: error: expected ‘,’ or ‘;’ before ‘static’
 static const char __UNIQUE_ID(name)[]       \
 ^
./include/linux/module.h:161:32: note: in expansion of macro ‘__MODULE_INFO’
 #define MODULE_INFO(tag, info) __MODULE_INFO(tag, tag, info)
                            ^
/home/yq/kernelProgram/kernel_hello.c:6:1: note: in expansion of macro ‘MODULE_AUTHOR’
 MODULE_AUTHOR("YANQIN")
 ^
In file included from /home/yq/kernelProgram/kernel_hello.c:2:0:
/home/yq/kernelProgram/kernel_hello.c: In function ‘__inittest’:
/home/yq/kernelProgram/kernel_hello.c:19:13: error: ‘hello_init’ undeclared (first use in this function)
 module_init(hello_init);
         ^
./include/linux/module.h:131:11: note: in definition of macro ‘module_init’
  { return initfn; }     \
       ^
/home/yq/kernelProgram/kernel_hello.c: At top level:
./include/linux/module.h:132:6: error: ‘init_module’ aliased to undefined symbol ‘hello_init’
  int init_module(void) __attribute__((alias(#initfn)));
  ^
/home/yq/kernelProgram/kernel_hello.c:19:1: note: in expansion of macro         
‘module_init’
 module_init(hello_init);
 ^
scripts/Makefile.build:301: recipe for target                                     
'/home/yq/kernelProgram/kernel_hello.o' failed
make[2]: *** [/home/yq/kernelProgram/kernel_hello.o] Error 1
Makefile:1524: recipe for target '_module_/home/yq/kernelProgram' failed
make[1]: *** [_module_/home/yq/kernelProgram] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.10.0-28-generic'
Makefile:7: recipe for target 'default' failed
make: *** [default] Error 2

我不知道为什么它说linux内核头有错误

标签: linuxubuntubuildkernel

解决方案


调用MODULE_LICENSE和其他MODULE_*宏,设置有关模块的信息,应该用;常见的 C 指令终止


推荐阅读