这是我的简单驱动程序 Makefile:
obj-m+=hello_world_module.o
KDIR=/lib/modules/$(shell uname -r)/build
all:
make -C $(KDIR) M=$(shell pwd) modules
clean:
make -C $(KDIR) M=$(shell pwd) clean
驱动程序代码为:
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/module.h>
static int __hello_world_init(void)
{
printf(KERN_INFO "Hello world\n");
printf(KERN_INFO "module inserted\n");
return 0;
}
void __exit hello_world_exit(void)
{
printf(KERN_INFO "module removed\n");
printf("exit\n");
}
module_init(hello_world_init);
module_exit(hello_world_exit);
MODULE_AUTHOR("Sridhar G");
当我运行时sudo make
出现此错误:
make -C /lib/modules/4.15.0-30-generic/build M=/home/sridhar/Documents
/DD/hello_world_module modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-30-generic'
CC [M] /home/sridhar/Documents/DD/hello_world_module/hello_world_module.o
cc1: error: code model kernel does not support PIC mode
scripts/Makefile.build:339: recipe for target '/home/sridhar/Documents
/DD/hello_world_module/hello_world_module.o' failed
make[2]: *** [/home/sridhar/Documents/DD/hello_world_module
/hello_world_module.o] Error 1
Makefile:1552: recipe for target '_module_/home/sridhar/Documents
/DD/hello_world_module' failed
make[1]: *** [_module_/home/sridhar/Documents/DD/hello_world_module] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-30-generic'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2