C语言 关闭 printf 语句的宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5765175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Macro to turn off printf statements
提问by system
What MACRO can be used to switch off printf statements, rather than removing them all for deployment builds, I just want to switch them off, skip them, ignore them.
什么 MACRO 可用于关闭 printf 语句,而不是将它们全部删除以进行部署构建,我只想关闭它们,跳过它们,忽略它们。
EDIT: I personally use gcc, but code is part of a larger project which will be compiled on a Panda board running Ubuntu.
编辑:我个人使用 gcc,但代码是一个更大项目的一部分,该项目将在运行 Ubuntu 的 Panda 板上编译。
回答by Alexander Gladysh
Not exactly what you ask for, but I use this construct in my code for debug output when I do not have a proper logging system handy:
不完全符合您的要求,但是当我手头没有合适的日志记录系统时,我会在我的代码中使用此构造进行调试输出:
#if 1
#define SPAM(a) printf a
#else
#define SPAM(a) (void)0
#endif
So I can do this all over my code
所以我可以在我的代码中做到这一点
SPAM(("foo: %d\n", 42));
and then disable all of them by changing 1to 0in #ifabove.
然后通过改变禁用所有的人都1以0在#if上面。
But if you have variadic macro support in all compilers that you write code for, then you may go for other answers and just redefine printf. (That being said, I find it useful to distinct debugging prints from regular ones in code — using a different function name helps readability.)
但是,如果您在为其编写代码的所有编译器中都支持可变参数宏,那么您可能会寻找其他答案并重新定义printf. (话虽如此,我发现将调试打印与代码中的常规打印区分开来很有用 - 使用不同的函数名称有助于提高可读性。)
Note that you also can redirect stdoutto the /dev/null, but I assume that you want to get rid from runtime overhead as well.
请注意,您也可以重定向stdout到/dev/null,但我假设您也想摆脱运行时开销。
回答by Jonathan Leffler
#ifdef IGNORE_PRINTF
#define printf(fmt, ...) (0)
#endif
See also C #define macro for debug printingwhich discusses some important issues closely related to this.
另请参阅用于调试打印的 C #define 宏,其中讨论了与此密切相关的一些重要问题。
回答by falstro
Two options, either:
两种选择,要么:
#define printf(...)
(requires C99 variadic macro parameters), you need to put it in some common header file which is never included before stdio.h, if there is one..
(需要 C99 可变参数宏参数),你需要把它放在一些公共头文件中,这个头文件在 stdio.h 之前从未包含过,如果有的话..
Or you can tell the linker to link it to something else, in GCC you would define
或者您可以告诉链接器将其链接到其他内容,在 GCC 中您将定义
int wrap_printf(void) {return 0;}
and link using
并使用链接
--wrap printf
All that said, you should probably not be using printffor printing debug output, but rather a macro or utility function (which in turn can use printf if you'd like) which you have better control over.
综上所述,您可能不应该使用printf打印调试输出,而应该使用宏或实用程序函数(如果您愿意,可以使用 printf),您可以更好地控制它们。
Hope that helps.
希望有帮助。
回答by user1284631
I use to prefix the debug printf()s (not all of them) with PDEB.
我使用 PDEB 作为调试 printf()s(不是全部)的前缀。
For the debug builds, I compile with -DPDEB= (nothing)
对于调试版本,我用 -DPDEB= 编译(没有)
For the release builds, I compile with -DPDEB="0&&" or -DPDEB="0 && "
对于发布版本,我使用 -DPDEB="0&&" 或 -DPDEB="0 && " 进行编译
That way, the following code (test.c):
这样,以下代码(test.c):
#include <stdio.h>
void main(void) {
printf("normal print\n");
PDEB printf("debug print\n");
}
outputs: either (in release mode): normal print
输出:要么(在发布模式下):正常打印
either (in debug mode): normal print debug print
要么(在调试模式下):正常打印调试打印
Ideally, one could aim for turning the PDEB into the "//" (comments mark), except that this is not possible under the standard pre-/processing chain.
理想情况下,人们可以将 PDEB 转换为“//”(注释标记),但在标准预处理/处理链下这是不可能的。
回答by Jerry Coffin
Another possibility would be something like freopen("/dev/null", "w", stdout);
另一种可能性是 freopen("/dev/null", "w", stdout);
This doesn't exactly disable printfthough -- it's roughly equivalent to running your program with stdout redirected to /dev/null, like: ./myprog > /dev/nullat the shell prompt.
但这并没有完全禁用printf——它大致相当于运行你的程序,标准输出重定向到 /dev/null,例如:./myprog > /dev/null在 shell 提示符下。
回答by Jens Gustedt
If you want to avoid the potential warning that Jonathan's answer may give you and if you don't mind an empty call to printfyou could also do something like
如果您想避免乔纳森的回答可能给您的潜在警告,并且如果您不介意给printf您打空电话,也可以执行类似的操作
#define printf(...) printf("")
This works because C macros are not recursive. The expanded printf("")will just be left as such.
这是有效的,因为 C 宏不是递归的。扩展printf("")将保持原样。
Another variant (since you are using gcc) would be something like
另一个变体(因为您使用的是 gcc)类似于
inline int ignore_printf(char const*, ...)
__attribute__ ((format (printf, 1, 2)));
inline int ignore_printf(char const*, ...) { return 0; }
#define printf ignore_printf
and in one compilation unit
并在一个编译单元中
int ignore_printf(char const*, ...)
回答by Srikanth P S
Below simple function serves the purpose, I use the same.
下面简单的函数为目的,我用的一样。
int printf(const char *fmt, ...)
{
return (0)
}
回答by Aspak Rogatiya
Use this macro to enable or disable the printf.
使用这个宏来启用或禁用 printf。
//Uncomment the following line to enable the printf function.
//#define ENABLE_PRINTF
#ifdef ENABLE_PRINTF
#define DEBUG_PRINTF(f,...) printf(f,##__VA_ARGS__)
#else
#define DEBUG_PRINTF(f,...)
#endif
Then call "DEBUG_PRINTF" instead of "printf".
然后调用“DEBUG_PRINTF”而不是“printf”。
For example:
例如:
DEBUG_PRINTF("Hello world: %d", whateverCount);
回答by gatoAlfa
I have used two macros for this. The first one defines the condition to print. In this simple example we print any time the parameter is not zero. More complex expressions can be used.
我为此使用了两个宏。第一个定义要打印的条件。在这个简单的例子中,我们在参数不为零的任何时候打印。可以使用更复杂的表达式。
The second one determines, based on the first macro, to call or not printf.
第二个根据第一个宏确定是否调用 printf。
If the condition can be determined by the compiler (with the right optimization settings) no code is generated.
如果编译器可以确定条件(使用正确的优化设置),则不会生成代码。
If the condition cannot be determined at compile time then will be at run time. One of the advantages of this method is that if printf is not going to happen then the whole printf is not evaluated avoiding many conversions to string that can happen in a complex printf statement.
如果在编译时无法确定条件,则将在运行时确定。这种方法的优点之一是,如果 printf 不会发生,那么整个 printf 不会被评估,避免了在复杂的 printf 语句中可能发生的许多字符串转换。
#define need_to_print(flag) ((flag) != 0))
#define my_printf(debug_level, ...) \
({ \
if(need_to_print(debug_level)) \
printf(__VA_ARGS__); \
})
to use it call my_printfinstead of printfand add a parameter at the beginning for the print condition.
使用它调用my_printf而不是printf并在打印条件的开头添加一个参数。
my_printf(0, "value = %d\n", vv); //this will not print my_printf(1, "value = %d\n", vv); //this will print my_printf(print_debug, "value = %d\n", vv); //this will print if print_debug != 0
the ( ... ) parenthesis surrounding the macro make it a single statement.
围绕宏的 ( ... ) 括号使其成为单个语句。
回答by H.Muhamed Ali
I included #define printf// in common header file. It will suppress all the printf.
我将#define printf//包含在通用头文件中。它将压制所有printf.

