Search code examples
cexternundefined-referencevariable-declaration

Undefined reference on compile


I am trying to use a variable I have already declared on a .h file on a .c file and i gives me a compile error:

undefined reference to var

this is the mach.c content:

#include "machheader.h"

int 
main( void )
{
    var = 1;
    printf("Variable %d\n", var);
}

And my machheader.h contains only this:

extern int var;

Any idea?


Solution

  • In your case,

     extern int var;
    

    is a declaration, not a definition. You need to have a definition of var in your code.