How to use C++ 20 in g++ - c++

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.

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 do I make sure I am utilizing C++14?

I am trying to compile some c++ I got from a book I am going through, when I try to compile I get this warning followed by 5 related errors.
main.cpp:16:9: warning: variable templates are a C++14 extension
[- Wc++14-extensions]
int table<RecordType>::CAPACITY;
^
I have never given a thought to updating c++ or being certain of what version I am using. I am compiling this in a mac using g++.
You may enable it with -std=c++14 flag. However, your GCC version should support it in the first place. Till GCC 4.9.3, you could use -std=c++1y whereas since GCC 5.2, it supports c++14 flag as well. For more info, refer to this.
Pass the -std=c++14 flag. There are also older versions with partial C++14 support which don't support -std=c++14 yet; for these, pass the -std=c++1y flag.
You need to tell the compiler which version of the standard to compile to.
Try g++ -std=c++14.
While -std=c++14 that others are recommending will enable C++14 feature support, it will also disable a bunch of things that are enabled by default, including support for advanced POSIX APIs.
Unless you specifically want to disable G++ extensions, you should use -std=gnu++14 not -std=c++14

I installed GCC 5.2 from source and I don't know how to uninstall it on Ubuntu 15.04

The other week I installed GCC 5.2 from source on my ubuntu machine. I wanted to be able to use fully supported cilk features. Now I would like to revert back to GCC 4.9. How can I uninstall GCC 5.2? I tried using make uninstall but as I understand this is not supported.
I believe you don't need to revert to the system compiler; it is a matter of path. Or set up your $PATH appropriately. Use /usr/bin/g++ for the system compiler, and probably /usr/local/bin/g++ for the compiler you have built from source code from GCC
BTW, you probably could use your GCC 5.2 for almost all your future builds
It depends how you have configured it. You should have configured it with ../gcc-5.2/configure --program-suffix=-my-5.2 then you would use g++-my-5.2 instead of g++
Try to type g++ -v (i.e. probably /usr/local/bin/g++ -v) to understand how it was configured.
You probably could remove the gcc and g++ binaries under /usr/local/bin/ and several other files and directories under /usr/local/ (but be careful).
Indeed, GCC does not support make uninstall

Compile c++14-code with g++

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.

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.