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

2021-12-18 00:00:00 gcc c++ compiler-warnings compiler-flags

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

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:

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

我尝试同时使用 -Wunused-value-Wno-unused-value 但都没有抑制上面的消息.

I tried using both -Wunused-value and -Wno-unused-value but neither suppressed the messages above.

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

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

也许 -Wall 覆盖了我的目标?

Perhaps the -Wall overrides my goal?

推荐答案

-Wno-unused-variable 开关通常可以解决问题.但是,如果您关心项目中的这些事情,这确实是一个非常有用的警告.但是,当 GCC 开始警告您代码中没有的内容时,这会很烦人.

The -Wno-unused-variable switch 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.

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

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

例如,使用 -isystem C:\boost_1_52_0 而不是 -IC:\boost_1_52_0.

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

希望有帮助.祝你好运!

Hope it helps. Good Luck!

相关文章