抑制 C 警告“未使用的变量 x”的最佳方法?

发布于 2024-09-13 05:53:26 字数 185 浏览 2 评论 0原文

抑制 C 编译器(例如 GCC)如“未使用的变量 x”警告的最佳/最巧妙的方法是什么?

GCC 的文档解释

-未使用变量
每当局部变量或非常量静态变量除了其声明之外未使用时发出警告

我不想向编译器提供任何特定标志来删除所有这些警告,只是针对特殊情况。

What is the best/neatest way to suppress a C compiler (for example GCC) like "Unused variable x" warning?

GCC's doumentation explains

-Wunused-variable
Warn whenever a local variable or non-constant static variable is unused aside from its declaration

I don't want to give any certain flags to my compiler to remove all these warnings, just for special cases.

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

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

发布评论

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

评论(11

給妳壹絲溫柔 2024-09-20 05:53:27

告诉您的编译器使用编译器特定的非标准机制

请参阅 __attribute__((unused))、各种 #pragma 等的单独答案。 (可选)将预处理器宏包裹在其周围以实现可移植性。

关闭警告

IDE 可以通过视觉方式指示未使用的变量(不同颜色或下划线)。这样一来,编译器警告可能就毫无用处了。

在 GCC 和 Clang 中,在命令行末尾添加 -Wno-unused-parameter 选项(在所有打开未使用参数警告的选项之后,例如 -Wall、<代码>-Wextra)。

将强制转换添加到 void

void foo(int bar) {
    (void)bar;
}

按照 jamesdlin 的回答Mailbag :关闭编译器警告

不要给变量命名(仅限 C23 和 C++)

在 C23 标准之前的 C 中不允许,但使用最新的编译器(2023 年)和 C++ 中(从今以后)可以这样做

void foo(int /*bar*/) {
    ...
}

请参阅 N2480 允许在函数定义中使用未命名参数 (pdf) 提案,以及检查实施状态 https://en.cppreference.com/w/c/compiler_support

GCC 11、Clang 11 和 ICX 2022.2 (oneAPI 2022.3) 支持此功能。

Tell your compiler using a compiler specific nonstandard mechanism

See individual answers for __attribute__((unused)), various #pragmas and so on. Optionally, wrap a preprocesor macro around it for portability.

Switch the warning off

IDEs can signal unused variables visually (different color, or underline). Having that, compiler warning may be rather useless.

In GCC and Clang, add -Wno-unused-parameter option at the end of the command line (after all options that switch unused parameter warning on, like -Wall, -Wextra).

Add a cast to void

void foo(int bar) {
    (void)bar;
}

As per jamesdlin's answer and Mailbag: Shutting up compiler warnings.

Do not give the variable a name (C23 and C++ only)

Not allowed in C before the C23 standard, but with a latest compiler (in 2023) and in in C++ (since like forever) one can do

void foo(int /*bar*/) {
    ...
}

See the N2480 Allowing unnamed parameters in a function definition (pdf) proposal, and check the implementation status at https://en.cppreference.com/w/c/compiler_support

GCC 11, Clang 11, and ICX 2022.2 (oneAPI 2022.3) support this.

み格子的夏天 2024-09-20 05:53:27

我找到了一篇文章,http://sourcefrog.n​​et/weblog/ software/languages/C/unused.html,解释了UNUSED。有趣的是,作者还修改了未使用的变量名称,这样您将来就不会无意中使用它。

摘抄:

#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif

void dcc_mon_siginfo_handler(int UNUSED(whatsig))

I found an article, http://sourcefrog.net/weblog/software/languages/C/unused.html, that explains UNUSED. It is interesting that the author also mangles the unused variable name, so you can't inadvertently use it in the future.

Excerpt:

#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif

void dcc_mon_siginfo_handler(int UNUSED(whatsig))
迷路的信 2024-09-20 05:53:27

如果这确实是您想要的,您可以使用 unused 属性(仅限 GCC),例如:

void foo(int __attribute__((__unused__)) bar) {
    ...
}

当然,不仅仅是函数参数,但这是最常见的用例,因为它可能是API 的回调函数,您实际上并不需要所有输入。

此外, GLib 有一个 G_GNUC_UNUSED 宏,我相信它扩展为该属性。

If this is really what you want, you could use the unused attribute (GCC only), something like:

void foo(int __attribute__((__unused__)) bar) {
    ...
}

Not just for function parameters, of course, but that's the most common use case, since it might be a callback function for an API where you don't actually need all the input.

Additionally, GLib has a G_GNUC_UNUSED macro which I believe expands to that attribute.

寂寞笑我太脆弱 2024-09-20 05:53:27

您可以使用#pragma消除警告

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused"

int unususedVariable = 1;

#pragma clang diagnostic pop

如果您使用的是GCC,请使用#pragma gcc ...

You can silence the warning using #pragma

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused"

int unususedVariable = 1;

#pragma clang diagnostic pop

If you are using GCC, use #pragma gcc ...

相守太难 2024-09-20 05:53:27

#pragma 未使用<变量>

#pragma unused <variable>

江湖彼岸 2024-09-20 05:53:27

强制转换为 void 是最好的方法,因为它表明您没有“意外”将变量保留在代码中 - 即:此函数可能是一个实例,其中您有一个需要相同参数类型的函数指针表,并且返回类型,但在此特定表条目中您没有使用该参数。

也就是说,如果你不需要它,就扔掉它。 ;)

The cast to a void is the best approach because it shows that you didn't "accidentally" keep the variable in your code - ie: this function might be an instance where you have a table of function pointers that need the same parameter types and return types, but in this particular table entry you are not using the parameter.

That said, if you don't need it, get rid of it. ;)

北方的韩爷 2024-09-20 05:53:27

这是一个非常黑客的解决方案,但尝试简单地将变量分配给自身。

我认为这应该会让大多数编译器误以为该变量已被使用。它也应该非常便携。

It's a very hackish solution, but try simply assigning the variable to itself.

I think that should fool most compilers into thinking that the variable is used. It should be quite portable too.

追星践月 2024-09-20 05:53:27

将其分配给自身:

void f(int unused) {
    unused = unused;
}

它在 GCC 中工作,但 Clang 需要 -Wno-self-assign 。


我认为强制转换为 void 是最可移植的解决方案:GCC 和 Clang 都明白这一点,即使有完整的警告 -W{all,extra,pedantic}

(void)unused;

Assign it to itself:

void f(int unused) {
    unused = unused;
}

It works in GCC, but Clang needs -Wno-self-assign.


I think casting to void is the most portable solution: Both GCC and Clang understand this, even with full warnings -W{all,extra,pedantic}:

(void)unused;
花海 2024-09-20 05:53:27

从代码中删除未使用的变量声明(双关语)。

(什么???这就是我所做的:指出显而易见的很可能是最好的解决方案。)

现在,从对其他答案的评论来看,显然它是由宏生成的垃圾。嗯,这是一个辩词。

解决方案:

  • 将该宏重构为 #if 仅在确实使用变量时才声明该变量;
  • 创建另一个版本的宏,跳过未使用的变量生成。
  • 更好的是,避免使用会给代码带来问题的宏。

Delete the unused variable declaration from the code (pun intended).

(What??? It's what I do: point that the obvious is most likely the best solution.)

Now, from comments on other answers, apparently it's garbage generated from macros. Well, that's a pleonasm.

Solutions:

  • refactor that macro to #if declare the variable only if it's really used;
  • create another version of the macro that skips the unused variable generation.
  • Better still, avoid using macros that bring issues to the code.
我一向站在原地 2024-09-20 05:53:27

如果它已被使用并且您正在交付项目,请将其删除。最差的,评论一下。

If it’s used and you are shipping the project, delete it. Worst, comment it.

痴意少年 2024-09-20 05:53:26

(void)variable 可能适用于某些编译器。

对于 C++ 代码,另请参阅Mailbag:关闭编译器警告,Herb Sutter 建议使用:

template<class T> void ignore( const T& ) { }

...

ignore(variable);

(void) variable might work for some compilers.

For C++ code, also see Mailbag: Shutting up compiler warnings where Herb Sutter recommends using:

template<class T> void ignore( const T& ) { }

...

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