12

I am using Atom as my IDE, my current __cplusplus = 201402 which is C++14 and my compiler is g++ (GCC) 9.2.0.

How do I upgrade to C++17 or C++20?

Everything I've searched up involves using another IDE (Microsoft Visual Studio).

Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
yellowgrass
  • 121
  • 1
  • 1
  • 7
  • 2
    `g++ -std=c++17` - https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/ – Jesper Juhl Jul 21 '20 at 05:01
  • and use clang-tidy to help you upgrade your code to C++17 – schorsch312 Jul 21 '20 at 05:18
  • gcc 9.2 supports the majority of C++17, but doesn't enable it by default. Use `g++ -std=c++17` to enable support. C++2a has partial support in gcc 9.2, and that can be enabled using `-std=c++2a`. You'll need to upgrade to a more recent version of gcc to have more complete support of C++20 - however, at present, there isn't complete support of C++2a in gcc. – Peter Jul 21 '20 at 05:20
  • Install a compiler that has the features you need. In Linux, typically by upgrading your version of Linux. I say that because the features provided are the compiler are a gradient, not an absolute. C++14,C++17,C++20 ... its misleading. A compiler at a certain version may have some features but not all. See: https://en.cppreference.com/w/cpp/compiler_support – Timothy John Laird May 02 '21 at 01:51
  • Real question is: what do you use as a build manager? Feeding compiler options directly to compiler is a crude solution. – Marek R Sep 27 '21 at 12:03

2 Answers2

22

You don't "upgrade" to newer C++ standards.
You can upgrade compiler to newer version supporting latest standards.

As of today, most compilers are set to C++14 by default.
To change it you need to pass additional argument during compilation.

For example, to compile hello.cpp with GCC for C++17 you need to execute

g++ -std=c++17 hello.cpp

You need to check how to pass compiler flags (or set standards) in your IDE / editor / build system.


I'm not familiar with Atom, but I've found this:

In the settings, click on Packages, then search for gpp-compiler. You should see a settings button – click on it and edit the command line options to suit your needs.

Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
6

Do-it-yourself:

#include <iostream>

int main(void) {
    std::cout << __cplusplus;

    return 0;
}

Compile this firstly with the following command:

$ g++ -o main main.cpp && ./main

Thereafter:

g++ -o main main.cpp -std=c++17 && ./main

You'll get to know the differences. Note that if you're unable to use -std=c++20 flag, it clearly means that your compiler doesn't supports C++20 standard.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34