0

Considering the example below:

header.h

extern int somevariable;
    
void somefunction();

source.cpp

#include "header.h"

somevariable = 0; // this declaration has no storage class or type specifier

void somefunction(){
    somevariable = 0; // works fine
}

I couldn't find the answer to the following questions:

  1. Why can't I just forward declare int somevariable and initialize it in the .cpp file?

  2. Why is the somefunction() function able to "see" somevariable when declared using extern int somevariable, and why is it not accessible outside of a function?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
mooder
  • 341
  • 2
  • 8
  • 1
    Between "forward declare" and "initialize", there needs to be the step "define". `extern int somevariable;` says to look elsewhere for a definition of `somevariable`, `somevariable = 0;` assigns a value of `0` to it, but in neither case have you actually provided a definition. If you want the definition to reside in the cpp file, the line would be `int somevariable;`, or `int somevariable = 0;` if you want to define and initialize it together. – Nathan Pierson Mar 01 '22 at 16:51
  • 1
    in cpp should be `int somevariable = 0;` – Marek R Mar 01 '22 at 16:51
  • Do not use global variables! Never. This makes code fragile and hard to maintain. https://stackoverflow.com/questions/484635/are-global-variables-bad – Marek R Mar 01 '22 at 16:53
  • Thanks, I had confusion with forward declaration and definition. Forward declaration is just a "this exists". Only reason to forward declare would be to share the variable outside? – mooder Mar 01 '22 at 16:55
  • C++ is not scripting language, so you can't run code from global scope (expect for initializes). – Marek R Mar 01 '22 at 16:56
  • You can't assign to a variable outside of a function. You can _initialise_ it, but that's a different thing, and would, perforce, include defining it. So, what @MarekR says in his first comment. – Paul Sanders Mar 01 '22 at 16:58

1 Answers1

3
extern int somevariable;

means definition of somevariable is located somewhere else. It is not forward declaration. Forward declaration is used in context of classes and structs.


somevariable = 0; 

is invalid since this is assignment and you can't run arbitrary code in global scope.
It should be:

int somevariable = 0; 

and this means define (instantiate) global variable somevariable in this translation unit and initialize it to 0. Without this statement linking of your program will fail with error something like: undefined reference to 'somevariable' referenced by ... .


Use of global variable in any langue is considered as bad practice, sine as project grows global variable makes code impossible to maintain and bug-prone.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • I understand that global variable is a sin. I used that as an example to make it easier to understand. So I wrote `int somevariable;` in the header file and then `int somevariable = 0;` and I get that it's defined multiple time. I got the header guard to prevent multiple import... why is it happening? I did the test with a `class objA` defined in header1, then in header2 I write `objA instanceA;` and then in the cpp same but I again get the error. – mooder Mar 01 '22 at 19:19