在我对内核程序的经验有限的情况下,我的操作系统是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内核头有错误
答案 0 :(得分:0)
调用 MODULE_LICENSE
和其他 MODULE_*
宏,其中设置有关模块的信息,应以 {{1像常见的C指令一样