23

I'm learning C++ and I'm using g++ on Linux for practicing.

  1. I want to know if people working as programmers use g++ -pedantic flag and also its importance in real world.

  2. What about other compilers, do they also allow this? Has this become some de-facto standard?

I'm interested because I'm reading C++ Primer where the author points that it’s illegal to use non-const expression as dimension in array definition and g++ by default allows it. And there might be other things I'm unaware of.

2

5 Answers 5

28

Yes, absolutely do this. In fact, you need to study the manual page and turn on more warnings than -pedantic and -Wall will do.

No, there's no standard. MSVC uses /W4 for example.

8
  • 7
    +1, I usually do -Wall -Wextra -Werror -pedantic -std=c++0x :)
    – greyfade
    Jun 30, 2011 at 21:25
  • 4
    @JaredBurrows: Actually I've stopped using -pedantic with C++ since GCC's manual implies it's only meant for C, and I've moved up to using -std=c++14 in new projects.
    – greyfade
    May 10, 2015 at 21:39
  • 1
    @greyfade Ah, I should have said, -Wall -Wextra -Werror -std=c++14 :) May 10, 2015 at 22:03
  • 1
    I just discovered -pedantic-errors which gives errors not warnings.
    – Galik
    Aug 4, 2015 at 19:16
  • 6
    @VillasV Well -Werror turns all warnings into errors. But -pedantic-errors only makes errors out of Standard ICO C++ violations.
    – Galik
    Feb 15, 2016 at 15:21
14

While I have not programmed C++ for quite some time, I'd advise you to use this flag. It helps you creating standards-compliant code and will make everyone's life easier. AFAIK, most other compilers don't support the gcc/g++ extensions.

I hate it for example, when I can't compile code just because the original developer decided to code against non-standard compiler extensions.

I bet that a huge quantity (let's say 20%) of linux programs that were written in C/C++ won't compile with anything but gcc/g++, which makes me kinda sad. Always adhere to the standards.

1
  • 2
    The last time I tried using other compilers, I'd say it was more like 20% that would work with other compilers, and 80% depended on gcc extensions. Jun 30, 2011 at 22:45
10

Personally I use:

-Wall -Wextra -Wshadow -Weffc++ -Wstrict-aliasing -ansi -pedantic -Werror

Thus turns on a host of warnings, but more importantly treats all warnings as errors (as most warnings are logical errors in your thinking).

5
  • 1
    Would you add anything to this list in 2019? Mar 26, 2019 at 11:11
  • 2
    @Ayxan That's the minimum set that I recommend everybody use. But there are a bunch more that are useful. But you need to reed the documentation and see if they are useful for you. Have a look at -Wunreachable-code -Wno-long-long Mar 26, 2019 at 15:00
  • 2
    @Ayxan When building profileing code: -g -pg -fprofile-arcs -ftest-coverage Mar 26, 2019 at 15:02
  • 2
    @Ayxan When building debug -g -O0 Mar 26, 2019 at 15:02
  • 2
    @Ayxan When building production code -O3 Mar 26, 2019 at 15:03
3

It is quite essential to use the -pedantic flag.

With >= g++-10, the current standard preview, it is advisable to first try something similar to:

-std=c++20 -Wall -Wextra -Werror -Wpedantic -pedantic-errors

Where:

  • -std=c++20 tells it to adhere to the revision of the C++ standard, is expected to be published in 2020, you can read more about it here.

  • -Wall tells it to:

    Enable all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros, in other words, some set of warnings.

  • -Wextra tells it to enable some more warnings; still not all.

  • -Werror tells it to make all warnings into errors.

  • -Wpedantic or just -pedantic tells it to:

    Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++.

  • -pedantic-errors tells it to give an error whenever the base standard (see -Wpedantic) requires a diagnostic.


This set is usually useful not only for beginners, as they will gradually learn each warning has it's potential for resulting in error, but also for normal use.

All warning options are described in detail here on the GNU/GCC page.

0

Use all warnings that are useful or mostly useful. Try all the options. Turn one on, and look at the warnings. Then you decide: Do these warnings show situations where a bug might be hiding? Or do they even show a bug? Or are they just useless? You decide if fixing these warnings would improve your code. If yes, you fix all those warnings. If there are very few possibly not useful warnings, you fix them. If there are lots of warnings, and they are pointless, don’t use that warning.

Go through all available warnings that way. Your goal should be to have as many warnings as possible enabled with your code actually not generating any warnings. And at that point you look for a compiler option that turns all warnings into errors.

(Sometimes you must disable warnings. There is one warning that I can turn on that warns in FIPS code. Which I cannot modify, because FIPS must be compiled with zero changes. So that warning must be disabled. I turn it on every few months to fix warnings outside FIPS).

Not the answer you're looking for? Browse other questions tagged or ask your own question.