首页 > 解决方案 > 尝试加载模块,但是当尝试编译模块时,计算机指出几个结构未定义

问题描述

这个模块的目的是基本上做与你在 linux 命令行中键入 cat /proc/buddyinfo 时发生的事情相同的事情

代码

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/mmzone.h>

#define DRIVER_AUTHOR "Matthew James Barnes <mbarnes2k5@Gmail.com>"
#define DRIVER_DESC     "A driver that does the same thing as /proc/buddyinfo"

/***
 *GLOBAL VARIABLES
 ***/
extern struct pglist_data *first_online_pgdat(void);
extern struct pglist_data *next_online_pgdat(struct pglist_data*pgdat);
struct pglist_data *pgdat;
extern struct zone *zone;


int init_module(void)
{
int order;
for(pgdat = first_online_pgdat(); pgdat; pgdat = next_online_pgdat(pgdat))
{
for(zone = pgdat->node_zones; zone; zone = next_zone(zone)){
printk(KERN_INFO "Node %d, zone %8s ",pgdat->node_id, zone->name);
printk(KERN_INFO "Node %d", pgdat->node_id);
printk(KERN_CONT "Zone %s",zone->name);
for( order = 0; order < MAX_ORDER; ++order)
        printk(KERN_CONT "%6lu ", zone->free_area[order].nr_free);
printk(KERN_CONT "\n");
}
}
return 0;
}

void cleanup_module(void){
        printk(KERN_INFO "Zones have been feenished.\n");
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);

~                                                                                                                                                                                                                                                                                                                             
~                                                                                                                                                                                                                                                                                                                             
~                                                                                                                                                                                                                                                                                                                             
~                                                                                                                                                                                                                                                                                                                             
~                                                                                                                                                                                                                                                                                                                             
~                                                                                                                                                                                                                                                                                                                             
~                                        

我决定精简并在我的内核中创建一个 makefile 以及一个名为 module-edits 的文件夹。

在运行 makefile 时的模块编辑中,我遇到了编译错误。

命令行返回以下内容:

make -C /lib/modules/5.4.0-26-generic/build M=/usr/local/share/source_code/version1/module-edits modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-26-generic'
  Building modules, stage 2.
  MODPOST 1 modules
ERROR: "next_online_pgdat" [/usr/local/share/source_code/version1/module-edits/printbuddy.ko] undefined!

ERROR: "next_zone" [/usr/local/share/source_code/version1/module-edits/printbuddy.ko] undefined!

ERROR: "zone" [/usr/local/share/source_code/version1/module-edits/printbuddy.ko] undefined!

ERROR: "first_online_pgdat" [/usr/local/share/source_code/version1/module-edits/printbuddy.ko] undefined!
make[2]:  [scripts/Makefile.modpost:94: __modpost] Error 1
make[1]:  [Makefile:1632: modules] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-26-generic'
make:  [makefile:5: all] Error 2

现在,令人困惑的是,当我输入

#include <linux/mmzone.h>

有许多结构。

pgdat is a struct that allows access to the current page data.

*first_online_pgdat(void); is a function that points to the first page. 

*next_online_pgdat(struct pglist_data*pgdat); points to the next page.

struct pglist_data *pgdat; 

is basically the representation of my page.

and extern struct zone *zone is the representation of the zone type of the pages.

所有这些都包含在 mmzone 中,但好像 mmzone.h 中的函数甚至没有被引用。

我真的不明白发生了什么,但我会感谢有经验的 linux 程序员的帮助。

标签: clinuxstructmakefile

解决方案


推荐阅读