16

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.

21

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.

  • 7
    +1, I usually do -Wall -Wextra -Werror -pedantic -std=c++0x :) – greyfade Jun 30 '11 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 '15 at 21:39
  • 1
    @greyfade Ah, I should have said, -Wall -Wextra -Werror -std=c++14 :) – Jared Burrows May 10 '15 at 22:03
  • 1
    I just discovered -pedantic-errors which gives errors not warnings. – Galik Aug 4 '15 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 '16 at 15:21
10

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.

  • 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. – Jerry Coffin Jun 30 '11 at 22:45
5

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).

  • Would you add anything to this list in 2019? – Ayxan Mar 26 at 11:11
  • 1
    @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 – Martin York Mar 26 at 15:00
  • 1
    @Ayxan When building profileing code: -g -pg -fprofile-arcs -ftest-coverage – Martin York Mar 26 at 15:02
  • 1
    @Ayxan When building debug -g -O0 – Martin York Mar 26 at 15:02
  • 1
    @Ayxan When building production code -O3 – Martin York Mar 26 at 15:03
1

It is quite essential to use the -pedantic flag.

I would only add, that with the current standard it is advisable to use something similar to:

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

Where:

  • -std=c++17 tells it to adhere to the revision of the C++ standard, published in 2017, 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.

protected by gnat Nov 6 '17 at 11:12

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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