编译示例内核模块时出错

时间:2019-11-23 06:52:50

标签: module kernel

我正在尝试使用《 Linux内核模块编程指南》中的init和cleanup函数的备用名称来编译示例模块,但我不断收到以下错误:

make -C /lib/modules/4.15.0-54-generic/build
 M=/media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3 modules
 make[1]: Entering directory '/usr/src/linux-headers-4.15.0-54-generic'
 CC [M] 
 /media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.o In
 file included from
 /media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.c:1:0:
 ./include/linux/module.h:129:42: error: redefinition of ‘__inittest’  
 static inline initcall_t __maybe_unused __inittest(void)  \
                                           ^ /media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.c:18:1:
 note: in expansion of macro ‘module_init’  module_init(hello_3_exit); 
 ^~~~~~~~~~~ ./include/linux/module.h:129:42: note: previous definition
 of ‘__inittest’ was here   static inline initcall_t __maybe_unused
 __inittest(void)  \
                                           ^ /media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.c:17:1:
 note: in expansion of macro ‘module_init’  module_init(hello_3_init); 
 ^~~~~~~~~~~
 /media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.c:
 In function ‘__inittest’:
 /media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.c:18:13:
 error: return from incompatible pointer type
 [-Werror=incompatible-pointer-types]  module_init(hello_3_exit);
              ^ ./include/linux/module.h:130:11: note: in definition of macro ‘module_init’   { return initfn; }     \
            ^~~~~~ /media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.c:
 At top level: ./include/linux/module.h:131:6: error: redefinition of
 ‘init_module’   int init_module(void) __attribute__((alias(#initfn)));
       ^ /media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.c:18:1:
 note: in expansion of macro ‘module_init’  module_init(hello_3_exit); 
 ^~~~~~~~~~~ ./include/linux/module.h:131:6: note: previous definition
 of ‘init_module’ was here   int init_module(void)
 __attribute__((alias(#initfn)));
       ^ /media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.c:17:1:
 note: in expansion of macro ‘module_init’  module_init(hello_3_init); 
 ^~~~~~~~~~~ cc1: some warnings being treated as errors
 scripts/Makefile.build:337: recipe for target
 '/media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.o'
 failed make[2]: ***
 [/media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3/hello-3.o]
 Error 1 Makefile:1552: recipe for target
 '_module_/media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3'
 failed make[1]: ***
 [_module_/media/aaron/GoogleDrive/School/CSC239/project/LKM/hello3]
 Error 2 make[1]: Leaving directory
 '/usr/src/linux-headers-4.15.0-54-generic' Makefile:4: recipe for
 target 'all' failed make: *** [all] Error 2

这是我的来源和制作文件:

#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h>

static int hello3_data __initdata = 3;

static int __init hello_3_init(void)
{
        printk(KERN_INFO "Hello world %d.\n", hello3_data);
        return 0;
}
static void __exit hello_3_exit(void)
{
        printk(KERN_INFO "Goodbye world 3.\n");
}

module_init(hello_3_init);
module_init(hello_3_exit);

Makefile:

obj-m := hello-3.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

我在做什么错?似乎init.h中存在错误,但我无法对其进行编辑。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

看看最后几行:

module_init(hello_3_init);
module_init(hello_3_exit);

您使用过module_init两次。

相反,您想使用module_exit

module_init(hello_3_init);
module_exit(hello_3_exit);

希望我回答这个还不算太晚!