Home > Mobile >  What's the difference between '-Wextra' and '-pedantic' in gcc?
What's the difference between '-Wextra' and '-pedantic' in gcc?

Time:08-11

Or - who is "stricter"?

From what I've read, both flags add extra warnings, not included in '-Wall', but what distinguish them?

CodePudding user response:

-Wextra is about turning on warnings that may be less useful or more of a nuisance (because they may warn about code that is acceptable to the programmer) than other options. It is generally for use when you want to scour your code for issues and make changes to satisfy these extra warnings.

-Wpedantic is about sticking more closely to the language standard. It warns for some uses of language extensions that the compiler otherwise considers normal but that can make the program non-portable to other compilers.

CodePudding user response:

-Wpedantic enforces ISO C and ISO C standards.

Personally, I find that to be unhelpful and a nuisance, so I prefer -Wall -Wextra -Werror rather than -Wall -Wextra -Wpedantic -Werror, as I regularly rely on compiler extensions in gcc or clang, especially when programming on embedded systems such as microcontrollers, or when doing low-level work with registers, binary packets, etc.

Here are some of my notes on this from my eRCaGuy_hello_world readme here:

  1. Use -Wall -Wextra -Werror (which is a good idea) to see all warnings and to convert all warnings to errors, to make you write better code. Some people may leave off -Wextra, and just use -Wall -Werror. Others may also like to add -Wpedantic to that list (now it's -Wall -Wextra -Wpedantic -Werror) to enforce ISO C and ISO C standards, but other people or code-bases will explicitly turn OFF pedantic with -Wno-pedantic to explicitly DISABLE pedantic (ISO standards) checks and ALLOW compiler extensions. Personally, I prefer to leave compiler extensions ENABLED (meaning: do NOT use -Wpedantic), as compiler extensions are very frequently used and very useful, especially in small, embedded, low-level and hardware-centric systems such as microcontrollers.
    • In other words, I recommend that you DO use -Wall -Wextra -Werror but that you NOT use -Wpedantic, but if you'd like to use -Wpedantic as well, you may. See just below for additional details.
    • For details on which warning flags -Wall and -Wextra each turn on, refer to the gcc user manual here (https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html), and search the page for these warnings, or just scroll partway down. There's a nice list detailing what individual warning flags are under each (-Wall, and -Wextra).
  2. Add the -Wpedantic or -pedantic (same thing) build flag to enforce strict ISO C or ISO C standards and "reject all programs that use forbidden extensions". Or, conversely, add -Wno-pedantic to ENABLE extensions and turn a previously-set -Wpedantic back OFF. -Wpedantic NOT being on is the default. See gcc's user manual section on Warning Options, for example, as well as the bullet just above, for details: https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html.
    • Some people want strict ISO C and C functionality only. They use -Wpedantic.
    • Some libraries REQUIRE compiler extensions and explicitly have -Wno-pedantic set for the library to enable compiler extensions and disable any previously-set -Wpedantic flags. Sometimes this is just for certain files which require exceptions to use certain compiler extensions.
    • Others rely on compiler extensions and do NOT use -Wpedantic. I am in this latter category and prefer NOT to use -Wpedantic, so that I CAN use gcc (or clang) compiler extensions. They are good. They are safe. They are helpful. They just may not be quite as portable across compilers is all, though from gcc to clang and back again, you are usually ok to use them, since clang strives to be gcc-compatible by design (see http://clang.llvm.org/: clang strives to be gcc compatible: "End-User Features" they advertise include: "GCC compatibility").
    • See also my notes about -Wpedantic in the previous higher-level bullet just above this one.
  3. clang, but NOT gcc, also has a -Weverything option which appears to include things such as -Wpedantic. You can test it here: https://godbolt.org/z/qcYKd1. Notice the -Wvla-extension warning we get since we are relying on a C99 extension in C in this case, and we have -Weverything set. We get the same warning if we just use -Wpedantic, as shown here: https://godbolt.org/z/M9ahE4, indicating that -Weverything does in fact include -Wpedantic. We get no warning if we have neither of those flags set: https://godbolt.org/z/j8sfsY. Despite -Weverything existing and working in clang, however, I can find no documentation whatsoever on its existence, neither in the clang man pages nor in the online manual here: https://clang.llvm.org/docs/DiagnosticsReference.html.
  •  Tags:  
  • c gcc
  • Related