You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在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);

问题修复

  1. 核心错误解决:MODULE_PARM_DESC(my_int,"A Integer.")这一行末尾缺少分号;,宏展开后破坏了后续代码的语法结构,导致编译器处理moduleparam.h时抛出错误。补上分号即可解决该编译问题。
  2. 额外优化(避免运行时问题):
    • pr_info中的换行需用\n替代直接换行,否则会引发编译警告或日志格式混乱。
    • 打印long类型变量my_long时,格式化符应使用%ld而非%d,避免类型不匹配。
    • 代码中pr_info("god %d arguments...")存在拼写错误,god应改为Got。

内容的提问来源于stack exchange,提问作者Mheboobkhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 20:15:01