debug(fmt,args...)调试

本文探讨了在C/C++中如何定义和使用debug宏进行调试,通过比较不同宏定义方式,如`#define debug(fmt,args...) printf (fmt ,##args)`,解释了每个定义的优缺点。调试功能在GCC编译环境下有效,但在VC6.0下可能出现错误。" 101549914,7341847,Unity 2D物理关节详解,"['Unity开发', '2D游戏开发', '物理引擎', '游戏机制']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、定义宏(debug.h)

#ifndef __DEBUG__H
#define __DEBUG__H

#include <stdio.h> #ifdef DEBUG
#define debug(fmt,args...) printf (fmt ,##args) #else #define debug(fmt,args...) #endif /* DEBUG */ #endif /* __DEBUG__H */

  为何是“#define debug(fmt,args...)    printf (fmt ,##args)”这种宏定义方式呢?

<1> #define debug(fmt,args)    printf (fmt ,##args)

  当不定参数多于1个,会提示参数参数多于agrs对应的参数

<2> #define debug(fmt,...)    printf (fmt ,##args)

  这种情况agrs找不到对应项

<3> #define debug(fmt,...)    printf (fmt ,...)

  这种情况下,不定参数在预编译时全部被替换成“...”

<4> #define debug(fmt,args...)    printf (fmt ,args)

   我测试了,也是可以的

<5> #define debug(fmt,args)    printf (fmt ,##args)

   u-boot使用的方法

2、调用程序(main.c)

#include "debug.h"
//#define DEBUG

int main(void)
{
    debug("File :%s Funtion :%s Line : %d\n", __FILE__, __FUNCTION__, __LINE__ );
    return 0;
}

  在main.c中,可以通过是否定义DEBUG决定是否使能本文件中的调试函数。

后记

  本功能在gcc编译环境下好使,但在VC6.0下提示“debug(fmt,args...) ”错误。

 

参考网页:Linux C 中连接操作符##  

    define小结 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值