0

I'm using an external library which has one particular header file that has around 50 static variables in it's header that are unused. I'm using gcc 5.5.0, cmake and c++14. When I try to compile I'm getting -Werror=unused-variable. Normally I would solve such a problem as follows:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
// libraries which have unused variables here
// ...
#pragma GCC diagnostic pop

However the unused variables are statics and therefore the unused (static) variables are emitted once the entire compilation unit has been processed, rather than at the point of declaration. Therefore, having the warning suppression active over the point of declaration does nothing at all, when you then pop it away, the warning is un-ignored at the point where it actually matters; i.e., after the end of the file. This is a reported GCC bug. See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69967

I cannot just switch gcc version at the moment, hence I'm trying to work around the problem.

One solution is to do (void)(unsused_var) in each cpp file where I include this particular header, however this results in quite an ugly hack as I would neet to put around 50 of these casts in multiple cpp files. Any recommendation to efficiently tackle this particular problem? E.g. I would be happy putting the casts in one place, however I'm not sure how to go about that in this situation.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Gio
  • 3,242
  • 1
  • 25
  • 53
  • The obvious "compile with -Wno-unused-variable" is off the table? – Pubby Sep 23 '19 at 18:59
  • Updating to GCC 6+ (where the bug is fixed) is not an option? – ShadowRanger Sep 23 '19 at 19:00
  • @Pubby, yes that's off the table. It's a large codebase and I'm trying to keep things tidy ;) – Gio Sep 23 '19 at 19:09
  • Another ugly hack: Put your `(void)(unsused_var)` in a header, that #includes the external header. Then #include your header instead of the external one. – Ripi2 Sep 23 '19 at 19:14
  • @Ripi don't think you can do that, i.e. a header file contains just definitions. E.g your statement would result in `unexpected unqualified-id before void`. – Gio Sep 23 '19 at 19:25

0 Answers0