1

I am trying to learn C++ having no previous experience with it or C, and I see that some training materials are sometimes trying to teach me C coding instead of C++.

As I don't have enough knowledge to discern if the books are trying to teach me proper C++ or not, I thought I could set some parameter on g++ compiler to save me this problem. So here my question is:

How can I set the g++ compiler in Windows so that he won't compile if what I am writing is not pure C++ standard?

Many thanks in advance

user5193682
  • 260
  • 1
  • 11
  • There is no way to do this, C and C++ share some similar syntax, and there is absolutely nothing a compiler is going to do if you use for example `printf()` rather than `cout/cerr` etc. This something you *just have to learn*. There is nothing inherrently bad in learning some of the C library functions (as they can be used from your C++ programs too, you just have learn to make the correct judgement call as to when to use or not..) – Nim Aug 05 '15 at 11:14
  • 1
    FWIW: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Lightness Races in Orbit Aug 05 '15 at 11:16

2 Answers2

5

No compiler is perfect (most do not 100% implement any particular C++ standard to the letter of the law), but you can turn your warnings way up high then make them errors:

g++ source.cpp -Wall -Wextra -pedantic -Werror

That'll cause most non-standard behaviours to be outright rejected; however, it will also turn some cautionary tales into errors which may or may not be what you want.

Use your best judgement to decide whether you want to use -Werror. Personally, I'd recommend it, unless you are forced not to by the requirement to include badly-written third-party library headers.

Still, you can always exclude specific warnings if they get in your way; e.g. by appending -Wno-unused-variable.

Consult your toolchain's documentation for available warning switches.

What this won't do is detect C idioms in standard-compliant C++ code. That's just not possible.
For that, you have better books, code review sessions and your conscience. :)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

How can I set the g++ compiler in Windows so that he won't compile if what I am writing is not pure C++ standard?

You cannot. I agree with most of what @Lightness said in his answer. It is not the compiler's job to warn you of all the ways you might be invoking undefined behaviour (some of which aren't even detectable at compile time).

However, I do not agree with the part of his answer where he equates -Wall -Wextra -pedantic with "warnings turned up way up high".

Quoting from the GCC manuals (v5.2) on what the options actually mean, emphasis mine:

-Wall

This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid [...]

-Wextra

This enables some extra warning flags that are not enabled by -Wall.

-pedantic

Issue all the warnings demanded by strict ISO C and ISO C++ [...]

Not all constructs that are "not pure C++" actually demand a warning by the standard. There are many things that even -Wall -Wextra -pedantic will not protect you from, and many things that are quite likely programming errors but not covered by the standard (e.g. -Wsign-conversion, -Wswitch-default, -Wlogical-op, -Woverloaded-virtual...)

So the recommendation here is, pick up the compiler manual, and check all the available warning options, selecting the strictest set you can live with. Repeat that process regularly.

Community
  • 1
  • 1
DevSolar
  • 67,862
  • 21
  • 134
  • 209