尝试创建一个宏,可以在定义DEBUG时用于打印调试消息,如下面的伪代码:
#define DEBUG 1
#define debug_print(args ...) if (DEBUG) fprintf(stderr, args)
Run Code Online (Sandbox Code Playgroud)
如何用宏实现这一目标?
浏览gcc编译器源代码(gcc/c-family/c-pragma.c)时,我看到:
typedef struct GTY(()) align_stack {
int alignment;
tree id;
struct align_stack * prev;
} align_stack;
Run Code Online (Sandbox Code Playgroud)
而且无论我有多少C编程年,我(())都知道这些:我还完全不知道.有人可以解释一下他们的意思吗?谷歌似乎没有找到它.
我在程序中使用cout语句进行调试.我想创建一个像它一样工作的函数,或者像printf一样工作,但是对全局变量很敏感.如果此全局变量为true,则它将打印到屏幕.如果它是假的,那么它将不会打印任何东西.是否已经有这样的功能?如果没有,那怎么可以呢?
我有三个文件如下
Test.cpp
void helloworld()
{
disable pf;
pf.Disable();
printf("No statement \n");
}
int main()
{
disable dis;
helloworld();
printf("Hello World");
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
disable.cpp
#include "StdAfx.h"
#include "disable.h"
disable::disable(void)
{#define printf(fmt, ...) (0)}
disable::~disable(void)
{}
void disable::Disable()
{
#define printf(fmt, ...) (0)
}
Run Code Online (Sandbox Code Playgroud)
disable.h
#pragma once
class disable
{
public:
disable(void);
~disable(void);
void Disable();
};
Run Code Online (Sandbox Code Playgroud)
执行后,我得到输出为No Statement Hello World.但我想two printf statements通过调用Disable function和禁用它们disable constructor..请帮助我为什么它不工作以及如何解决这个问题.请帮忙.
但是,如果我愿意,事情就可以了
main()
{
#define printf(fmt, ...) (0)
printf("Hello …Run Code Online (Sandbox Code Playgroud)