有没有比在 C 中使用 #ifdef 更简单的方法?

发布于 2024-07-24 14:07:26 字数 365 浏览 4 评论 0原文

据我了解,断言是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

梦在深巷 2024-07-31 14:07:27

我还会打印一些其他 C 预处理器标志,可以帮助您跟踪问题

printf("%s:%d {a=%i, b=%i}\n", __FILE__, __LINE__, a, b);

I would also print some other C preprocessor flags that can help you track problems down

printf("%s:%d {a=%i, b=%i}\n", __FILE__, __LINE__, a, b);
你的笑 2024-07-31 14:07:27

您可以将 PRINTF_IF_DEBUGGING 定义为

#ifndef NDEBUG
#define PRINTF_IF_DEBUGGING(X) printf(X)
#else
#define PRINTF_IF_DEBUGGING(X)
#endif

这会将 #ifdef 集中在一处。

You can define PRINTF_IF_DEBUGGING as

#ifndef NDEBUG
#define PRINTF_IF_DEBUGGING(X) printf(X)
#else
#define PRINTF_IF_DEBUGGING(X)
#endif

This will centralize the #ifdefs in only one place.

一口甜 2024-07-31 14:07:26

试试这个:

#ifdef debugmode
#define DEBUG(cmd) cmd
#else
#define DEBUG(cmd)
#endif


DEBUG(printf("a = %i, b = %i", a, b));

现在,如果你定义了debugmode,你就会得到你的打印语句。 否则,它永远不会出现在二进制文件中。

try this:

#ifdef debugmode
#define DEBUG(cmd) cmd
#else
#define DEBUG(cmd)
#endif


DEBUG(printf("a = %i, b = %i", a, b));

now, if you have debugmode defined, you get your print statement. otherwise, it never shows up in the binary.

爱的故事 2024-07-31 14:07:26

使用 GCC,我真的很喜欢在每个文件中添加:

#if 0
#define TRACE(pattern,args...)   fprintf(stderr,"%s:%s/%u" pattern "\n",__FILE__,__FUNCTION__,__LINE__,##args)
#else
#define TRACE(dummy,args...)
#endif

然后在代码中:

i++;
TRACE("i=%d",i);

仅当我激活文件顶部的 TRACE() 宏时才会打印 i 。 效果真的很棒,而且它还打印源文件、行和它发生的函数。

Using GCC, I really enjoy to add, per file:

#if 0
#define TRACE(pattern,args...)   fprintf(stderr,"%s:%s/%u" pattern "\n",__FILE__,__FUNCTION__,__LINE__,##args)
#else
#define TRACE(dummy,args...)
#endif

and then in the code:

i++;
TRACE("i=%d",i);

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.

对岸观火 2024-07-31 14:07:26
if (MY_DEBUG_DEFINE) {
        do_debug_stuff();
}

任何像样的编译器都会优化该块。 请注意,您需要将 MY_DEBUG_DEFINE 定义为布尔值(即 0 或非 0)。

#define MY_DEBUG_DEFINE defined(NDEBUG)

如果您碰巧以最大警告级别进行编译,此技巧可以避免未引用的参数

if (MY_DEBUG_DEFINE) {
        do_debug_stuff();
}

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).

#define MY_DEBUG_DEFINE defined(NDEBUG)

If you happen to compile with maximum warning level, this trick avoids unreferenced argument.

梦行七里 2024-07-31 14:07:26

使用不支持的预处理器获取 vararg 的解决方法

#define DEBUG

#ifdef DEBUG
#define trace(args) printf args
#else
#define trace(args)
#endif


int dostuff(int value)
{


    trace(("%d", value));

}

Workaround to get vararg with preprocessors that don't support it

#define DEBUG

#ifdef DEBUG
#define trace(args) printf args
#else
#define trace(args)
#endif


int dostuff(int value)
{


    trace(("%d", value));

}
千笙结 2024-07-31 14:07:26

好吧,这里给出的 PRINT_DEBUG 类型宏的问题是它们只允许一个参数。 对于正确的 printf(),我们需要几个,但 C 宏(目前)不允许变量参数。

因此,为了实现这一目标,我们必须发挥创造力。

#ifdef debugmode
     #define PRINTF   printf
#else
     #define PRINTF    1 ? NULL : printf
#endif

然后,当您在非调试模式下编写 PRINTF("a = %i, b = %i", a, b); 时,它将被渲染为(有效):

 if (true) NULL;
 else 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.

#ifdef debugmode
     #define PRINTF   printf
#else
     #define PRINTF    1 ? NULL : printf
#endif

Then when you write PRINTF("a = %i, b = %i", a, b);, in non-debug mode, it will be renders as (effectively):

 if (true) NULL;
 else printf("a = %i, b = %i", a, b);

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文