IAR-ARM 带变参数宏定义

来源:互联网 发布:java毫秒数转换成时间 编辑:程序博客网 时间:2024/04/19 16:22

在C99中已经有对可变参数宏的扩展,在IAR的帮助文档中,有这样的描述  __VA_ARGS__ :

 

__VA_ARGS__

Syntax

#define P(...)       __VA_ARGS__#define P(x, y, ...)   x + y + __VA_ARGS__

__VA_ARGS__ will contain all variadic arguments concatenated, including the separating commas.

Description

Variadic macros are the preprocessor macro equivalents of printf style functions. __VA_ARGS__ is part of the C99 standard.

Example

#if DEBUG  #define DEBUG_TRACE(S, ...) printf(S, __VA_ARGS__)#else  #define DEBUG_TRACE(S, ...)#endif/* Place your own code here */DEBUG_TRACE("The value is:%d/n",value);

will result in:

printf("The value is:%d/n",value);
    这样子,我们在进行可变参数传递的时候,可以参数上面的方案。但是,上的处理方法,在IAR ARM 4.X的环境中会有问题,当我们禁用DEBUG的时候,编译有时候会出错:
比如:
       DEBUG_TRACE("This is a ideal day!");,
      这样语句会报错,报错类型为参数不对。在DEBUG_TRACE所要求的参数与这条语句中的参数不一样。
   
      DEBUG_TRACE("This is number %d", value);
      上面的语句就不会报错。

这样,我们从于调试考虑的宏定义就没有多大的意义了,因为不好禁用调试。

 

但是,有另外一种方案,这样方案是一种技巧性的:

 

#if DEBUG

#define DEBUG_TRACE(args)    (printf args)

#else

#define DEBUG_TRACE(args)

#endif

 

但是在调用DEBUG_TRACE时候,要用两个引号:

DEBUG_TRACE(("This is a good day!"));

DEBUG_TRACE(("This is number %d",value));

这样是可用的。太牛B了,当DEBUG使能的时候,其为可变参数,当DEBUG禁能的时候,其为固定参数,因而可以通过编绎链接!