3

I have recently started learning C++ and, since I'm on Linux, I'm compiling using G++.

Now, the tutorial I'm following says

If you happen to have a Linux or Mac environment with development features, you should be able to compile any of the examples directly from a terminal just by including C++11 flags in the command for the compiler:

and tells me to compile using this command: g++ -std=c++0x MY_CODE.cpp -o MY_APP.

Now, what I'm wondering, what is the point of the std=c++0x flag? Is it required, or can I just run g++ MY_CODE.cpp -o MY_APP?

RPiAwesomeness
  • 5,009
  • 10
  • 34
  • 52
  • Haven't used GCC in a while, but last time I checked, the default mode for C++ code was C++03, so if you wanted to use C++11, you had to specify this flag explicitly. (BTW, why don't you try it?) – The Paramagnetic Croissant Nov 08 '14 at 22:19
  • 2
    Use `-std=c++11` GCC versions that don't support that are getting more outdated. – chris Nov 08 '14 at 22:20
  • `-std=c++11` turns on certain language features that are no available on older versions of standard C++ – Galik Nov 08 '14 at 22:26
  • This is supposed to become unnecessary with gcc-5, which should default to -std=gnu++11. The switch hasn't happened yet though, so who knows... – Marc Glisse Nov 09 '14 at 14:08

3 Answers3

12

By default, GCC compiles C++-code for gnu++98, which is a fancy way of saying the C++98 standard plus lots of gnu extenstions.

You use -std=??? to say to the compiler what standard it should follow.
Don't omit -pedantic though, or it will squint on standards-conformance.

The options you could choose:

standard          with gnu extensions

c++98             gnu++98
c++03             gnu++03
c++11 (c++0x)     gnu++11 (gnu++0x)
c++14 (c++1y)     gnu++14 (gnu++1y)

Coming up:

c++1z             gnu++1z (Planned for release sometime in 2017, might even make it.)

GCC manual: https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Standards.html#Standards

Also, ask for full warnings, so add -Wall -Wextra.

There are preprocessor-defines for making the library include additional checks:

  • _GLIBCXX_CONCEPT_CHECKS to add additional compile-time-checks for some templates prerequisites. Beware that those checks don't actually always do what they should, and are thus deprecated.
  • _GLIBCXX_DEBUG. Enable the libraries debug-mode. This has considerable runtime-overhead.
  • _GLIBCXX_DEBUG_PEDANTIC Same as above, but checks against the standards requirements instead of only against the implementations.
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • So, `-pendantic` makes it so...g++ is less strict? – RPiAwesomeness Nov 08 '14 at 22:39
  • @RPiAwesomeness: No `-pedantic` makes it so that GCC actually remembers you asked for standard conformance. Otherwise, it is far too lenient. – Deduplicator Nov 08 '14 at 22:41
  • 2
    Excellent presentation. But since you mention `-Wall -Wextra`, you should also mention `-D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_GLIBCXX_CONCEPT_CHECKS`, which do the same thing for the library. They should be present in every debug build. (For release builds, the first two can impact performance enormously, and so may have to be removed.) – James Kanze Nov 09 '14 at 01:14
  • And the related `-std=` options for C: `c90`, `c99`, `c11` and `gnu90` (the default for C), `gnu99` or `gnu11`. – Michael Burr Nov 09 '14 at 05:33
  • 1
    Concept checking is deprecated and breaks valid code. You can mention it, but only with a big warning sign... – Marc Glisse Nov 09 '14 at 14:05
  • @MichaelBurr When you mentioned c90 that brought up a compiler warning I got later on. I ran `g++ -Wall -o "game_stats2" "game_stats2.cpp"` and this warning came up: `game_stats2.cpp:34:2: warning: this decimal constant is unsigned only in ISO C90 [enabled by default]`. Is this because I didn't include the `-std=c++0x` flag? – RPiAwesomeness Nov 09 '14 at 22:42
  • @RPiAwesomeness: In C90 a decimal integer constant without a type suffix that can't fit in a `long int` will be an `unsigned long int` int (if possible). That doesn't happen in C99 or any of the C++ standards. I suspect that you have such an decimal literal - depending on the standard in effect, it might be a `long long int` (C99/C++11 or later) or undefined behavior (C++98, C++03). The warning is just that: warning you that you might be getting something you aren't expecting. For more detail see http://stackoverflow.com/a/20910712/12711 – Michael Burr Nov 10 '14 at 02:06
4

You want to use the C++11 standard (and you are right to want that), but C++11 made a huge progress w.r.t. its older C++98 standard.

But old versions of GCC (i.e. GCC 4.8 or earlier) where not finalized before the standard itself (so they accepted the -std=c++0x flag). I strongly recommend (if you want C++11) to use the latest version of GCC, that is GCC 4.9. A bug fixing GCC 4.9.2 release appeared at end of october 2014. So use it please, and pass it the std=c++11 flag to tell the compiler you want C++11 conformance.

I actually suggest to pass std=c++11 -Wall -Wextra -g to get C++11, all warnings, and debug info. Once you have debugged your program (with gdb, and you'll better also use a recent version of gdb!) you might ask the compiler to optimize with -O2 (and perhaps -mtune=native if you want to optimize for your own computer)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • So, should I download GCC 4.9.2 from [here](mirrors.concertpass.com/gcc/snapshots/LATEST-4.9/)? That seems to be a Nightly/Bleeding edge release...where is a Stable 4.9.2 that I can download? – RPiAwesomeness Nov 08 '14 at 22:29
  • 1
    You might download the source code from [GNU GCC mirrors](https://gcc.gnu.org/mirrors.html), then you'll need to configure, build and install it. Compile in a build tree outside of the source tree, and read the [installation instructions](https://gcc.gnu.org/install/) – Basile Starynkevitch Nov 08 '14 at 22:31
1

Source for your reference:

main.cpp

#include <iostream>
using namespace std;

int main()
{
    cout << "Test main CPP" << endl;
    return 0;
}

build.sh

rm demoASI*
echo "**cleaned !!**"

##### C++ 11 Compliance #####
# type ONE
g++ -o demoASI_1 -std=c++0x main.cpp
echo "**rebuild-main-done (C++ 11 Compilation) !**"

# type TWO
g++ -o demoASI_2 -std=c++11 main.cpp
echo "**rebuild-main-done (C++ 11 Compilation) !**"

##### C++ 11+ Compliance #####
# type THREE
g++ -o demoASI_3 -std=c++1y main.cpp
echo "**rebuild-main-done (C++ 11+ (i.e. 1y, but not C++14) Compilation) !**"

###### C++ 14 Compliance  ######
# type FOUR
g++ -o demoASI_4 -std=c++14 main.cpp
if [ $? -eq 0 ]
then
    echo "**rebuild-main-done (C++ 14 Compilation) !** :: SUCCESS"
else
    echo "**rebuild-main-done (C++ 14 Compilation) !** :: FAILED"
fi

Now, execute the script as; ./build.sh (assuming build.sh has execution permission)

You can first check the version of your g++ compiler, as;

g++ --version

The version of g++, after 4.3, has support for the c++11.

Please see, for c++14 support info in compiler.

parasrish
  • 3,864
  • 26
  • 32