How to disable unused variable warnings coming out of gcc?

I would like to know which switch you pass to the gcc compiler to disable unused variable warnings? I get errors from promotion on windows, and I do not want to touch the promotion code:

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-value and -Wno-unused-value , but did not suppress the above messages.

What is the correct command, here is my compilation 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 -Wall redefines my purpose?

+67
c ++ gcc compiler-warnings compiler-flags
Feb 24 '13 at 16:24
source share
9 answers

The -Wno-unused-variable switch usually does the trick. However, this is a very useful warning, indeed, if you care about these things in your project. It gets annoying when GCC starts warning you about things that aren't in your code.

I would recommend that you keep the warning, but use -isystem instead of -I to include third-party project directories. This flag tells GCC not to warn you that you have no control.

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

Hope this helps. Good luck

+173
Feb 24 '13 at 16:31
source share
— -

Sometimes you just need to suppress only some warnings, and you want to keep other warnings in order to be safe. In code, you can suppress warnings for variables and even formal parameters using an unused GCC attribute. Let's say you have this piece of code:

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

A situation may arise when you need to use this function as a handler, which (imho) is quite common in the C ++ Boost library. Then you need the second formal parameter version , so the function signature matches what the handler needs, otherwise compilation will fail. But you really do not need this in the function itself ...

The decision on how to mark a variable or formal parameter that should be excluded from warnings is as follows:

 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 on the manual pages. This also works for C programs, not just C ++, and I think that it can be used for almost every function, not just handlers. Go and try!;)

PS: Recently, I used this to suppress Boosts serialization warnings in a template as follows:

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

EDIT: Apparently, I did not answer your question at your request, drak0sha did it better. This is because I basically followed the title of the question, my bad. Hope this can help other people who get here because of this name ... :)

+54
Apr 28 '14 at 2:22
source share

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

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

For managed code, you can also use __attribute__((unused)) to instruct the compiler that certain variables are not used.

+49
Mar 28 '14 at 9:17
source share

See man gcc in the "Warning Options" section. There you have a whole group unused

Alert Options
... -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.

Many parameters have long names starting with -f or with -W --- for example, -fmove-loop-invariants, -Wformat, etc. Most of them have both positive and negative forms; the negative form of -ffoo will be -fno-foo. This guide contains only one of these two forms, depending on which one is not the default.

For a more detailed explanation, see Request or Alert Suppression Parameters.

+11
Feb 24 '13 at 16:30
source share

Use -Wno-unused-variable .

+8
Feb 24 '13 at 16:26
source share

The compiler already tells you that this is not value , but variable . You are looking for -Wno-unused-variable . Alternatively, try g++ --help=warnings to see a list of available options.

+2
Feb 24 '13 at 16:28
source share

How to disable unused variable warnings coming out of gcc?
I get errors from promotion on windows, and I don't want to touch forced code ...

You visit Boost Trac and report a bug report with Boost.

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

+1
Jul 26 '15 at 19:52
source share

Remove -Wall and it should work. The problem with -Wall removal is that it will also suppress legitimate warnings. You will have to manually select the -W options in this case or change to -Wno-unused-variable after you have all the warnings.

 LOCAL_CFLAGS = -Wno-unused-variable -g 
-3
May 28 '13 at 22:13
source share
 export IGNORE_WARNINGS=1 

It displays warnings, but continues through assembly

-3
Oct 14 '13 at 9:53 on
source share



All Articles