128

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?

WilliamKF
  • 41,123
  • 68
  • 193
  • 295
  • As the error message more or less says, try `-Werror=no-unused-variable` – Joachim Isaksson Feb 24 '13 at 16:28
  • Possible duplicate of [How do I best silence a warning about unused variables?](https://stackoverflow.com/questions/1486904/how-do-i-best-silence-a-warning-about-unused-variables) – Jim Fell Jun 06 '18 at 13:52
  • I am pretty sure that "unused value" and "unused variable" are two different things, otherwise why would we have two different options? – n. m. could be an AI Oct 23 '22 at 07:49

10 Answers10

286

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.

starball
  • 20,030
  • 7
  • 43
  • 238
  • 1
    Where to add this setting? Did it under "Project Settings / AVR GNU C Compiler / Miscellaneous" but it will be ignored (path is correct, still get warnings) When adding that to the Directory settings (uncheck relative path checkbox) AtmelStudio will crash. – hfrmobile Feb 26 '14 at 08:18
  • 5
    we also have `-Wno-unused-parameter` for unused function parameters, `-Wno-unused-function` for unused function – Ngo Thanh Nhan Oct 16 '18 at 03:35
  • 3
    You need: `-Wno-unused-but-set-variable -Wno-unused-parameter -Wno-unused-variable` – Ronny Sherer Apr 12 '19 at 08:13
108

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

Community
  • 1
  • 1
Dee'Kej
  • 1,200
  • 1
  • 9
  • 8
  • 8
    For formal parameters, you can omit the name, e.g. `void func(unsigned number, const int)`. Then, gcc also won't complain about unused `version`. – Olaf Dietsche Oct 08 '16 at 10:29
  • @OlafDietsche I think this depends on `gcc` version you are using. IIRC, I had to use it those 4 years ago, to suppress warnings about unused `version`. ;) – Dee'Kej Aug 13 '18 at 08:36
91

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.

Eponymous
  • 6,143
  • 4
  • 43
  • 43
Ole Wolf
  • 1,312
  • 13
  • 18
  • 3
    That's modulo a few GCC bugs: [Bug C++/53431: C++ preprocessor ignores #pragma GCC diagnostic](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53431); [Bug C++/66943: GCC warns of Unknown Pragma for OpenMP, even though it support it](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66943), and friends. – jww Jul 26 '15 at 20:10
  • Generally, I would say using #pragma directives is not very safe, unless you really know what you are doing, or you are using e.g. the OpenMP for parallelization... ;) – Dee'Kej Feb 17 '16 at 00:12
  • 1
    @Dee'Kej `#pragma` directives are perfectly safe if you wrap them into `#ifdef some_compiler ... #endif`. Warnings are a compiler-specific feature and aren't defined by the standard. You can't suppress any warnings from code without `#pragma`s. – Kotauskas Jul 08 '19 at 17:48
  • @vladislav-toncharov You're right about warnings being compiler specific. But we're not talking about compiler warnings in general here - we're talking specifically about GCC. You're are also correct about #pragma not being harmful per say. However, IMO many people don't know how to properly use #pragma, and that's why I consider it "dangerous" (for the lack of better words). – Dee'Kej Jul 18 '19 at 11:17
19

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

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
14

Use -Wno-unused-variable should work.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
6

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;
      |       ^~~~~~~
jplozier
  • 255
  • 3
  • 6
4

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.

jww
  • 97,681
  • 90
  • 411
  • 885
3

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.

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
0

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

Cerniuk
  • 14,220
  • 2
  • 29
  • 27
0

I recommend neither editing 3rd party code nor suppressing warnings globally. If you are using CMake, you can suppress specific warning only for the external library.

find_package(Boost REQUIRED)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    target_compile_options(Boost::boost PUBLIC -Wno-unused-variable)
endif()
...
add_executable(main "main.cpp")
target_link_libraries(main Boost::boost)

See also FindBoost.cmake.

Burak
  • 2,251
  • 1
  • 16
  • 33