我正在尝试开发一些额外的 https://github.com/ffnord/alfred/blob/master/vis/vis.c 功能由于我不熟悉Linux列表(list.h),我尝试遵循此list.h教程。为此,我创建了一个非常简单的test.c文件,并且还导入了提到的batman/alfred的list.h文件(通过openmesh)。
Alfred/batman Github代码编译完美,但在示例代码中,GCC抱怨list.h。
Description Resource Path Location Type
expected ‘;’ before ‘}’ token list.h /C_Linux_kernel_lists/src line 68 C/C++ Problem
Description Resource Path Location Type
lvalue required as unary ‘&’ operand list.h /C_Linux_kernel_lists/src line 68 C/C++ Problem
所以我的问题是:为什么 GCC 不抱怨上游 list.h 代码,当我尝试使用相同的代码时它会返回我这些消息?
附源码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
struct Person
{
char name[30];
unsigned int weight;
unsigned char gender;
struct list_head list;
};
int main()
{
struct Person personList;
LIST_HEAD_INIT(&personList.list);
struct Person* aNewPersonPointer;
aNewPersonPointer = malloc(sizeof(*aNewPersonPointer));
strcpt(aNewPersonPointer->name, "roman10");
aNewPersonPointer->weight = 130;
aNewPersonPointer->gender = 1;
INIT_LIST_HEAD(&aNewPersonPointer->list);
list_add(&aNewPersonPointer->list, &personList.list);
return 0;
}
你应该打电话给INIT_LIST_HEAD
而不是LIST_HEAD_INIT
。 这只是基于alfred代码其余部分如何使用列表接口的猜测,LIST_HEAD_INIT
从未在list.h之外使用,但INIT_LIST_HEAD
在main.c
,recv.c
和vis/vis.c
中。
该教程的评论中指出了这一点。