how to change gcc compiler to c++11 on ubuntu - c++

I use ubuntu 12.04 and the default gcc is 4.6.3. It is not accepting c++11 commands and is giving me output saying the command is not c++98 compatible. I checked online and have seen people advising to not change default compilers on operating system as it becomes unstable. Can anybody suggest a fix or a safe way of downloading a gcc compiler that is c++11 compliant.

As others have suggested, you need to enter the std commandline option. Let us make it easy for you
Open terminal by pressing Ctrl+Alt+T
sudo gedit ~/.bashrc
Enter the following line as the last line
alias g++="g++ --std=c++0x"
Save and close the file and close the terminal.
Now open terminal again and compile your c++ 11 programs simply by g++ filename.cpp
Thats it. By default it will compile for c++11 standard.
NOTE: If you follow the above mentioned option, to compile non-c++ 11 programs, you have to use
g++ --std=c++98 filename.cpp

gcc 4.6.3 supports many c++11 features. However, they are disabled by default. To enable them, use the following flag:
g++ -std=c++0x ...
This flag also disables GNU extensions; to keep them enabled, use -std=gnu++0x flag.

Related

Can't get VSCode to use a modern C++ compiler on M1 MacBook [duplicate]

I am trying to access std::popcount, but it seems like it's only there in C++ 20.
When I try compiling with g++ -std=c++20 main.cpp, it says g++: error: unrecognized command line option '-std=c++20'; did you mean '-std=c++03'
How do I tell g++ to use c++ 20?
I am using Ubuntu 18.04
C++20 features are available since GCC 8.
To enable C++20 support, add the command-line parameter
-std=c++20
For G++ 9 and earlier use
-std=c++2a
Or, to enable GNU extensions in addition to C++20 features, add
-std=gnu++20
I would try updating gcc. C++ 20 was introduced in gcc version 8 which is pretty new.
If it's an option you can update to Ubuntu 20.04 LTS which includes GCC version 9 out of the box. This would enable you to use C++ 20 and thus std::popcount
Note: use -std=c++2a in GCC 9 and earlier
there are different versions of the compiler exist and g++ is usually linked to the older one. for me, the current one is g++-9 and it clearly does not understand C++20.
C++20 requires installing gcc-10 and g++-10 (plus dependencies). assuming you already have them installed, then you need to run:
g++-10 -std=c++20 main.cpp
PS: if you want to go with v10 as default, then update links for gcc, g++ and other related ones, and use v9 (or whatever old you have) by full name.
EDIT: depending on the host OS, v11 and v12 could also be installed, but the naming is still important. replace with g++-11 or g++-12.

unrecognized command line option “-std=c++11”

I am trying to run wiringPi Cpp version with raspberryPi. I downloaded this and tryed to run but I go the error below.
What is the problem here? Did I do something wrong?
Upgrade your GCC. You appear to have GCC 4.6, and the flag you mention was introduced with GCC 4.7.
Run g++ -v on the command line. If the version number is 4.3 through 4.6 replace -std=c++11 with -std=c++0x and see if you get any love. If that still doesn't work, you'll have upgrade the compiler or remove the c++11 features from the library. Seriously recommend the former over the latter.
If the compiler is before version 4.3 definitely upgrade.
edit
I need to read more goodly. skip getting the version number. Try -std=c++0x, then upgrade the compiler if it fails.

command to compile c++ program on live Fedora

I'm using Fedora 20 live with the help of a DVD instead of installing it. I have used the following command to compile c++ program:
g++ programname.cpp
But it displayed this:
bash: g++: command not found...
Is there any other alternative command?
Why g++ command is not working for me?
Is using Live Fedora the reason??
You can also try c++, which is usually a symlink to the currently installed C++ compiler. If you don't have that, try clang++.
The g++ command is part of the GNU C/C++ compiler gcc. Using the g++ command specifies that you want to compile a C++ program, but it's actually just a link to the gcc command. Try using gcc instead of g++. You may have problems with that because it will try to compile it as a C program, but I think there is a compiler flag that will force it to compile it as a C++ program. If none of this works, then you probably don't have a compiler installed. I am not familiar with Fedora's package manager, but the package you want will probably be called either gcc or g++.

Is g++ compiler the same with the gcc compiler for C++? (Mac OS X)

I just need to make clear one thing. In University we are learning the C++ programming language and they suggest us to use the GNU C++ Compiler which is part of the GCC.
So on my Mac OS X Mavericks I download the command line tools from the developers.apple.com.
I wrote a simple C++ program and I compile this program using the g++ command like this:
g++ program.cpp
./a.out
And the program runs perfect. But as I know, using a different compiler, means that you have to use the correct syntax/commands/libraries for this spesific compiler, so while in the University we use the "GNU C++ compiler", I just want to make clear that with the g++ command is meant that I use the "GNU C++ Compiler".
Cheers.
Traditionally, gcc and g++ are both components of the GNU C compiler suite. gcc is the C compiler, and g++ is the C++ compiler.
On current versions of Mac OS X, the commands gcc and g++ are both treated as alternate names for clang and clang++, which are components of the Clang C compiler. However, this compiler is almost entirely compatible with GCC — the few differences that do exist will almost certainly not come up in the coursework you're doing.
(The most significant difference is that Clang's diagnostics are much better: it will point out exactly where a syntax error occurs in a line, rather than just what line it's on, and it can often identify potential typos or subtle mistakes in situations where GCC would just give you a cryptic error message. If you're just learning C, you will appreciate this a lot.)
As per #duskwuff, I would prefer to use clang++, however if you must be compatible, then you can installi the real GNU compiler via macports.
After installing macports (which includes a xcode-select step), simply do:
$ sudo port selfupdate
$ sudo port install gcc46
(or gcc47, etc.)
The compiler will be in your $PATH (if you set-up macports correctly), but explicitly, it will be /opt/local/bin/gcc46 (see sudo port select gcc).

How to use std::thread of C++ 11 under Cygwin GCC 4.7.2

I've been trying to compile a multithread hello-world program under Cygwin using the newly introduced C++ 11 std::thread feature without success. I compiled and installed GCC 4.7.2 by myself, and the same code works without any problems under Linux with the same version of GCC. The first error I got was that the compiler did not recognize the -pthread flag. After researching on it for a while I noticed someone said on Cygwin this flag should be -lthread. I made the change and that error was gone, but another series of errors occur telling me thread is not member of std. I wonder if it's caused by the wrong configuration of the compiler during installation, or std::thread is simply not supported under Cygwin?
This looks like you did not compile the program with the appropriate standard library flag. If you want to compile for C++11 you should use:
g++ --std=c++0x -o ...
The --std flag sets the appropriate language compatibility level. If this does not help, please post the error messages you got as a source listing.