C++ 如何禁用我不想编辑的第 3 方代码中来自 gcc 的未使用变量警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15053776/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:57:18  来源:igfitidea点击:

How do you disable the unused variable warnings coming out of gcc in 3rd party code I do not wish to edit?

c++gcccompiler-warningscompiler-flags

提问by WilliamKF

I'd like to know what switch you pass to the gcc compiler to turn off unused variable warnings? I'm getting errors out of boost on windows and I do not want to touch the boost code:

我想知道您传递给 gcc 编译器的哪个开关可以关闭未使用的变量警告?我在 Windows 上遇到 boost 错误,我不想触及 boost 代码:

C:\boost_1_52_0/boost/system/error_code.hpp: At global scope:
C:\boost_1_52_0/boost/system/error_code.hpp:214:36: error: 'boost::system::posix_category' defined but not used [-Werror=unused-variable]
C:\boost_1_52_0/boost/system/error_code.hpp:215:36: error: 'boost::system::errno_ecat' defined but not used [-Werror=unused-variable]
C:\boost_1_52_0/boost/system/error_code.hpp:216:36: error: 'boost::system::native_ecat' defined but not used [-Werror=unused-variable]

I tried using both -Wunused-valueand -Wno-unused-valuebut neither suppressed the messages above.

我尝试使用两者-Wunused-value-Wno-unused-value但都没有抑制上面的消息。

What is the right command, here is my compile line:

什么是正确的命令,这是我的编译行:

g++  -g -fno-inline -Wall -Werror -Wextra -Wfloat-equal -Wshadow
-Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wno-conversion 
-Wdisabled-optimization -Wredundant-decls -Wunused-value -Wno-deprecated 
-IC:\boost_1_52_0 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 
-c -o op.o op.cpp

Perhaps the -Walloverrides my goal?

也许-Wall超越了我的目标?

回答by

The -Wno-unused-variableswitch usually does the trick. However, that is a very useful warning indeed if you care about these things in your project. It becomes annoying when GCC starts to warn you about things not in your code though.

-Wno-unused-variable开关通常是卓有成效的。但是,如果您关心项目中的这些事情,这确实是一个非常有用的警告。但是,当 GCC 开始警告您代码中没有的内容时,这会变得很烦人。

I would recommend you keeping the warning on, but use -isysteminstead of -Ifor include directories of third-party projects. That flag tells GCC not to warn you about the stuff you have no control over.

我建议您保持警告,但使用-isystem而不是-I用于包含第三方项目的目录。该标志告诉 GCC 不要就您无法控制的内容向您发出警告。

For example, instead of -IC:\\boost_1_52_0, say -isystem C:\\boost_1_52_0.

例如,不要-IC:\\boost_1_52_0-isystem C:\\boost_1_52_0

Hope it helps. Good Luck!

希望能帮助到你。祝你好运!

回答by Dee'Kej

Sometimes you just need to suppress only some warnings and you want to keep other warnings, just to be safe. In your code, you can suppress the warnings for variables and even formal parameters by using GCC's unusedattribute. Lets say you have this code snippet:

有时你只需要抑制一些警告,而你想保留其他警告,只是为了安全。在您的代码中,您可以使用 GCC 的未使用属性来抑制变量甚至形式参数的警告。假设你有这个代码片段:

void func(unsigned number, const int version)
{
  unsigned tmp;
  std::cout << number << std::endl;
}

There might be a situation, when you need to use this function as a handler - which (imho) is quite common in C++ Boost library. Then you need the second formal parameter version, so the function's signature is the same as the template the handler requires, otherwise the compilation would fail. But you don't really need it in the function itself either...

可能有一种情况,当您需要将此函数用作处理程序时 - 这(恕我直言)在 C++ Boost 库中很常见。那么你需要第二个形参version,所以函数的签名与处理程序需要的模板相同,否则编译会失败。但是您也不需要在函数本身中使用它...

The solution how to mark variable or the formal parameter to be excluded from warnings is this:

如何标记变量或从警告中排除的形式参数的解决方案是:

void func(unsigned number, const int version __attribute__((unused)))
{
  unsigned tmp __attribute__((unused));
  std::cout << number << std::endl;
}

GCC has many other parameters, you can check them in the man pages. This also works for the C programs, not only C++, and I think it can be used in almost every function, not just handlers. Go ahead and try it! ;)

GCC 有许多其他参数,您可以在手册页中查看它们。这也适用于 C 程序,不仅适用于 C++,而且我认为它几乎可以用于所有函数,而不仅仅是处理程序。继续尝试!;)

P.S.: Lately I used this to suppress warnings of Boosts' serialization in template like this:

PS:最近我用它来抑制模板中 Boosts 序列化的警告,如下所示:

template <typename Archive>
void serialize(Archive &ar, const unsigned int version __attribute__((unused)))

EDIT: Apparently, I didn't answer your question as you needed, drak0shadone it better. It's because I mainly followed the title of the question, my bad. Hopefully, this might help other people, who get here because of that title... :)

编辑:显然,我没有按照您的需要回答您的问题,drak0sha做得更好。这是因为我主要是按照问题的标题,我的不好。希望这可以帮助其他人,因为这个标题而来到这里...... :)

回答by Ole Wolf

If you're using gcc and want to disable the warning for selected code, you can use the #pragma compiler directive:

如果您正在使用 gcc 并希望禁用所选代码的警告,您可以使用 #pragma 编译器指令:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
( your problematic library includes )
#pragma GCC diagnostic pop

For code you control, you may also use __attribute__((unused))to instruct the compiler that specific variables are not used.

对于您控制的代码,您还可以__attribute__((unused))用来指示编译器不使用特定变量。

回答by Olaf Dietsche

See man gccunder Warning Options. There you have a whole bunch of unused

请参阅man gcc警告选项下的内容。你有一大堆unused

Warning Options
... -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable

警告选项
... -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable

If you prefix any of them with no-, it will disable this warning.

如果您为其中任何一个添加前缀no-,它将禁用此警告。

Many options have long names starting with -f or with -W---for example, -fmove-loop-invariants, -Wformat and so on. Most of these have both positive and negative forms; the negative form of -ffoo would be -fno-foo. This manual documents only one of these two forms, whichever one is not the default.

许多选项都有以 -f 或 -W 开头的长名称---例如,-fmove-loop-invariants、-Wformat 等。其中大部分有正面和负面两种形式;-ffoo 的否定形式是 -fno-foo。本手册仅记录了这两种形式中的一种,以非默认形式为准。

More detailed explanation can be found at Options to Request or Suppress Warnings

更详细的解释可以在请求或抑制警告的选项中找到

回答by Mats Petersson

Use -Wno-unused-variableshould work.

使用-Wno-unused-variable应该有效。

回答by Daniel Frey

The compiler is already telling you, it's not valuebut variable. You are looking for -Wno-unused-variable. Also, try g++ --help=warningsto see a list of available options.

编译器已经告诉你,它不是value但是variable。您正在寻找-Wno-unused-variable. 此外,尝试g++ --help=warnings查看可用选项列表。

回答by jww

How do you disable the unused variable warnings coming out of gcc?
I'm getting errors out of boost on windows and I do not want to touch the boost code...

如何禁用来自 gcc 的未使用变量警告?
我在 Windows 上的 boost 出错了,我不想碰 boost 代码......

You visit Boost's Tracand file a bug report against Boost.

您访问Boost 的 Trac并针对 Boost 提交错误报告。

Your application is not responsible for clearing library warnings and errors. The library is responsible for clearing its own warnings and errors.

您的应用程序不负责清除库警告和错误。库负责清除自己的警告和错误。

回答by William Cerniuk

-Walland -Wextrasets the stage in GCC and the subsequent -Wno-unused-variablemay not take effect. For example, if you have:

-Wall-Wextra在 GCC 中设置阶段,后续-Wno-unused-variable可能不会生效。例如,如果您有:

CFLAGS += -std=c99 -pedantic -pedantic-errors -Werror -g0 -Os \ -fno-strict-overflow -fno-strict-aliasing \ -Wall -Wextra \ -pthread \ -Wno-unused-label \ -Wno-unused-function \ -Wno-unused-parameter \ -Wno-unused-variable \ $(INC)

CFLAGS += -std=c99 -pedantic -pedantic-errors -Werror -g0 -Os \ -fno-strict-overflow -fno-strict-aliasing \ -Wall -Wextra \ -pthread \ -Wno-unused-label \ -Wno-unused-function \ -Wno-unused-parameter \ -Wno-unused-variable \ $(INC)

then GCC sees the instruction -Wall -Wextraand seems to ignore -Wno-unused-variable

然后 GCC 看到了指令-Wall -Wextra,似乎忽略了-Wno-unused-variable

This can instead look like this below and you get the desired effect of not being stopped in your compile on the unused variable:

这可以看起来像下面这样,您将获得在未使用变量的编译中不被停止的预期效果:

CFLAGS += -std=c99 -pedantic -pedantic-errors -Werror -g0 -Os \ -fno-strict-overflow -fno-strict-aliasing \ -pthread \ -Wno-unused-label \ -Wno-unused-function \ $(INC)

CFLAGS += -std=c99 -pedantic -pedantic-errors -Werror -g0 -Os \ -fno-strict-overflow -fno-strict-aliasing \ -pthread \ -Wno-unused-label \ -Wno-unused-function \ $(INC)

There is a good reason it is called a "warning" vs an "error". Failing the compile just because you code is not complete (say you are stubbing the algorithm out) can be defeating.

它被称为“警告”与“错误”是有充分理由的。仅仅因为你的代码不完整而导致编译失败(比如你正在剔除算法)可能会失败。

回答by Sifiso Myeni

export IGNORE_WARNINGS=1

It does display warnings, but continues with the build

它确实显示警告,但继续构建