1

"main.c"

#include "header.h"

int main()
{
    int ch;
    start = NULL;
    printf("Enter your choice:\n");
    printf("1 --> To create list\n");

    switch (ch)
    {
    case 1:
        start = create(start);
        break;
    }
}

"header.h"

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node NODE;

struct node
{
int info;
NODE* link;
};

extern NODE *start;

NODE* create(NODE*);
  • The error is undefined reference to 'start' in main, but i have already declared it the header file, and i have even included 'header.h' file in 'main.c' file.
alk
  • 69,737
  • 10
  • 105
  • 255
  • Are you sure the compiler is including the correct `header.h`? Can you write: `NODE *alter_ego = NULL;` in `main()` before you assign to `start`? If not, you've probably managed to include the wrong `header.h` despite your best intentions. – Jonathan Leffler Oct 15 '18 at 03:12
  • 1
    Are you getting this error when compiling or linking? – fghzxm Oct 15 '18 at 03:15
  • 2
    @FredMiller: Good catch — "undefined reference" is normally a linker error, and since `main.c` doesn't define `start`, unless there's another object file that does define it, the reported error message is inevitable. That's much more probable than my 'wrong header' suggestion. – Jonathan Leffler Oct 15 '18 at 03:19
  • 3
    In C a "declaration" is just a promise to the compiler that the thingy mentioned would be "defined" somewhere else, and be found by the linker on link-time, if the latter fails, the linker complains about an "undefined reference". – alk Oct 15 '18 at 11:18

1 Answers1

4

in header.h you have declared extern NODE *start

But the definition of start is not given.

You need to define start. Usually in some .c file. Probably in main.c.

NODE *start;   //in Global space, above main() function.

Also refer this answer for further information.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31