码迷,mamicode.com
首页 > 其他好文 > 详细

__decspec(selectany)

时间:2015-09-11 19:16:36      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

我们在使用宏定义一类的技术时,容易发生符号的重定义,特别在这个符号是全局变量时。

可以使用__decspec(selectany)提示编译器,可以重定义此符号。

1.cpp

int sym = 1;

int main()

{

...

}

2.cpp

int sym = 1;

void function()

{...}//only used to prompt the sym is global symbol 

编译器会提示,符号重定义。


 

我们两种解决方案:

1.cpp

int sym = 1;

int main()

{

...

}

2.cpp

extern int sym;

void function()

{...}//only used to prompt the sym is global symbol 

第一个方法就是不要重定义咯~


 

1.cpp

extern __declspec(selectany)  int sym = 1;

int main()

{

...

}

2.cpp

extern __declspec(selectany) int sym = 1;

void function()

{...}//only used to prompt the sym is global symbol 

这个时候编译器会任选一个sym的定义作为sym的定义语句。

所以,我们通常这么也这么定义:

extern const __declspec(selectany) int sym = 1;

 

以上

__decspec(selectany)

标签:

原文地址:http://www.cnblogs.com/wangpei0522/p/4801714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!