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

C++ Problem Overview


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:\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 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

Perhaps the -Wall overrides my goal?

C++ Solutions


Solution 1 - C++

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.

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.

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

Hope it helps. Good Luck!

Solution 2 - C++

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 unused attribute. Lets say you have this code snippet:

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

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! ;)

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

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

EDIT: Apparently, I didn't answer your question as you needed, drak0sha done 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... :)

Solution 3 - C++

If you're using gcc and want to disable the warning for 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 code you control, you may also use __attribute__((unused)) to instruct the compiler that specific variables are not used.

Solution 4 - C++

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

> Warning 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 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.

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

Solution 5 - C++

Use -Wno-unused-variable should work.

Solution 6 - C++

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

Solution 7 - C++

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

You visit Boost's Trac and file a bug report against Boost.

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

Solution 8 - C++

-Wall and -Wextra sets the stage in GCC and the subsequent -Wno-unused-variable may not take effect. For example, if you have:

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 -Wextra and seems to ignore -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)

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.

Solution 9 - C++

You can prefix the variables with '(void)'. This can be useful if you don't have access to the build framework or you only want to affect a local change in behavior.

IE:

int main()
{
  int unused1;       //This will print warning
  int unused2;       //This will not print warning -
                     //                             |
  (void) unused2;    // <----------------------------
}

Output:

$ g++ -Wall test.cc
test.cc: In function ‘int main()’:
test.cc:4:7: warning: unused variable ‘unused1’ [-Wunused-variable]
    4 |   int unused1;
      |       ^~~~~~~

Solution 10 - C++

export IGNORE_WARNINGS=1

It does display warnings, but continues with the build

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWilliamKFView Question on Stackoverflow
Solution 1 - C++user405725View Answer on Stackoverflow
Solution 2 - C++Dee'KejView Answer on Stackoverflow
Solution 3 - C++Ole WolfView Answer on Stackoverflow
Solution 4 - C++Olaf DietscheView Answer on Stackoverflow
Solution 5 - C++Mats PeterssonView Answer on Stackoverflow
Solution 6 - C++Daniel FreyView Answer on Stackoverflow
Solution 7 - C++jwwView Answer on Stackoverflow
Solution 8 - C++CerniukView Answer on Stackoverflow
Solution 9 - C++jplozierView Answer on Stackoverflow
Solution 10 - C++Sifiso MyeniView Answer on Stackoverflow