Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress warnings about unused variables in C++?

Tags:

c++

How can one suppress warnings compiler generates about unused variables in a c++ program?

I am using g++ compiler

like image 674
James Raitsev Avatar asked Aug 06 '12 23:08

James Raitsev


2 Answers

Put in a cast to void:

int unused;
(void)unused;
like image 101
Ross Smith Avatar answered Oct 31 '22 03:10

Ross Smith


Compile with the -Wno-unused-variable option.

See the GCC documentation on Warning Options for more information.

The -Wno-__ options turn off the options set by -W__. Here we are turning off -Wunused-variable.

Also, you can apply the __attribute__((unused)) to the variable (or function, etc.) to suppress this warning on a case-by-case basis. Thanks Jesse Good for mentioning this.

like image 33
Jonathon Reinhart Avatar answered Oct 31 '22 03:10

Jonathon Reinhart