2

I want to know what the default c++ language standard version that is used by my compiler is when I do something like g++ main.cpp.

I know I can run g++ -v for the compiler version, which for example returns:

Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

But I'm not sure what the default C++ language version used here is.

I do also know I can use -std=c++(version) to change it but, it would be nice to set the default to c++20 to stay up to date and not have to use flags.

starball
  • 20,030
  • 7
  • 43
  • 238
Macto24
  • 79
  • 6

2 Answers2

8

Note that on MacOS with the Xcode developer tools, the g++ and clang compiler toolsets are just aliases to Apple Clang, which is Apple's version of Clang.

If you want to find out experimentally, you can get the C++ language standard version being used at compile-time using the standard __cplusplus macro. Just compile a file that does a message pragma printing the value of __cplusplus without specifying a c++ language version flag in the compile command.

But you may as well read the docs.

Defaults for GCC

See its documentation, which at the time of this writing states:

C++17 features are available since GCC 5. This mode is the default in GCC 11; it can be explicitly selected with the -std=c++17 command-line flag, or -std=gnu++17 to enable GNU extensions as well.

GCC has full support for the of the 2014 C++ standard. This mode is the default in GCC 6.1 up until GCC 10 (including); it can be explicitly selected with the -std=c++14 command-line flag, or -std=gnu++14 to enable GNU extensions as well.

GCC has full support for the 1998 C++ standard as modified by the 2003 technical corrigendum and some later defect reports, excluding the export feature which was later removed from the language. This mode is the default in GCC versions prior to 6.1; it can be explicitly selected with the -std=c++98 command-line flag, or -std=gnu++98 to enable GNU extensions as well.

Also related: Which C++ standard is the default when compiling with g++?

Defaults for Clang

See its documentation, which at the time of this writing states:

By default, Clang builds C++ code according to the C++14 standard. You can use Clang in C++14 mode with the -std=c++14 option (use -std=c++1y in Clang 3.4 and earlier).

The default C++ language version sometimes changes with newer releases of Clang, such as it did in Clang 6.0 from gnu++98 to gnu++14

Defaults for MSVC

See its documentation for the /std flag, which at the time of this writing states:

/std:c++14
The /std:c++14 option enables C++14 standard-specific features implemented by the MSVC compiler. This option is the default for code compiled as C++. It's available starting in Visual Studio 2015 Update 3.

Changing the Defaults

The way to change the default depends on what other build tools you are using.

I'm not aware of a way to configure the compiler itself apart from doing something for/with the buildsystem or shell that you run the compiler with. You might need to actually build a modified version of the compiler if you actually want to go that deep.

Related question: Set as default C++11 in Clang.


That being said, for any code large enough to warrant using a buildsystem, since not all projects will have the same minimum language standard requirements, it's better to specify compiler-language-mode requirements for a project explicitly in the buildsystem's configuration files (for example, using target_compile_features in CMake) so that users invoking the buildsystem for the project won't have to remember to manually set the right language-standard flags and can just let the buildsystem do what's needed.

Remember that for some projects (especially widely used-libraries), having lower requirements for language standard to build them or use their headers is a feature, because it allows people who (for their own reasons) cannot or choose not to use newer compilers (and instead use older ones that don't support or don't fully support newer language features) to build them or include headers from them.

starball
  • 20,030
  • 7
  • 43
  • 238
1

There is no direct way to do this. g++/gcc do not have something like a config file where you can set the C++ standard.

You also would not want to do this for the following reason:

If you ship your code to someone else and expect them to be able to build it, part of the build process would not be "change the default C++ standard for your compiler in a config file somewhere". That wouldn't make any sense.

That is why you have to pass a command line argument, which could be included as part of a bash script to compile your code, or better as part of a proper build system like Make or CMake.

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • 1
    "_That is why you have to pass a command line argument_" if building a project really requires a specific or minimum language-standard mode of the compiler, then ideally the user invoking the build wouldn't need to specify such a requirement manually, and that would instead be baked into the buildsystem configuration files, such as by using [`target_compile_features`](https://cmake.org/cmake/help/latest/command/target_compile_features.html) in CMake. – starball Mar 17 '23 at 05:56
  • @user as per what is written in the question *which could be included as part of a bash script to compile your code, or better as part of a proper build system like Make or CMake* – FreelanceConsultant Mar 17 '23 at 09:02