让GCC警告预处理文件

发布于 2025-02-12 18:51:17 字数 459 浏览 0 评论 0原文

GCC显示了.c文件的行号,在我的情况下,它是使用其他宏定义的宏,使用更多的宏来定义。文件?

bla.c

#include <stdlib.h>
#include <stdio.h>

#define PRINT(a) printf("%d\n", (a));

int main()
{
    double a = 2.5;

    PRINT(a);

    return 0;
}

它将打印包含打印(a)(第10行)的行的警告,但我希望它打印包含printf的行(“%d \ n”,(a));;来自预处理文件(第1837行)

预处理文件:

<other code above>
int main()
{
 double a = 2.5;

 printf("%d\n", (a));;

 return 0;
}

GCC shows line numbers for the .c file, which in my case is a macro that is defined using other macros, which are defined using even more macros, etc. Is there a way to have GCC give the line numbers for the preprocessed .i file instead?

bla.c

#include <stdlib.h>
#include <stdio.h>

#define PRINT(a) printf("%d\n", (a));

int main()
{
    double a = 2.5;

    PRINT(a);

    return 0;
}

It will print the warning for the line containing PRINT(a) (line 10), but I want it to print the line containing printf("%d\n", (a));; from the preprocessed file (line 1837)

preprocessed file:

<other code above>
int main()
{
 double a = 2.5;

 printf("%d\n", (a));;

 return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

も让我眼熟你 2025-02-19 18:51:17

使用选项-no-no-no-cpp -p获取:

/tmp/ccQ2ECah.i: In function 'main':
/tmp/ccQ2ECah.i:729:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]
  729 |     printf("%d\n", (a));;
      |             ~^     ~~~
      |              |     |
      |              int   double
      |             %f

-no-No-integrated-cpp选项会导致预处理和编译器作为真正单独的通过,在其中运行它们之间只有通信是预处理器输出。

在这种模式下,预处理器通常包括指令在其输出中以从原始文件中标识源线号。 -p选项抑制它们。然后,编译器通行证没有有关原始文件的行号的信息,并且只能打印预处理器输出的行号。

Use the options -no-integrated-cpp -P to get:

/tmp/ccQ2ECah.i: In function 'main':
/tmp/ccQ2ECah.i:729:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=]
  729 |     printf("%d\n", (a));;
      |             ~^     ~~~
      |              |     |
      |              int   double
      |             %f

The -no-integrated-cpp option causes the preprocessor and compiler to run as truly separate passes, where the only communication between them is the preprocessor output.

In this mode, the preprocessor would normally include #line directives in its output to identify source line numbers from the original file. The -P option suppresses them. Then the compiler pass has no information about the original file's line numbers, and can only print the line numbers of the preprocessor output.

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