Global Constants in C++

C++ global constants have static linkage. This is different than C. If you try to use a global constant in C++ in multiple files you get an unresolved external error. Global constants are optimized out by the compiler, leaving no space reserved for the variable. One alternative is to include the const initializations in a header file and include that header in your CPP files when necessary,  just as if it was function prototype. Another possibility is to make the variable non-constant and use a constant reference when assessing it. The following code illustrates this error.

MAIN.CPP

void test(void);
const int lnktest1 = 0;

void main(void)
{

test( );

}

TEST.CPP

extern int lnktest1;

void test(void)
{
  int i = lnktest1;   // Causes LNK2001 reason c
}