c y*_* yj 3 c++ undef c-preprocessor
我遵循了在线教程,并想用它#undef来设计我的调试输出功能。我写了一个debugOut.h文件。内容如下?
#include <stdio.h>
#define NOOP //(void(0))
#undef DEBUG_PRINT
#if DEBUG_OUT
#define DEBUG_PRINT printf
#else
#define DEBUG_PRINT(...) NOOP
#endif
#undef DEBUG_OUT
Run Code Online (Sandbox Code Playgroud)
然后我写了一个main函数来测试我的设计是否正确。
#include<iostream>
#include "Header/debug_out.h"
#define DEBUG_OUT
int main(){
DEBUG_PRINT("module1 debug...\n");
printf("hello,world");
}
Run Code Online (Sandbox Code Playgroud)
但输出结果仅为hello, world. 为什么我定义#define DEBUG_OUT,为什么DEBUG_PRINT不替换为printf
我是根据在线教程编写的。我想基于此为 C++ 编写一个输出函数。但在句子中#define DEBUG_PRINT(...) NOOP,(...)代表什么?有什么办法可以输出宏定义被替换的内容吗?
预处理器基本上从上到下扫描输入。所以它首先处理#if DEBUG_OUT包含的 from #include "Header/debug_out.h",然后才处理#define DEBUG_OUT.
DEBUG_OUT在处理 的内容之前,您需要确保已定义Header/debug_out.h。以下应该工作:
#include<iostream>
#define DEBUG_OUT // first define DEBUG_OUT
#include "Header/debug_out.h" // when this is processed DEBUG_OUT is defined
int main(){
DEBUG_PRINT("module1 debug...\n");
printf("hello,world");
}
Run Code Online (Sandbox Code Playgroud)
另外在“Header/debug_out.h”中有一个错字:
#if DEBUG_OUT
Run Code Online (Sandbox Code Playgroud)
应该
#ifdef DEBUG_OUT
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |