xtaens 发表于 2014-11-27 15:24:30

哪位可以把一个单词宏替换成//,如此非常有利于程序调试!

本帖最后由 xtaens 于 2014-11-27 15:37 编辑

如题,移植的一个函数库中N多文件使用了大量的 DEBUG(XXXXX);输出语句(这些语句都在同一行),我不想要这个语句了,又不想一句一句的删除,又不想用查找替换掉,还想有的时候可以把调试打开,输出一些参数。


它的原样子是这样的:#define DEBUGprintf

想了一下似乎可以改改,即用 #define DEBUG//printf

编译提示:Warning: expression has no effect


怎么才能把那个DEBUG用宏替换成//呢,这样的话整行就用//屏蔽,这样就不编译了,下次使用的时候再修改一下宏,这样是不是很好呢?


比如下面部分语句,前提是不用去修改原来的文件,即使去修改,那好几百的,够你去弄了

switch (bw)
          {
            case NA_80MHz:
            DEBUG("New Mode 80 Mhz ");
            break;
            case NA_22MHz:
            DEBUG("New Mode 22 Mhz ");
            break;
            default:
            DEBUG("Unknown mode ??? ");
            break;
          }
          switch (sd)
          {
            case NA_1us:
            DEBUG("1 us, ");
            break;
            case NA_4us:
            DEBUG("4 us, ");
            break;
            default:
            break;
          }
          switch (br)
          {
            case NA_250k_S:
            DEBUG("250 kSym\r\n");
            break;
            case NA_1M_S:
            DEBUG("1 MSym\r\n");
            break;
            default:
            break;
          }
          break;

dr2001 发表于 2014-11-27 15:24:31

本帖最后由 dr2001 于 2014-11-27 15:58 编辑

如果你用C99编译器的话,定义修改为:
#define DEBUG(...)
即可。

如果你用的是C90的编译器,不支持变参数宏的话,没有好办法,必须调用空的inline函数,然后让编译器优化掉。比如:
static inline int _dummy_printf(const char * fmt, ...) {
}
#define DEBUG _dummy_printf
自己替换inline为你编译的内联名称。

直接替换函数名是个比较2的写法,会造成很多麻烦事儿;尤其是printf这种,用到了不定长参数的情况。

wye11083 发表于 2014-11-27 15:27:23

#if() #else()
#if defined() #else()
注意需要常量。

jiaowoxiaolu 发表于 2014-11-27 15:27:40

直接把宏改为空格即可

dxgdsx 发表于 2014-11-27 15:28:47

#if 1
#define dmsg(x...) do{printf(x);}while(0)
#else
#define dmsg(x...)   
#endif

试一试这样行吗?

KongQuan 发表于 2014-11-27 15:29:12

把printf替换成自定义函数。这函数什么都不做。

kakarotto 发表于 2014-11-27 15:30:53

本帖最后由 kakarotto 于 2014-11-27 15:32 编辑

重写printf函数,改成

__inline void printf(void *s)
{

}

tairuibao 发表于 2014-11-27 15:32:23

//是编译器定义的,除了去搞编译器才能改;不过编译器你是改不了的。所以,你不能改。

xtaens 发表于 2014-11-27 15:32:23

wye11083 发表于 2014-11-27 15:27
#if() #else()
#if defined() #else()
注意需要常量。

这样太麻烦了,而且会修改原文件

ijlc1314 发表于 2014-11-27 15:34:40


直接
#define DEBUG    printf
改成
#define DEBUG

#define DEBUG    do{}while(0)

就可以了吧

tim 发表于 2014-11-27 15:35:51

查找 #define DEBUG
替换成 //#define DEBUG

需要恢复的时候再
查找 //#define DEBUG
替换成 #define DEBUG

xtaens 发表于 2014-11-27 15:36:15

jiaowoxiaolu 发表于 2014-11-27 15:27
直接把宏改为空格即可

不行,()里面的语句还会被编译的

Puppey 发表于 2014-11-27 15:36:19

一直用的条件编译!

xtaens 发表于 2014-11-27 15:37:11

tim 发表于 2014-11-27 15:35
查找 #define DEBUG
替换成 //#define DEBUG



源文件有10多个,每个文件都有,替换不是个好办法

xtaens 发表于 2014-11-27 15:39:23

Puppey 发表于 2014-11-27 15:36
一直用的条件编译!

我也一直用条件编译,但这个文件是别人写的,没有用

lihuaping0357 发表于 2014-11-27 15:39:48

        #define DEBUG(a)((void)0)

xtaens 发表于 2014-11-27 15:41:40

KongQuan 发表于 2014-11-27 15:29
把printf替换成自定义函数。这函数什么都不做。

字符串还好弄,那个参数怎么办?

dxgdsx 发表于 2014-11-27 15:46:32

#if 1
#define DEBUG(x...) do{printf(x);}while(0)
#else
#define DEBUG(x...)   
#endif

好吧,我再打一遍。

xtaens 发表于 2014-11-27 15:47:12

收到一个:需要重写   NoDebugging(args, ...)

dxgdsx 发表于 2014-11-27 15:49:12

楼主如果觉得调试信息都在同一行不方便查看,可以试一下这样。
#if 1
#define DEBUG(x...) do{printf("\n"); printf(x);}while(0)
#else
#define DEBUG(x...)   
#endif

KongQuan 发表于 2014-11-27 15:49:53

xtaens 发表于 2014-11-27 15:41
字符串还好弄,那个参数怎么办?

#include <string.h>
#include <stdio.h>
#include <stdarg.h>


int MyPrintf ( const char * format, ... );


#define Debug myPrintf

int MyPrintf ( const char * format, ... )
{
        returen 0;
}

dxgdsx 发表于 2014-11-27 15:52:17

xtaens 发表于 2014-11-27 15:41
字符串还好弄,那个参数怎么办?

楼主你上面提到的,19楼的代码都可以满足的。

Gallen.Zhang 发表于 2014-11-27 15:55:20

工程中验证过的,见图所示

takashiki 发表于 2014-11-27 15:58:32

C99版,很简洁:
#if 1
#define DEBUG printf
#else
#define DEBUG(...)
#endif

xtaens 发表于 2014-11-27 15:58:40

KongQuan 发表于 2014-11-27 15:49
#include
#include
#include


这个测试通过啦

lusson 发表于 2014-11-27 16:00:46

#define DEBUG(xxx) /##/
这样行吗?或者
#define DEBUG(xxx) /#/

不记得是哪个了

xtaens 发表于 2014-11-27 16:03:02

takashiki 发表于 2014-11-27 15:58
C99版,很简洁:

测试通过,非常感谢

xtaens 发表于 2014-11-27 16:05:07

ijlc1314 发表于 2014-11-27 15:34
直接
#define DEBUG    printf
改成


do{}while(0)不行

空格 也不行

xtaens 发表于 2014-11-27 16:06:07

lusson 发表于 2014-11-27 16:00
#define DEBUG(xxx) /##/
这样行吗?或者
#define DEBUG(xxx) /#/


这样不行的

我刚开始也用 # 和转义字符试了,都不行

xtaens 发表于 2014-11-27 16:07:21

Gallen.Zhang 发表于 2014-11-27 15:55
工程中验证过的,见图所示

可以使用

非常感谢

xtaens 发表于 2014-11-27 16:08:33

dr2001 发表于 2014-11-27 15:57
如果你用C99编译器的话,定义修改为:
#define DEBUG(...)
即可。


debug(...)就可以了。

为啥 debug (...)多了一个空格就不行了呢

xtaens 发表于 2014-11-27 16:12:11

dxgdsx 发表于 2014-11-27 15:46
好吧,我再打一遍。

测试通过,多谢

括号里多了一个x也可以

这个x干什么用的呢

xtaens 发表于 2014-11-27 16:28:33

感谢楼上各位的回答,好多人的办法都可以使用。

分数给   dr2001吧,他回答的比较全面一些

总结:这种情况可以使用2个办法解决:

1:直接用debug(...)或 debug(x...) 或 debug(args, ...)    或 debug(args, arg...)替换----------------推荐

2、用自己的函数名替换,然后重写该函数


谢谢各位啦

gdoujiajia 发表于 2014-11-27 17:32:29

学习了!

L7科创 发表于 2014-11-27 17:40:25

学习了{:sad:}

sxmilovebb2 发表于 2014-11-27 17:53:36

xtaens 发表于 2014-11-27 15:37
源文件有10多个,每个文件都有,替换不是个好办法

IAR
SI
都可以集体操作哦。几个文件几十个都可以一瞬间搞定

1a2b3c 发表于 2014-11-28 13:20:58

我以前是这样 #define DEBUG /\/ 注意最后一个/要换行到第二行,手机没有回车键,晕

sxmilovebb2 发表于 2014-12-1 12:12:47

si的


页: [1]
查看完整版本: 哪位可以把一个单词宏替换成//,如此非常有利于程序调试!