1

I'm trying to use the Linux Kernel Linked List implementation but I am unable to compile. I'm following these sources exactly with no results (http://www.roman10.net/linux-kernel-programminglinked-list/ and http://kernelnewbies.org/FAQ/LinkedLists)

The list.h Kernel Macro for LIST_HEAD_INIT is as follows:

#define LIST_HEAD_INIT(name) { &(name), &(name) }


struct Node {
int data;
struct list_head list;
};

struct Node mylinkedlist;
LIST_HEAD_INIT(&mylinkedlist.list);    

void add(){
struct Node first;
first.data = 1;
first.list = LIST_HEAD_INIT(first.list);
list_add_tail(&first->list, &mylinkedlist.list);
return 0;
}

I keep getting: "error: expected identifier or '(' before '{'"

16
  • I have a sneaking suspicion as to what might be going on. Could we see where LIST_HEAD_INIT is defined? Commented Oct 29, 2013 at 0:17
  • Its defined outside of the function add() right above it. LIST_HEAD_INIT(&mylinkedlist.list). I'm under the impression that it's a Macro in list.h, correct? Commented Oct 29, 2013 at 0:22
  • Probably. What I wanted to see was the macro (not where it's used). Commented Oct 29, 2013 at 0:25
  • oh okay. @DennisMeng: #define LIST_HEAD_INIT(name) { &(name), &(name) } Commented Oct 29, 2013 at 0:27
  • @Alex In the question description if possible. Commented Oct 29, 2013 at 0:28

1 Answer 1

1

You are getting that wrong.
First, your should LIST_HEAD(mylinkedlist), not LIST_HEAD_INIT nor struct Node mylinkedlist.
mylinkedlist should be a standalone head of kernel linked list struct, it's used to link all list_head.

Second, you should INIT_LIST_HEAD(&first.list), this is the way to dynamically assignment; LIST_HEAD_INIT is used when structure is statically created at compile time.

Last, you should list_add_tail(&first.list, &mylinkedlist).

so the complete code should be:

LIST_HEAD(mylinkedlist);

void add(){
  struct Node first;
  first.data = 1;
  INIT_LIST_HEAD(&first.list);
  list_add_tail(&first.list, &mylinkedlist);
}

this code work fine for me.
I suggest you read Linux Kernel Development chapter 6, it explain this very well.

Sign up to request clarification or add additional context in comments.

3 Comments

I got an error at error: void value not ignored as it out to be at struct Node first = {1, INIT_LIST_HEAD(&first.list)};
Are you play another piece of code? if you are statically assign, use LIST_HEAD_INIT.
Adding link to the kernel-doc: kernel.org/doc/html/v6.0/core-api/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.