在Ubuntu编译内核模块时遇moduleparam.h语法错误求助
内核模块编译错误:expected ‘,’ or ‘;’ before ‘static’
编译内核模块时出现如下错误:
./include/linux/moduleparam.h:409:9: error: expected ‘,’ or ‘;’ before ‘static’
以下是待编译的代码:
#include <linux/module.h> #include <linux/printk.h> #include <linux/init.h> #include <linux/kernel.h> //This for an ARRAY_SIZE() #include <linux/moduleparam.h> //Module parameters #include <linux/stat.h> MODULE_LICENSE("GPL"); static short my_short = 1; static long my_long = 9999; static int my_int = 5; static char *my_string = "WH"; static int my_array[2] = {429,429}; static int arr_argc = 0; module_param(my_short,short,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); MODULE_PARM_DESC(my_short,"A short integer."); module_param(my_long,long,S_IRUSR); MODULE_PARM_DESC(my_long,"A long Integer."); module_param(my_int,int,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); MODULE_PARM_DESC(my_int,"A Integer.") module_param (my_string,charp,0000); MODULE_PARM_DESC(my_string,"A character string"); module_param_array (my_array,int, &arr_argc,0000); MODULE_PARM_DESC(my_array,"An Array of integer"); static int __init start_module(void) { int i; pr_info("Hello, World 5. ====================== "); pr_info("My_Short is a short integer %d ",my_short); pr_info("My_long is a long integer %d ",my_long); pr_info("My_int is a integer %d ",my_int); pr_info("My_string is a character string %s ",my_string); for (i=0;i < ARRAY_SIZE(my_array);i++) pr_info ("At index %d, we have %d in my_array ",i,my_array[i]); pr_info("god %d arguments in the my_array ",arr_argc); return 0; } static void __exit end_module(void) { pr_info("GoodBye! world 5 "); } module_init(start_module); module_exit(end_module);
问题修复
- 核心错误解决:
MODULE_PARM_DESC(my_int,"A Integer.")这一行末尾缺少分号;,宏展开后破坏了后续代码的语法结构,导致编译器处理moduleparam.h时抛出错误。补上分号即可解决该编译问题。 - 额外优化(避免运行时问题):
pr_info中的换行需用\n替代直接换行,否则会引发编译警告或日志格式混乱。- 打印
long类型变量my_long时,格式化符应使用%ld而非%d,避免类型不匹配。 - 代码中
pr_info("god %d arguments...")存在拼写错误,god应改为Got。
内容的提问来源于stack exchange,提问作者Mheboobkhan
相关产品推荐
相关产品推荐

