1

I would like to make a linked list globally available across multiple .c files.

I've read how to do this but I can't identify what is causing my problem.

I declare the variable with extern in LinkedList.h:

extern LinkedList* canQueue;

And then in main.c I initialise the variable by sending it to a function in LinkedList.c like so:

LinkedList *canQueue=createList();

This is the create function in LinkedList.c:

LinkedList* createList() /*creates empty linked list*/
  {
    LinkedList* myList;
    myList = malloc(sizeof(LinkedList));
    myList->head = NULL;
    return myList;
  }

I then want to use the canQueue in another file, cpu.c. I've included LinkedList.h in the cpu.c, so at this point the Linked List should be available here from what I know. But when I try to access it I get an error:

undefined reference to 'canQueue'

Have I missed something or done something wrong?

3
  • Also please not I have to use the global variable for this problem, so simply not using one is not a good solution for me. Commented May 3, 2013 at 7:25
  • 1
    You need to define your variable (once in a .c file, with no extern) in addition to declaring it. Commented May 3, 2013 at 7:32
  • Why do those ugly guys in robot outfits show up whenever I want to answer a question in a jiffy? Commented May 3, 2013 at 7:36

2 Answers 2

2

It seems that you simply don't define such a global variable. If this code compiles:

LinkedList *canQueue = createList();

then it's not a "global" (file-scope) variable. Define it at file scope and initialize it carefully so you don't shadow it with a local variable. All in all, do something like this:

// at global scope
LinkedList *canQueue;

void initialize() // or whatever
{
    canQueue = createList();
}
Sign up to request clarification or add additional context in comments.

9 Comments

Good point. Now that I've changed it to LinkedList *cpuQueue , I still get the undefined reference errors so I've done something wrong with the declaration.
@Dawson Definitely. Make sure it isn't static, it's actually declared as extern in the header, etc.
@Dawson "Now that I've changed it to LinkedList *cpuQueue" .. it's a different name ... where did you change it? in .h or .c?
sorry I mean canQueue, I changed the declaration in main.c
@H2CO3 Can you tell me something plain and simple.What storage class variables declared at file scope have? extern or static.I read in many places that global variables have extern storage class by default,but a 80k reputation user on SO tells me they have default static storage.
|
1

You need to assign a constant to a global variable when initializing it.And return of a function is not considered a constant.So the following will show error:

 LinkedList *canQueue=createList();

Edit I missed it that you have declared and initialized the pointer *canQueue at function scope instead of file scope.That goes against the very definition of a global variable.But there's one more catch.If you declare something like LinkedList *canQueue=createList(); at the file-scope, you'll get the following error:

     Initializer element not constant

Since the object will be "declared" at file scope, it has static storage duration. Initializers for objects of static storage duration must be constant expressions. The result of a function call is not a constant expression

2 Comments

It didn't error out - the problem is that canQueue wasn't really a "global" (file-scope) variable.
Oops, it missed it that he has defined the variable in main()!! LOL..There is no way it will be global.

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.