Header files are usually not translation units but meant to be included by them. That’s why header files usually do not “define” variables, since this would lead to multiple definition errors when the header file is included by different translation units (thereby re-defining the variable again and again).
That’s where “extern” comes into place, since this is for just “declaring” a variable without “defining” it. “extern” means “will be defined in some other translation unit”.
So the usual way is:
// Test.h
extern int externalint; // just declares externalint
// Test.cpp
int externalint = 10; // defines externalint
// main.cpp
#include "Test.h" // importing the declaration of "externalint" defined elsewhere
int main() {
std::cout << externalint << std::endl;
}
solved No clue why extern is not working