Elsban
Elsban

Reputation: 1297

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?

Upvotes: 0

Views: 121

Answers (2)

Clifford
Clifford

Reputation: 93476

"undefined reference" is a linker error rather than a compiler error. You need to link the object code containing the instantiation of var which must be separately compiled or provided by a library.

Upvotes: 1

Sourav Ghosh
Sourav Ghosh

Reputation: 134316

In your case,

 extern int var;

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

Upvotes: 5

Related Questions