关于gcc 5.5.0关于宏展开的BUG

gcc版本:5.5.0

vs版本:vs 2015

如下宏定义代码

#define LOGDEBUG(format, ...) fprintf(stdout, "DEBUG file:%s, line:%d, msg: " format"\n", __FILE__, __LINE__, ##__VA_ARGS__)
//#define LOGDEBUG(format, args...) fprintf(stdout, "DEBUG file:%s, line:%d, msg: " format"\n", __FILE__, __LINE__, ##args)
#define LOGINFO(format, args...) fprintf(stdout, "INFO file:%s, line:%d, msg: " format"\n", __FILE__, __LINE__, ##args)
#define LOGWARN(format, args...) fprintf(stderr, "WARN file:%s, line:%d, msg: " format"\n", __FILE__, __LINE__, ##args)
#define LOGERROR(format, args...) fprintf(stderr, "ERROR file:%s, line:%d, msg: " format"\n", __FILE__, __LINE__, ##args)
#define LOGFATAL(format, args...) fprintf(stderr, "FATAL file:%s, line:%d, msg: " format"\n", __FILE__, __LINE__, ##args)

VS和GCC都可以编译通过的。

而如下代码gcc是无法编译通过的,vs是能正常编译通过的。(与上份代码的区别,只是format前面少了一个空隔)

#define LOGDEBUG(format, ...) fprintf(stdout, "DEBUG file:%s, line:%d, msg: "format"\n", __FILE__, __LINE__, ##__VA_ARGS__)
//#define LOGDEBUG(format, args...) fprintf(stdout, "DEBUG file:%s, line:%d, msg: " format"\n", __FILE__, __LINE__, ##args)
#define LOGINFO(format, args...) fprintf(stdout, "INFO file:%s, line:%d, msg: "format"\n", __FILE__, __LINE__, ##args)
#define LOGWARN(format, args...) fprintf(stderr, "WARN file:%s, line:%d, msg: "format"\n", __FILE__, __LINE__, ##args)
#define LOGERROR(format, args...) fprintf(stderr, "ERROR file:%s, line:%d, msg: "format"\n", __FILE__, __LINE__, ##args)
#define LOGFATAL(format, args...) fprintf(stderr, "FATAL file:%s, line:%d, msg: "format"\n", __FILE__, __LINE__, ##args)

按照正常的宏定义的理解,format前面有无空隔均可以编译通过的。见如下代码:

	LOGDEBUG("helllo world.%d", 1);
    //compile okay
	fprintf(stdout, "DEBUG file:%s, line:%d, msg: ""helllo world.%d""\n", __FILE__, __LINE__, 1);
    //compile okay
	fprintf(stdout, "DEBUG file:%s, line:%d, msg: " "helllo world.%d""\n", __FILE__, __LINE__, 1);

 

另外,gcc 6.5.0版本是能够正常编译通过。所以我认为这是gcc 5.5.0的BUG,coder们遇到此问题无需纠结,解决方法就是在变量名前加一个空隔,原因可能把空隔当成了分隔符来处理了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值