__decspec(selectany)

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

可以使用__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;

 

以上

转载于:https://www.cnblogs.com/wangpei0522/p/4801714.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值