有没有比在 C 中使用 #ifdef 更简单的方法?
据我了解,断言是 C 中的一个宏,据说如果您在编译时使用它但将其禁用,那么就不会产生开销(我不知道这可能不正确)。 对我来说,问题是我想做的是将所有变量传递给我的函数并打印出该输出,但前提是我想启用调试。 到目前为止,这是我所拥有的:
int exampleFunction (int a, int b)
{
#ifdef debugmode
printf("a = %i, b = %i", a, b);
#endif
}
我想知道是否有更简单(而且不那么难看)的方法来做这样的事情。 xdebug for php 有这个功能,我发现它在调试时节省了我大量的时间,所以我想为每个函数都这样做。
谢谢
From what I understand assert is a macro in C and supposedly if you use it at compile time but leave it disabled then there won't be overhead (which might not be correct I don't know).
The problem for me is that what I'd like to do is get all the variables passed to my function and print out that output, but only if I want debugging enabled. Here is what I have so far:
int exampleFunction (int a, int b)
{
#ifdef debugmode
printf("a = %i, b = %i", a, b);
#endif
}
I'm wondering if there is any easier (and less ugly) method for doing something like this. xdebug for php has this feature and I've found is saves me an enormous amount of time when debugging so i want to do it for each function.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我还会打印一些其他 C 预处理器标志,可以帮助您跟踪问题
I would also print some other C preprocessor flags that can help you track problems down
您可以将 PRINTF_IF_DEBUGGING 定义为
这会将 #ifdef 集中在一处。
You can define PRINTF_IF_DEBUGGING as
This will centralize the #ifdefs in only one place.
试试这个:
现在,如果你定义了
debugmode
,你就会得到你的打印语句。 否则,它永远不会出现在二进制文件中。try this:
now, if you have
debugmode
defined, you get your print statement. otherwise, it never shows up in the binary.使用 GCC,我真的很喜欢在每个文件中添加:
然后在代码中:
仅当我激活文件顶部的 TRACE() 宏时才会打印
i
。 效果真的很棒,而且它还打印源文件、行和它发生的函数。Using GCC, I really enjoy to add, per file:
and then in the code:
i
will be printed only when I activate the TRACE() macro in the top of the file. Works really great, plus it prints the source file, line and function it occurred.任何像样的编译器都会优化该块。 请注意,您需要将 MY_DEBUG_DEFINE 定义为布尔值(即 0 或非 0)。
如果您碰巧以最大警告级别进行编译,此技巧可以避免未引用的参数。
Any half decent compiler would optimize the block away. Note you need to define
MY_DEBUG_DEFINE
as a boolean (ie 0 or not 0).If you happen to compile with maximum warning level, this trick avoids unreferenced argument.
使用不支持的预处理器获取 vararg 的解决方法
Workaround to get vararg with preprocessors that don't support it
好吧,这里给出的 PRINT_DEBUG 类型宏的问题是它们只允许一个参数。 对于正确的 printf(),我们需要几个,但 C 宏(目前)不允许变量参数。
因此,为了实现这一目标,我们必须发挥创造力。
然后,当您在非调试模式下编写
PRINTF("a = %i, b = %i", a, b);
时,它将被渲染为(有效):编译器很高兴,但是 printf 永远不会执行,并且如果编译器很聪明(即任何现代 C 编译器),则 printf() 的代码将永远不会生成,因为编译器将认识到永远无法采用该路径。
但请注意,参数仍将被评估,因此如果它们有任何副作用(即 ++x 或函数调用),则可能会生成它们的代码(但不会执行)
Well, the problem with the PRINT_DEBUG type macros as given here, is that they only allow one parameter. For a proper printf() we'll need several, but C macros don't (presently) allow variable arguments.
So, to pull this off, we've got to get creative.
Then when you write
PRINTF("a = %i, b = %i", a, b);
, in non-debug mode, it will be renders as (effectively):The compiler is happy, but the printf is never execute, and if the compiler if bright (i.e, any modern C compiler), the code for the printf() will never be generated, as the compiler will recognize that path can never be taken.
Note, however, that the parameters will still be evaluated, so if they have any side effects (i.e, ++x or a function call), they code may be generated (but not executed)