Share via


selectany (Windows CE 5.0)

Send Feedback

The __declspec( selectany ) extended storage-class modifier tells the compiler that the declared global data item (variable or object) is a pick-any COMDAT item.

__declspec( selectany )declarator

At link time, if multiple definitions of a COMDAT are seen, the linker picks one and discards the rest.

If the linker option /OPT:REF (Optimizations) is selected, COMDAT elimination occurs to remove all unreferenced data items in the linker output.

For dynamically-initialized, global objects, selectany discards an unreferenced object's initialization code.

A global data item can normally be initialized only once in an EXE or DLL project.

selectany can be used in initializing global data defined by headers, when the same header appears in more than one source file.

selectany is available in both the C and C++ compilers.

Note   selectany can only be applied to the initialization of global data items that are externally visible.

Example

The following code example shows how to use the selectany attribute:

//Correct - x1 is initialized and externally visible 
__declspec(selectany) int x1=1;

//Incorrect - const is by default static in C++, so 
//x2 is not visible externally (This is OK in C, since
//const is not by default static in C)
const __declspec(selectany) int x2 =2;

//Correct - x3 is extern const, so externally visible
extern const __declspec(selectany) int x3=3;

//Correct - x4 is extern const, so it is externally visible
extern const int x4;
const __declspec(selectany) int x4=4;

//Incorrect - __declspec(selectany) is applied to the uninitialized
//declaration of x5
extern __declspec(selectany) int x5;

// OK: dynamic initialization of global object
// compile with cl /c
class X {
public:
X(int i){i++;};
int i;
};

__declspec(selectany) X x(1);

See Also

Compiler Options | __declspec

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.