Karmah24
Karmah24

Reputation: 493

G++ Compiler warning when using c++ 17 updates

I'm running g++ compiler on windows 10 with mingw. On checking compiler version in cmd I get the following:

g++ --version- g++ (MinGW.org GCC Build-2) 9.2.0

same with c++ --version

When I compiled a cpp program making use of structured bindings I got a warning:

warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'

But otherwise the code ran fine. Does everyone get this warning or am I running a lower version compiler?

I'm using an extension - Competitive programming helper, and this warning interrupts the process. Hence, if it is the case that everyone gets this warning, is there a way I can block version specific warnings only without having to block all compiler warnings.

TIA.

Upvotes: 0

Views: 4073

Answers (1)

Akash papnai
Akash papnai

Reputation: 84

As the warning itself says:
structured bindings only available with '-std=c++17' or '-std=gnu++17'.
Meaning, you are using cpp version 14 or lower as compiler. However, you also have cpp 17 or higher installed on your system to get such kind of warning.
Your program will still run but with the warning.
For Windows:
If you are using VS code (assuming that you are already using code runner) then,

  1. Open Settings(UI) of VS code (Ctrl + ,)
  2. Search for Code-runner: Executor Map
  3. Click on Edit in settings.json
  4. Something like this will appear:
    enter image description here
  5. In cpp section: change it to "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" as in the picture above.

It is finally done now.

Below steps are for linux.

  1. Open .bashrc file using command sudo gedit ~/.bashrc in terminal.
  2. In the .bashrc file, add
    alias g++="g++ --std=c++2a" at the end of the file.
    This will always compile your file in cpp+20 version.

Once done, you will not get any warning.
Note: It is not mandatory to use cpp+20. You can use any version you like.
Change the version according to this website
https://www.learncpp.com/cpp-tutorial/configuring-your-compiler-choosing-a-language-standard/

Upvotes: 4

Related Questions