【CPP】__declspec(selectany) 用法

__declspec是一个Microsoft Visual C++特定的编译器属性开关。括号中指明的是哪一个属性生效。关于__declspec的其他属性可以百度“__declspec msdn”查看微软的官方帮助。

Tells the compiler that the declared global data item (variable or object) is a pick-any

COMDAT (a packaged function). At link time, if multiple a definitions of COMDAT are

seen, the linker picks one and discards the rest. Selectany can be used in initializing

global data defined by headers,when the same header appears in more than one

source file.

简单翻译一下:告诉编译器定义的全局数据项(变量或对象)这是一套能被任意挑选的COMDAT(一套函数)。在链接时,如果多个COMDAT的定义被找到,链接器将挑选一个并剔除其他的多余的。Selectany可以被用于当定义有初始化全局变量数据的头文件被应用于多于一个的源文件时。

我这样的翻译还是挺虚的,直白了说:当在头文件定义全局变量,并且这个头文件被include多次时可以用这个开关剔除由于多次include而产生的重定义。


【CPP】__declspec(selectany) 用法
https://hodlyounger.github.io/B_Code/CPP/【CPP】__declspec(selectany)/
作者
mingming
发布于
2024年9月5日
许可协议