在学习写Linux模块驱动的时候写了个demo,分别在自己的电脑和RockPi4B上分别尝试运行,在自己电脑上能顺利运行,但是在RockPi4B上编译时却发生如下报错
编译报错
include/linux/module.h:130:6: warning: 'init_module' specifies less restrictive attribute than its target 'demo_init': 'cold' [-Wmissing-attributes]error, forbidden warning:module.h:130130 | int init_module(void) __attribute__((alias(#initfn)));|^~~~~~~~~~~/home/rock/Linux_driver/chardev_hello/chardevbase.c:76:1: note: in expansion of macro 'module_init'76 | module_init(demo_init);| ^~~~~~~~~~~/home/rock/Linux_driver/chardev_hello/chardevbase.c:55:19: note: 'init_module' target declared here55 | static int __init demo_init(void)| ^~~~~~~~~In file included from /home/rock/Linux_driver/chardev_hello/chardevbase.c:2:include/linux/module.h:136:7: warning: 'cleanup_module' specifies less restrictive attribute than its target 'demo_exit': 'cold' [-Wmissing-attributes]error, forbidden warning:module.h:136136 | void cleanup_module(void) __attribute__((alias(#exitfn)));| ^~~~~~~~~~~~~~/home/rock/Linux_driver/chardev_hello/chardevbase.c:77:1: note: in expansion of macro 'module_exit'77 | module_exit(demo_exit);| ^~~~~~~~~~~/home/rock/Linux_driver/chardev_hello/chardevbase.c:65:20: note: 'cleanup_module' target declared here65 | static void __exit demo_exit(void)
查阅了一些社区资料后大概确定这个报错与Linux内核代码和编译器gcc-9有关系。
GCC 9.0: ‘init_module’ specifies less restrictive attribute than its target
这个Bug貌似在Linux内核代码 5.0后的某个版本修复了,而我电脑上的内核代码版本为 5.13.0-40,RockPi4B上的内核代码为 4.4.154-116,所以会发生如上问题。
由于RockPi的官方并没有在RockPi4B上出新的内核版本系统,所以我直接选择更换make时使用的编译器
make CC=gcc-8#指定使用gcc-8编译器
或者在Makefile首行中添加
CC := gcc-8
最后编译成功