0

I understand why this won't link:

extern bool g_WinGame;
...
g_WinGame=true;

But why does this compile and link?

extern bool g_WinGame=false;
...
g_WinGame=true;

I'm using MSVC 2010

[edit] all is explained HERE

Community
  • 1
  • 1
Valmond
  • 2,897
  • 8
  • 29
  • 49
  • The `extern` in the second example can be ignored. It is equivalent to `bool g_WinGame = true` – David G Sep 10 '15 at 15:17
  • Well yeah, that's the question, why is it legal? As it seems legal to declare extern and then it isn't... – Valmond Sep 10 '15 at 17:35
  • 1
    Because you can use extern to override internal linkage when using `const`. For example, in namespace scope `int const x = 5` has internal linkage but `extern int const x = 5` has external linkage. – David G Sep 10 '15 at 19:32
  • That should have been the correct answer. Didn't know about internal/external linkage, a quick search provided me with all info needed. Thanks! – Valmond Sep 10 '15 at 21:42
  • Please don't edit answers into the question – M.M Sep 10 '15 at 22:31

1 Answers1

3
extern bool g_WinGame;

is a declaration.

extern bool g_WinGame=false;

is a definition. Here extern is redundant but legal.

R Sahu
  • 204,454
  • 14
  • 159
  • 270