Automatically select a C++11-compatible g++ version in the Makefile - c++

The problem:
2 version of g++ installed on a computer running Ubuntu 12.04. They are the versions 4.6 and 5.2.
I have to compile a C++11 program using a Makefile. If I use g++ as compiler it calls automatically the version 4.6, it does not support c++11 so the compilation fails. I've followed a tutorial online, so that now if I call g++ it calls automatically the version 5.2 and now it works.
I find this solution not so good, since it works only on my PC. Is there a way to recognize in the Makefile if the default g++ version support C++11 and, in case not, switch to a more recent version?
Thank you!

Is there a way to recognize in the Makefile if the default g++ version support C++11 and, in case not, switch to a more recent version?
You can certainly detect the version of the default compiler available in PATH in your makefile. However, where do you search for another version?
The standard approach is to let the user specify the C compiler through CC and C++ compiler through CXX make variables, e.g.: make CC=/path/to/my/gcc CXX=/path/to/my/g++.

You can always select which gcc to use while invoking make
make CXX=/gcc/path/of/your/choice
otherwise you can detect gcc version using
ifdef CXX
GCC_VERSION = $(shell $(CXX) -dumpversion)
else
GCC_VERSION = $(shell g++ -dumpversion)
endif
in Makefile and while using, you can test if your gcc is >=4.6
ifeq ($(shell expr $(GCC_VERSION) '>=' 4.6), 1)
UPDATE: newer gcc needs -dumpfullversion together (icx is the CC from Intel OneAPI)
$ icx -dumpversion
14.0.0
$ gcc -dumpversion
9
$ icx -dumpfullversion -dumpversion
14.0.0
$ gcc -dumpfullversion -dumpversion
9.3.1

One very simple way is to use conditional statements in your makefile, and go for versions which you know are compatible, and only use the default gcc as a fallback. Here's a basic example:
CXX=g++
ifeq (/usr/bin/g++-4.9,$(wildcard /usr/bin/g++-4.9*))
CXX=g++-4.9
# else if... (a list of known-to-be-ok versions)
endif
The other, more robust method, is to generate your makefile using a script that checks for capabilities using test compilations, kind of like what ./configure usually does. I really don't mean to recommend autotools, though.

The thing to do is build your Makefile to use as many implicit rules as possible. By default compilation uses various environment variables.
The variable $(CXX) is the C++ compiler command and defaults to g++ on Linux systems. So clanging CXX to a different compiler executable will change the compiler for all implicit compile commands.
When you write explicit rules use the same variable that the implicit rules use. So instead of this:
program: program.cpp
g++ -o program program.cpp
Do this:
program: program.cpp
$(CXX) -o program program.cpp
Other variables you should use are:
CPPFLAGS = -Iinclude
CXXFLAGS = -std=c++14 -g3 -O0
Those are for pre-processing flags CPPFLAGS and compiler flags CXXFLAGS and library linking flags LDLIBS.
Using the default environment variables allows the person compiling the project the freedom to control the compilation for their desired environment.
See the GNU make manual

This works for me:
cmake_minimum_required(VERSION 2.8.11)
project(test)
if (${CMAKE_CXX_COMPILER_VERSION} LESS 5.0)
message(FATAL_ERROR "You need a version of gcc > 5.0")
endif (${CMAKE_CXX_COMPILER_VERSION} LESS 5.0)
add_executable(test test.cpp)

You can check in your source code the gcc version and abort compilation if you don't like it. Here is how it works:
/* Test for GCC > 4.6 */
#if !(__GNUC__ > 3 && __GNUC_MINOR__ > 6)
#error gcc above version 4.6 required!
#endif

Related

In Visual Studio Code: "no template named 'initializer_list' in namespace 'std'"? [duplicate]

I wanted to compile C++11 source code within Mac Terminal but failed. I tried g++ -std=c++11, g++ -std=c++0x, g++ -std=gnu++11 and g++ -std=gnu++0x but nothing worked. Terminal always read unrecognized command line option. However, g++ -std=gnu and things like that worked fine (of course C++11 source code could not pass).
Which option should I use to turn on C++11 support?
By the way, the command line tool I'm using is installed within Xcode, and I'm pretty sure that they are up-to-date.
As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.
clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.
xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.
For example you probably don't want the c++98 compatibility warnings. At the end of those warnings it shows the flag -Wc++98-compat and to disable them you pass -Wno-c++98-compat.
XCode uses clang and clang++ when compiling, not g++ (assuming you haven't customized things). Instead, try:
$ cat t.cpp
#include <iostream>
int main()
{
int* p = nullptr;
std::cout << p << std::endl;
}
$ clang++ -std=c++11 -stdlib=libc++ t.cpp
$ ./a.out
0x0
Thanks to bames53's answer for pointing out that I had left out -stdlib=libc++.
If you want to use some GNU extensions (and also use C++11), you can use -std=gnu++11 instead of -std=c++11, which will turn on C++11 mode and also keep GNU extensions enabled.

gcc-11 macOS doesn't compile [duplicate]

What is the difference between g++ and gcc? Which one of them should be used for general c++ development?
gcc and g++ are compiler-drivers of the GNU Compiler Collection (which was once upon a time just the GNU C Compiler).
Even though they automatically determine which backends (cc1 cc1plus ...) to call depending on the file-type, unless overridden with -x language, they have some differences.
The probably most important difference in their defaults is which libraries they link against automatically.
According to GCC's online documentation link options and how g++ is invoked, g++ is equivalent to gcc -xc++ -lstdc++ -shared-libgcc (the 1st is a compiler option, the 2nd two are linker options). This can be checked by running both with the -v option (it displays the backend toolchain commands being run).
GCC: GNU Compiler Collection
Referrers to all the different languages that are supported by the GNU compiler.
gcc: GNU C      Compiler
g++: GNU C++ Compiler
The main differences:
gcc will compile: *.c\*.cpp files as C and C++ respectively.
g++ will compile: *.c\*.cpp files but they will all be treated as C++ files.
Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).
gcc compiling C files has fewer predefined macros.
gcc compiling *.cpp and g++ compiling *.c\*.cpp files has a few extra macros.
Extra Macros when compiling *.cpp files:
#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern
For c++ you should use g++.
It's the same compiler (e.g. the GNU compiler collection). GCC or G++ just choose a different front-end with different default options.
In a nutshell: if you use g++ the frontend will tell the linker that you may want to link with the C++ standard libraries. The gcc frontend won't do that (also it could link with them if you pass the right command line options).
What is the difference between g++ and gcc?
gcc has evolved from a single language "GNU C Compiler" to be a multi-language "GNU Compiler Collection". The term gcc may still sometimes refer to the "GNU C Compiler" in the context of C programming.
man gcc
# GCC(1) GNU
#
# NAME
# gcc - GNU project C and C++ compiler
However, g++ is the C++ compiler for the GNU Compiler Collection. Like gnat is the Ada compiler for gcc. see Using the GNU Compiler Collection (GCC)
For example, the Ubuntu 16.04 and 18.04 man g++ command returns the GCC(1) manual page.
The Ubuntu 16.04 and 18.04 man gcc states that ...
g++ accepts mostly the same options as gcc
and that the default ...
... use of gcc does not add the C++ library. g++ is a program
that calls GCC and automatically specifies linking against the C++
library. It treats .c, .h and .i files as C++ source files instead of
C source files unless -x is used. This program is also useful when
precompiling a C header file with a .h extension for use in C++
compilations.
Search the gcc man pages for more details on the option variances between gcc and g++.
Which one should be used for general c++ development?
Technically, either gcc or g++ can be used for general C++ development with applicable option settings. However, the g++ default behavior is naturally aligned to a C++ development.
The Ubuntu 18.04 'gcc' man page added, and Ubuntu 20.04 continues to have, the following paragraph:
The usual way to run GCC is to run the executable called gcc, or machine-gcc when cross-compiling, or machine-gcc-version to run a specific version of GCC. When you compile C++ programs, you should invoke GCC as g++ instead.
Side Note: In the case of the Xcode.app embedded toolchain, g++ simply links to gcc. Thus, g++ invocations may vary on a per-toolchain basis.
ls -l /Applications/Xcode.app/Contents/Developer/usr/bin
# …
# lrwxr-xr-x 1 root wheel 3 Apr 27 2021 g++ -> gcc
# -rwxr-xr-x 1 root wheel 167120 Nov 23 20:51 gcc
### -- versus --
which -a g++
# /usr/bin/g++
ls -l /usr/bin/g++
# -rwxr-xr-x 1 root wheel 137616 Jan 1 2020 /usr/bin/g++
One notable difference is that if you pass a .c file to gcc it will compile as C.
The default behavior of g++ is to treat .c files as C++ (unless -x c is specified).
Although the gcc and g++ commands do very similar things, g++ is designed to be the command you'd invoke to compile a C++ program; it's intended to automatically do the right thing.
Behind the scenes, they're really the same program. As I understand, both decide whether to compile a program as C or as C++ based on the filename extension. Both are capable of linking against the C++ standard library, but only g++ does this by default. So if you have a program written in C++ that doesn't happen to need to link against the standard library, gcc will happen to do the right thing; but then, so would g++. So there's really no reason not to use g++ for general C++ development.
I became interested in the issue and perform some experiments
I found that description here, but it is very short.
Then I tried to experiment with gcc.exe and g++.exe on my windows machine:
$ g++ --version | head -n1
g++.exe (gcc-4.6.3 release with patches [build 20121012 by perlmingw.sf.net]) 4.6.3
$ gcc --version | head -n1
gcc.exe (gcc-4.6.3 release with patches [build 20121012 by perlmingw.sf.net]) 4.6.3
I tried to compile c89, c99, and c++1998 simple test files and It's work well for me with correct extensions matching for language
gcc -std=c99 test_c99.c
gcc -std=c89 test_c89.c
g++ -std=c++98 test_cpp.cpp
gcc -std=c++98 test_cpp.cpp
But when I try to run "gnu compiler collection" tool in that fashion:
$ gcc -std=c++98 test_cpp.c
cc1.exe: warning: command line option '-std=c++98' is valid for C++/ObjC++ but not for C [enabled by default]
But this one still work with no errors
$ gcc -x c++ -std=c++98 test_cpp.c
And this also
$ g++ -std=c++0x test_cpp_11.cpp
p.s. Test files
$ cat test_c89.c test_c99.c test_cpp.cpp
// C89 compatible file
int main()
{
int x[] = {0, 2};
return sizeof(x);
}
// C99 compatible file
int main()
{
int x[] = {[1]=2};
return sizeof(x);
}
// C++1998,2003 compatible file
class X{};
int main()
{
X x;
return sizeof(x);
}
// C++11
#include <vector>
enum class Color : int{red,green,blue}; // scoped enum
int main()
{
std::vector<int> a {1,2,3}; // bracket initialization
return 0;
}
Findings:
If look at process tree then it seems that gcc, and g++ is backend to other tools, which in my environment are: cc1plus.exe, cc1.exe, collect2.exe, as.exe, ld.exe
gcc works fine as metatool for if you have correct extension or set correct
-std -x flags. See this
“GCC” is a common shorthand term for the GNU Compiler Collection. This is both the most general name for the compiler, and the name used when the emphasis is on compiling C programs (as the abbreviation formerly stood for “GNU C Compiler”).
When referring to C++ compilation, it is usual to call the compiler “G++”. Since there is only one compiler, it is also accurate to call it “GCC” no matter what the language context; however, the term “G++” is more useful when the emphasis is on compiling C++ programs.
You could read more here.
I was testing gcc and g++ in a linux system. By using MAKEFILE, I can define the compliler used by "GNU make". I tested with the so called "dynamic memory" locating feature of "C plus plus" by :
int main(){
int * myptr = new int;
* myptr = 1;
printf("myptr[0] is %i\n",*myptr);
return 0;
}
Only g++ can successfully compile on my computer while gcc will report error
undefined reference to `operator new(unsigned long)'
So my own conclusion is gcc does not fully support "C plus plus". It seems that choosing g++ for C++ source files is a better option.
gcc and g ++ are both GNU compiler. They both compile c and c++. The difference is for *.c files gcc treats it as a c program, and g++ sees it as a c ++ program. *.cpp files are considered to be c ++ programs. c++ is a super set of c and the syntax is more strict, so be careful about the suffix.

i was trying to execute a simple c++ program in vs code but this gives me a lot of errors. also the colour of the "cout" keyword is not changing [duplicate]

What is the difference between g++ and gcc? Which one of them should be used for general c++ development?
gcc and g++ are compiler-drivers of the GNU Compiler Collection (which was once upon a time just the GNU C Compiler).
Even though they automatically determine which backends (cc1 cc1plus ...) to call depending on the file-type, unless overridden with -x language, they have some differences.
The probably most important difference in their defaults is which libraries they link against automatically.
According to GCC's online documentation link options and how g++ is invoked, g++ is equivalent to gcc -xc++ -lstdc++ -shared-libgcc (the 1st is a compiler option, the 2nd two are linker options). This can be checked by running both with the -v option (it displays the backend toolchain commands being run).
GCC: GNU Compiler Collection
Referrers to all the different languages that are supported by the GNU compiler.
gcc: GNU C      Compiler
g++: GNU C++ Compiler
The main differences:
gcc will compile: *.c\*.cpp files as C and C++ respectively.
g++ will compile: *.c\*.cpp files but they will all be treated as C++ files.
Also if you use g++ to link the object files it automatically links in the std C++ libraries (gcc does not do this).
gcc compiling C files has fewer predefined macros.
gcc compiling *.cpp and g++ compiling *.c\*.cpp files has a few extra macros.
Extra Macros when compiling *.cpp files:
#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern
For c++ you should use g++.
It's the same compiler (e.g. the GNU compiler collection). GCC or G++ just choose a different front-end with different default options.
In a nutshell: if you use g++ the frontend will tell the linker that you may want to link with the C++ standard libraries. The gcc frontend won't do that (also it could link with them if you pass the right command line options).
What is the difference between g++ and gcc?
gcc has evolved from a single language "GNU C Compiler" to be a multi-language "GNU Compiler Collection". The term gcc may still sometimes refer to the "GNU C Compiler" in the context of C programming.
man gcc
# GCC(1) GNU
#
# NAME
# gcc - GNU project C and C++ compiler
However, g++ is the C++ compiler for the GNU Compiler Collection. Like gnat is the Ada compiler for gcc. see Using the GNU Compiler Collection (GCC)
For example, the Ubuntu 16.04 and 18.04 man g++ command returns the GCC(1) manual page.
The Ubuntu 16.04 and 18.04 man gcc states that ...
g++ accepts mostly the same options as gcc
and that the default ...
... use of gcc does not add the C++ library. g++ is a program
that calls GCC and automatically specifies linking against the C++
library. It treats .c, .h and .i files as C++ source files instead of
C source files unless -x is used. This program is also useful when
precompiling a C header file with a .h extension for use in C++
compilations.
Search the gcc man pages for more details on the option variances between gcc and g++.
Which one should be used for general c++ development?
Technically, either gcc or g++ can be used for general C++ development with applicable option settings. However, the g++ default behavior is naturally aligned to a C++ development.
The Ubuntu 18.04 'gcc' man page added, and Ubuntu 20.04 continues to have, the following paragraph:
The usual way to run GCC is to run the executable called gcc, or machine-gcc when cross-compiling, or machine-gcc-version to run a specific version of GCC. When you compile C++ programs, you should invoke GCC as g++ instead.
Side Note: In the case of the Xcode.app embedded toolchain, g++ simply links to gcc. Thus, g++ invocations may vary on a per-toolchain basis.
ls -l /Applications/Xcode.app/Contents/Developer/usr/bin
# …
# lrwxr-xr-x 1 root wheel 3 Apr 27 2021 g++ -> gcc
# -rwxr-xr-x 1 root wheel 167120 Nov 23 20:51 gcc
### -- versus --
which -a g++
# /usr/bin/g++
ls -l /usr/bin/g++
# -rwxr-xr-x 1 root wheel 137616 Jan 1 2020 /usr/bin/g++
One notable difference is that if you pass a .c file to gcc it will compile as C.
The default behavior of g++ is to treat .c files as C++ (unless -x c is specified).
Although the gcc and g++ commands do very similar things, g++ is designed to be the command you'd invoke to compile a C++ program; it's intended to automatically do the right thing.
Behind the scenes, they're really the same program. As I understand, both decide whether to compile a program as C or as C++ based on the filename extension. Both are capable of linking against the C++ standard library, but only g++ does this by default. So if you have a program written in C++ that doesn't happen to need to link against the standard library, gcc will happen to do the right thing; but then, so would g++. So there's really no reason not to use g++ for general C++ development.
I became interested in the issue and perform some experiments
I found that description here, but it is very short.
Then I tried to experiment with gcc.exe and g++.exe on my windows machine:
$ g++ --version | head -n1
g++.exe (gcc-4.6.3 release with patches [build 20121012 by perlmingw.sf.net]) 4.6.3
$ gcc --version | head -n1
gcc.exe (gcc-4.6.3 release with patches [build 20121012 by perlmingw.sf.net]) 4.6.3
I tried to compile c89, c99, and c++1998 simple test files and It's work well for me with correct extensions matching for language
gcc -std=c99 test_c99.c
gcc -std=c89 test_c89.c
g++ -std=c++98 test_cpp.cpp
gcc -std=c++98 test_cpp.cpp
But when I try to run "gnu compiler collection" tool in that fashion:
$ gcc -std=c++98 test_cpp.c
cc1.exe: warning: command line option '-std=c++98' is valid for C++/ObjC++ but not for C [enabled by default]
But this one still work with no errors
$ gcc -x c++ -std=c++98 test_cpp.c
And this also
$ g++ -std=c++0x test_cpp_11.cpp
p.s. Test files
$ cat test_c89.c test_c99.c test_cpp.cpp
// C89 compatible file
int main()
{
int x[] = {0, 2};
return sizeof(x);
}
// C99 compatible file
int main()
{
int x[] = {[1]=2};
return sizeof(x);
}
// C++1998,2003 compatible file
class X{};
int main()
{
X x;
return sizeof(x);
}
// C++11
#include <vector>
enum class Color : int{red,green,blue}; // scoped enum
int main()
{
std::vector<int> a {1,2,3}; // bracket initialization
return 0;
}
Findings:
If look at process tree then it seems that gcc, and g++ is backend to other tools, which in my environment are: cc1plus.exe, cc1.exe, collect2.exe, as.exe, ld.exe
gcc works fine as metatool for if you have correct extension or set correct
-std -x flags. See this
“GCC” is a common shorthand term for the GNU Compiler Collection. This is both the most general name for the compiler, and the name used when the emphasis is on compiling C programs (as the abbreviation formerly stood for “GNU C Compiler”).
When referring to C++ compilation, it is usual to call the compiler “G++”. Since there is only one compiler, it is also accurate to call it “GCC” no matter what the language context; however, the term “G++” is more useful when the emphasis is on compiling C++ programs.
You could read more here.
I was testing gcc and g++ in a linux system. By using MAKEFILE, I can define the compliler used by "GNU make". I tested with the so called "dynamic memory" locating feature of "C plus plus" by :
int main(){
int * myptr = new int;
* myptr = 1;
printf("myptr[0] is %i\n",*myptr);
return 0;
}
Only g++ can successfully compile on my computer while gcc will report error
undefined reference to `operator new(unsigned long)'
So my own conclusion is gcc does not fully support "C plus plus". It seems that choosing g++ for C++ source files is a better option.
gcc and g ++ are both GNU compiler. They both compile c and c++. The difference is for *.c files gcc treats it as a c program, and g++ sees it as a c ++ program. *.cpp files are considered to be c ++ programs. c++ is a super set of c and the syntax is more strict, so be careful about the suffix.

How to change default GCC compiler to be used with MPI on Linux CentOS

I have two GCC compilers installed on a Linux (CentOS) machine. The old version of GCC (4.4.7) is in the default folder (came with CentOS) and the newer one that I intend to use is in /usr/local/gcc/4.9.3/. My code utilizes MPI and LAPACK/LAPACKE/BLAS libraries and with the old GCC I used to compile source (for example “main.cpp”) like this:
mpiCC main.cpp -o main -L/home/USER1/lapack-3.6.1 -llapacke -llapack -lblas -lm –Wall
This still invokes the old GCC 4.4.7. What should I modify so the above MPI compilation (mpiCC) invokes GCC 4.9.3 executable from the new location at /usr/local/gcc/4.9.3/el6/bin/ ?
From MPICH Installer's Guide version 3.2 (page 6):
"The MPICH configure step will attempt to find the C, C++, and Fortran compilers for you, but if you either want to override the default or need to specify a compiler that configure doesn't recognize, you can specify them on the command line [...]. For example, to select the Intel compilers instead of the GNU compilers on a system with both, use"
./configure CC=icc CXX=icpc F77=ifort FC=ifort ...
Is there a way to dicriminate between different version of GCC compilers in ./configure ?
I guess mpiCC uses the first gcc compiler found in the $PATH variable.
You should be able to set the new version of gcc by running:
PATH="/usr/local/gcc/4.9.3/el6/bin:$PATH" mpiCC main.cpp -o main -L/home/USER1/lapack-3.6.1 -llapacke -llapack -lblas -lm –Wall
If you really want two versions of GCC installed at the same time and use both of them here is a good link that explains how to do this:
http://gcc.gnu.org/faq.html#multiple
Finally found how. Here is the recipe:
1) check your if you shell is bash, if not set it to bash: $ echo $SHELL
/bin/tcsh
It was tcsh and needed to be set to bash.
2) Switch to bash: $ bash
bash-4.1$
3) Add new version of GCC to the front of the PATH:
bash-4.1$ export PATH=/usr/local/gcc/4.9.3/el6/bin:$PATH
4) Check the PATH: bash-4.1$ echo $PATH
/usr/local/gcc/4.9.3/el6/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin
5) Check version of GCC used (It picks up the first GCC from the PATH):
bash-4.1$ gcc --version
gcc (GCC) 4.9.3
Note: this is just for the current session.

What's the point of including -std=c++0x in a G++ compile command?

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