4

I use GCC 4.5.1 and get warnings like:

warning: expected [error|warning|ignored] after '#pragma GCC diagnostic'

The reason is "#pragma GCC diagnostic push", which doesn't exist for GCC before version 4.6.

I can't change the code (it is not my) and the GCC version too. How can I disable these warnings? Some GCC flags may be?

P.S.: I saw Why "pragma GCC diagnostic push" pop warning in GCC/C++?, but there isn't answer to my question.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
klm123
  • 12,105
  • 14
  • 57
  • 95
  • 1
    I found this [Disabling-pragma-warnings](http://stackoverflow.com/questions/132667/how-to-disable-pragma-warnings?rq=1) solves similar problem – BobLoblaw Aug 15 '13 at 17:13

2 Answers2

9

GCC has these two flags to control warnings regarding pragmas:

-Wunknown-pragmas
Warn when a "#pragma" directive is encountered that is not understood by GCC. If this command-line option is used, warnings are even issued for unknown pragmas in system header files. This is not the case if the warnings are only enabled by the -Wall command-line option.

-Wno-pragmas
Do not warn about misuses of pragmas, such as incorrect parameters, invalid syntax, or conflicts between pragmas. See also -Wunknown-pragmas.

You can turn them off with -Wno-unknown-pragmas.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nos
  • 223,662
  • 58
  • 417
  • 506
  • 2
    FYI: -Wno-unknown-pragmas doesn't help. But -Wno-pragmas does. – klm123 Aug 15 '13 at 17:26
  • [Some context for *-Wno-unknown-warning*](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html): *"...if the -Wno- form is used, the behavior is slightly different: no diagnostic is produced for -Wno-unknown-warning unless other diagnostics are being produced."*. But does `-Wno-unknown-pragmas` actually exist? Must it explicitly be listed or not (on the same page as *[-Wunknown-pragmas](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wunknown-pragmas)*)? Is it GCC version-dependent? – Peter Mortensen Nov 15 '21 at 23:06
0

These seem to be your options:

  1. Use -Wno-pragmas to suppress all incorrect pragma usage warnings. I don't believe it's possible to ignore just the pragma you're interested in.
  2. Ignore the warning outputs. You have been warned by the compiler, so it's done its job. You know why the warning occurs and whether or not the underlying case is harmless.
  3. Use sed find/replace to remove the offending #pragmas for your copy only. You can probably incorporate it into the build script and have it generate a copy of the source files on the fly. You could probably use sed to comment out the offending #pragmas and use a reverse transform to un-comment it (say, prior to committing to a shared repository).
  4. Compile GCC 4.6 in a local place and use that instead.

3.8 Options to Request or Suppress Warnings has the relevant warning information.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131