user13669314
user13669314

Reputation:

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

Upvotes: 60

Views: 167843

Answers (4)

TonyParker
TonyParker

Reputation: 2474

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

Upvotes: 78

Yılmaz Durmaz
Yılmaz Durmaz

Reputation: 3024

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.

Upvotes: 1

Yvan Pearson
Yvan Pearson

Reputation: 329

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

Upvotes: 8

Gabriel Deml
Gabriel Deml

Reputation: 193

I would try updating gcc. C++ 20 was introduced in gcc version 8 which is pretty new.

Upvotes: 15

Related Questions