2016-03-08 19 views
4

我試圖開發一些額外的功能https://github.com/ffnord/alfred/blob/master/vis/vis.c 因爲我不熟悉Linux列表(list.h),我試圖按照this list.h tutorial。爲此,我創建了一個非常簡單的test.c文件,並且還導入了batman/alfred提到的list.h file(通過openmesh)。list.h語法錯誤,只有當我使用我的C項目中的代碼

阿爾弗雷德/蝙蝠俠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; 


} 
+0

你應該提供一個[mcve]來獲得這樣的幫助。 –

+0

@GilHamilton done –

+0

FWIW鏗鏘也不喜歡那條線。 'test.c:18:5:錯誤:不能取'struct list_head *''類型的右值的地址。 – Schwern

回答

2

我相信你應該打電話INIT_LIST_HEAD,而不是LIST_HEAD_INIT。這只是基於alfred代碼的其餘部分如何使用列表界面的猜測,LIST_HEAD_INIT從不在list.h外使用,但INIT_LIST_HEADmain.c,recv.cvis/vis.c

這是pointed out in a comment on that tutorial

+1

現貨。 'LIST_HEAD_INIT'被用作一個直接初始化程序(而不是初始化*調用*)。唯一可以這樣使用的方法是,如果你只是聲明一個裸列表頭,在這種情況下,你應該使用'LIST_HEAD(name)',它已經使用'LIST_HEAD_INIT',所以你不需要。 –

相關問題