【发布时间】:2018-01-29 01:54:52
【问题描述】:
所以我在 c 中有一个链表。我正在尝试使用 Linux 中的模块对其进行编译。所以在我的链表中,我输入了 6 个人。有月、日、年和名字。在程序中,我输入了 6 个人和属性。当我尝试横向链接列表时,它没有正确编译。我的代码几乎完成了,但不知道我在哪里搞砸了。
我收到的错误消息:
implicit declartion of funciton 'print'
assignment from incompatible pointer type
expected ; before while
ISO c90 forbids mixed declarations and code
expected expression before',' token
'next' undeclared
invalid type argument of unary '*"
compiling through terminal and the erro i'm getting are: warning control reaches end non-void function. – black 7 mins ago
Expted declaration or statement at end of input. – black 7 mins ago
in expantion of macro 'Module_License – black 6 mins ago
warning 'alias' attribute ignored. – black 5 mins ago
Error: invalid storage class for function – black 5 mins ago
error: expected identifier before '=' token *birthday_list->NULL; – black 5 mins ago
error: struct birthday has no member named next – black 4 mins ago edit
You have a lot of mistake in your code such as ptr=&birthday_list (forgot semicolon in the end of the line. – ymonad just now
所以我试图让它到达输出的位置: It loads the module and states the six people then removes the people in the list.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/slab.h>
struct birthday
{
int month;
int day;
int year;
char *name;
struct list_head list;
};
/**
* The following defines and initializes a list_head object named birthday_list
*/
static LIST_HEAD(birthday_list);
int simple_init(void)
{
struct birthday *person;
/* Creating Person 1 */
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->month = 8;
person->day = 12;
person->year = 1993;
person->name = "Aaron";
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
/* Creating Person 2 */
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->month = 4;
person->day = 15;
person->year = 1993;
person->name = "Fish";
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
/* Creating Person 3 */
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->month = 3;
person->day = 29;
person->year = 1983;
person->name = "js";
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
/* Creating Person 4 */
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->month = 7;
person->day = 25;
person->year = 1999;
person->name = "Mark";
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
/* Creating Person 5 */
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->month = 7;
person->day = 19;
person->year = 1992;
person->name = "Leah";
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
/* Creating Person 6 */
person = kmalloc(sizeof(*person), GFP_KERNEL);
person->month = 1;
person->day = 11;
person->year = 1991;
person->name = "Han";
INIT_LIST_HEAD(&person->list);
list_add_tail(&person->list, &birthday_list);
printk(KERN_INFO "Loading Module\n");
struct birthday *ptr;
list_for_each_entry(ptr, &birthday_list, list) {
/* On each iteration ptr points */
/* to the next birthday struct */
if(ptr ==NULL){
print("List is empty");
}else{
ptr=&birthday_list
while(ptr!=NULL){
ptr=ptr->list;
}
}
return 0;
}
void simple_exit(void) {
printk(KERN_INFO "Removing Module\n");
struct birthday *ptr, *next
list_for_each_entry_safe(ptr, next, *birthday_list, list) {
while(ptr!=NULL){
ptr->next=*birthday_list->next;
*birthday_list->=NULL;
}
list_del(&ptr->list);
kfree(ptr);
}
}
module_init(simple_init);
module_exit(simple_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Kernel Data Structures");
MODULE_AUTHOR("SGG");
【问题讨论】:
-
你是怎么编译的,错误信息是什么?
-
通过终端编译,我得到的错误是:警告控制到达非无效函数。
-
输入结束时的扩展声明或语句。
-
您应该将错误消息粘贴到问题中,而不是作为评论。
-
在“我收到的错误消息:”部分下的问题中,您会得到一个垃圾,例如
black 6 mins ago。从编译器的输出中复制实际错误消息,将其粘贴到问题帖子中并格式化为代码。
标签: c linux linked-list linux-kernel