Compile c++14-code with g++ - c++

I'm using g++ 4.8.4 on Ubuntu 14.04 LTS. When trying to compile with '-std=c++14', I get this error:
g++: error unrecognized command line option '-std=c++14'
Compiling with '-std=c++11' works fine, so I'm not sure what's going on. Does g++ really have no support for c++14 yet? Am I using a wrong command line option?
I used "sudo apt-get install g++" which should automatically retrieve the latest version, is that correct?

For gcc 4.8.4 you need to use -std=c++1y in later versions, looks like starting with 5.2 you can use -std=c++14.
If we look at the gcc online documents we can find the manuals for each version of gcc and we can see by going to Dialect options for 4.9.3 under the GCC 4.9.3 manual it says:
‘c++1y’
The next revision of the ISO C++ standard, tentatively planned for 2014. Support is highly experimental, and will almost certainly change in incompatible ways in future releases.
So up till 4.9.3 you had to use -std=c++1y while the gcc 5.2 options say:
‘c++14’
‘c++1y’
The 2014 ISO C++ standard plus amendments. The name ‘c++1y’ is deprecated.
It is not clear to me why this is listed under Options Controlling C Dialect but that is how the documents are currently organized.

The -std=c++14 flag is not supported on GCC 4.8. If you want to use C++14 features you need to compile with -std=c++1y. Using godbolt.org it appears that the earilest version to support -std=c++14 is GCC 4.9.0 or Clang 3.5.0

G++ does support C++14 both via -std=c++14 and -std=c++1y. The latter was the common name for the standard before it was known in which year it would be released. In older versions (including yours) only the latter is accepted as the release year wasn't known yet when those versions were released.
I used "sudo apt-get install g++" which should automatically retrieve the latest version, is that correct?
It installs the latest version available in the Ubuntu repositories, not the latest version that exists.
The latest GCC version is 5.2.

Follow the instructions at https://gist.github.com/application2000/73fd6f4bf1be6600a2cf9f56315a2d91 to set up the gcc version you need - gcc 5 or gcc 6 - on Ubuntu 14.04. The instructions include configuring update-alternatives to allow you to switch between versions as you need to.

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.

How to use C++ 20 in g++

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.

getting c++11 - compliant compiler

This all seems like a colossal mess.
All I want is a compiler that implements C++11, so I can use <chrono>. But I'm so confused from the very beginning.
Currently, I build programs by invoking G++, but when I check the version via $ g++ -v, I get:
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build
2336.11.00)
What's going on? Am I using G++? GCC? LLVM? I don't even know. Are they the same thing?
So now I'm trying to build and download GCC 4.7 via gnu.org, but I have no idea what any of the guides are talking about. I've never seen so many acronyms for things I dont know.
Why is this so complicated? What's with all those versions, with some of them only implementing some parts of C++11 and not others?
Here's the situation on OS X.
There are two C++ compilers installed by default.
[5:49pm][wlynch#watermelon ~] g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
[5:49pm][wlynch#watermelon ~] clang++ --version
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
g++ is running llvm-gcc, which is the gcc frontend, and then the llvm backend.
clang++ is running clang, which is the clang frontend and then the llvm backend.
If you want a C++11 compiler on OS X without installing other packages, your only option is to use the clang compiler.
The flags necessary are:
clang++ -stdlib=libc++ -std=gnu++11
To describe the two flags I'm passing:
-stdlib=libc++ uses the libc++ standard library, instead of the gnu libstdc++. On OS X, the libc++ version has c++11 support. The gnu libstdc++ one does not.
-std=gnu++11 tells the compiler to support c++11 code features, like lambdas and enum class. You can also pass -std=c++11, which is similar, but does not enable some commonly expected gnu extensions.
Update for OS X 10.9: As of OS X Mavericks, both g++ and clang++ are actually using clang. The only difference, is that g++ will imply -stdlib=libstdc++ and clang++ will imply -stdlib=libc++. So, on Mavericks, if you'd like to use C++11, you can follow the above advice, or just do:
clang++ -std=gnu++11
Update for OS X 10.10: As of OS X Yosemite, g++ is still clang in disguise. However, neither uses libstdc++ by default anymore. Both are now on libc++.
It sounds like you have Xcode 4.6 and the latest command line tools. This is from the release notes:
Important: The LLVM GCC compiler does not include the latest Objective-C and
C++11 features. Xcode 4.6 is the last major Xcode release which includes the
LLVM GCC compiler and the GDB debugger. Please migrate your projects to use the
LLVM compiler and LLDB debugger…
I think you want to use c++ instead:
$ c++ -v
Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
#sharth: The situation changed on the Mac quite significantly since the release of XCode 5.0. clang/clang++ are the default C/C++ compilers. They correspond to the LLVM version 3.3 I believe, and this version of clang++ is fully C++11-compliant. Note that clang++ --version will return a version number like "5.0.x" on the Mac but that refers to the XCode version.
I have been using the Apple clang++ in a C++11 project for months now and so far I have not seen any problems. There is absolutely no reason to use any other C++ compiler on the Mac just now :-)
The situation with GCC/G++ is not so rosy. The latest version of G++ (4.8.2) does implement most of the C++11 standard, however the standard library is not compliant! For instance, std::regex is not implemented in libstdc++, but you find this out only when you run your code and the regex constructors throw std::regex_error -s. (I found this out the hard way when trying to port the aforementioned little project to Linux.) The community believes full compliance will be achieved with the 4.9 release of G++. Until then you should use the Clang compilers on Linux as well.
I have no access to the latest Intel C++ compiler suite so have no idea how compliant icpc is.

Know g++ Version of Code blocks in Windows

I am solving questions on Interviewstreet.com. They said they use C++ version g++ 4.6.3,C0x mode.
I am writing code on code blocks. So i want to know which version iam using in code blocks is it in C0x mode or C11 mode??
I have tried using g++ --version i got g++ TDM-2 mingw32 4.4.1.Can u tell me where i can get this kind of information.
what is the difference between C++ 0x and C++11??
You'll have to update the version of g++ to 4.6.3 (or later) if you want to use c++11 features. See this question and it's answers on how to do it for deb linux.
Then you'll have to pass --std=c++0x to the compiler in options. You should be able to easily find them in codeblocks.
what is the difference between C++ 0x and C++11??
c++0x is a synonym for c++11.
The command:
g++ --version
gives you the version of your g++ or mingw compiler. Since you got g++ TDM-2 mingw32 4.4.1 then your version is 4.4.1. If you want to use version 4.6.3 as in that web site, then you would have to update.
It wouldn't hurt to use a newer than 4.6.3 version of mingw, so please see here for the latest version. This page offers an windows installer for mingw.
After installation, you would have to configure CodeBlocks to use the newly installed compiler by looking into Compiler and debugger settings -> Toolchain executables tab and setting the paths for the compiler-related executables to the new ones.
Hope this helps.
EDIT:
Here is a small tutorial/example of what the CodeBlocks settings look like.

Updating Apple g++/gcc

What is the difference between Apple gcc and GNU gcc? Is Apple gcc a superset of the standard one?
The g++ version information in my OSX shows:
$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
Some of the latest features of C++11 are in gcc 4.3/4.4 as per this. Is there any newer version of Apple gcc I can upgrade to. if so, how can i do it? I have Xcode 4.1
Well, for the first part, Apple in this case is using the LLVM backend for g++ as the default g++. Apple also installs the wonderfully named clang and clang++ front-ends for LLVM. However, there is absolutely nothing stopping you from installing newer branches of GCC; MacPorts has packages for everything up to 4.6. If you look for "APPLE ONLY" in the gcc man page, you can see what won't be available outside of Apple branches.
Beside the already mentioned llvm-gcc and clang, there is also an Apple-supplied gcc-4.2 (without LLVM backend) at /usr/bin/gcc-4.2 in Xcode 4.1. But do not overwrite the Apple-supplied versions in /usr/bin. All three support a superset of features include multi-arch support and multi-abi support not found in the vanilla GNU distributions and many third-party packages depend on these features in OS X. If you install something via MacPorts or from source, it will be installed to a different path, like /opt/local/bin or /usr/local/bin. Use PATHs or environment variables to manage which compiler you use.
You can use macport to install newer versions. You can download it here. Once you have installed gcc with macport, you can use it with xcode by adding an user-defined setting to your build :
- Go to the build setting of your project
- Click on the add build setting button
- Choose user-defined setting
- Name it CC
- In the value field, put the path of the gcc version installed by macport.
One thing that definitely is present in the Apple GCC branch but not in GNU GCC is blocks.