c++ - Issue with Headers and Global Variable -


so i'm having issue declaring global variable (in header) , using it. i've done reading on header files , how work, can't understand why code isn't working.

here's have (in simplified manner):

main:

// main.cpp  #include "source.hpp"  int main() {     return variable; } 

source:

// source.cpp  #include "source.hpp"  variable = 17; 

header:

// source.hpp  #ifndef __source_hpp_included__ #define __source_hpp_included__  extern int variable;  #endif  // __source_hpp_included__ 

i've tried , without extern in header file. error when try (compile in source): 'variable' not name type not understanding properly?

also, when declare same variable in main.cpp, compiler throw error 'redefining' variable. why that?

in source.cpp need define variable:

int variable = 17; 

Comments