Compiler Error C2496

'identifier' : 'selectany' can only be applied to statically initialized data items with external linkage

The selectany extended attribute can only be applied to the static initialization of externally visible and global data items. See the following example:

// OK
__declspec(selectany) int x1 = 1;

// error - x2 is not externally visible
const __declspec(selectany) int x2 = 2;

// OK
extern const __declspec(selectany) int x3 = 3;

// error - x4 is not initialized
__declspec(selectany) int x4;
// error - dynamic initialization of x5
__declspec(selectany) int x5 = foo();
// error - x6 is not externally visible
static __declspec(selectany) int x6 = 6;

extern const int x7;
// OK - redeclaration of x7 that is extern
const __declspec(selectany) int x7 = 7;