I have the Windows 10 OS, use VS Code to write my C++ code and use CMD to compile my programs. I don't really know which standard the compiler in my PC (MinGW, gcc version 6.3.0) uses, but I just want to endure that it uses the latest one like C++14 or 17. Unfortunately, I need to type in -std=c++17
every time I need to compile my program using that standard. How do I set the desired standard as default?
-
11) Related (possibly duplicate): [Change default C++ standard in g++](https://stackoverflow.com/questions/41648978/change-default-c-standard-in-g). 2) Since you are on Windows, any particular reason, why you aren't using Visual Studio 2017/2019 Community Edition? – Algirdas Preidžius Aug 19 '19 at 13:44
-
_I don't really know which standard the compiler in my PC (MinGW, gcc version 6.3.0) uses_ In this case, this Q/A might be interesting: [SO: Which C++ standard is the default when compiling with g++?](https://stackoverflow.com/a/44735016/7478597). ;-) – Scheff's Cat Aug 19 '19 at 13:49
-
I just [**tried on coliru**](http://coliru.stacked-crooked.com/a/955202e327454d7b) which has a brand new gcc 9.2.0. Output `#define __cplusplus 201402L`. So, I don't see a way without `-std=c++17` if you need C++17. (The older gcc 6.3 might even have C++11 as default.) – Scheff's Cat Aug 19 '19 at 13:53
-
If you use a `make` file, you don't have to keep typing the version. You would set up the `CPP` or compiler definition variable and the parameters to it. – Thomas Matthews Aug 19 '19 at 14:58
-
I think this is a valid question for Stack Overflow because it involves software directly related to programming. – L. F. Aug 19 '19 at 16:25
2 Answers
Unfortunately, I need to type in -std=c++17 every time I need to compile
This is why build scripts exist. There are many arguments that you want to pass to your compiler at some point:
- Source files (you may have multiple translation units =
.cpp
files) - Include directories
- Libraries to link
- Optimization level
- Warnings
- C++ standard
- Symbol defines
- ...and many more compiler flags...
In bigger projects you may also have multiple build targets (and thus compiler invocations) and you don't want to do all that by hand every time either.
As a simple solution, you could write a .bat
script that invokes the compiler with the right arguments for you. However, there are tools that do a way better job at this, such as make
(generally only found in the Linux world) or MSBuild
(part of Visual Studio). Then there are also tools that generate these scripts for you, such as CMake and several IDEs with their own project configuration files.

- 23,383
- 5
- 39
- 72
I just want to endure that it uses the latest one like C++14 or 17. Unfortunately, I need to type in -std=c++17 every time
-std=c++17
is exactly how you ensure that you're using the C++ version you want (in this case C++17).

- 27,315
- 3
- 37
- 54