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?

6
  • 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. Commented Mar 1, 2022 at 16:51
  • 1
    in cpp should be int somevariable = 0; Commented Mar 1, 2022 at 16:51
  • Do not use global variables! Never. This makes code fragile and hard to maintain. stackoverflow.com/questions/484635/are-global-variables-bad Commented Mar 1, 2022 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? Commented Mar 1, 2022 at 16:55
  • C++ is not scripting language, so you can't run code from global scope (expect for initializes). Commented Mar 1, 2022 at 16:56

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.