Search

0.12 — Configuring your compiler: Choosing a language standard

With many different versions of C++ available (C++98, C++03, C++11, C++14, C++17, etc…) how does your compiler know which one to use? Generally, a compiler will pick a standard to default to (often not the most recent language standard). If you wish to use a different standard, you’ll have to configure your IDE/compiler to do so. These settings are applied to the active project only. You need to set them again when you create a new project.

Learncpp uses C++17. If your compiler isn’t capable of C++17, you won’t be able to follow all lessons, though the majority is unaffected. Make sure you’re using an up-to-date compiler so you can compile all the examples yourself. There’s an example at the end of this lesson which you can use to test if you set up your compiler correctly.

Code names for in-progress language standards

Note that the language standards are named after the years in which they are finalized (e.g. C++17 was finalized in 2017).

However, when a new language standard is being agreed upon, it’s not clear in what year the finalization will take place. Consequently, in-progress language standards are given code names, which are then replaced by the actual names upon finalization of the standard. For example, C++11 was called c++1x while it was being worked on. You may still see the code names used in places (especially for upcoming version of the language standard, which won’t have a final name yet).

Here’s a mapping of code names to the final names:

  • c++1x = C++11
  • c++1y = C++14
  • c++1z = C++17
  • c++2a = C++20

For example, if you see c++1z, this is synonymous with the C++17 language standard.

Setting a language standard in Visual Studio

As of the time of writing, Visual Studio 2019 defaults to C++14 capabilities, which does not allow for the use of newer features introduced in C++17 (and C++20), some of which are covered in future lessons.

To use these newer features, you’ll need to enable a newer language standard. Unfortunately, there is currently no way to do this globally -- you must do so on a project-by-project basis.

To do so, open your project, then go to Project menu > (Your application’s Name) Properties, then open Configuration Properties > C/C++ > Language.

From there, you can set the C++ Language Standard to the version of C++ you wish to use.

As of the time of writing, we recommend selecting “ISO C++17 Standard (/std:c++17)”, which is the latest stable standard.

If you want to experiment with capabilities from the upcoming C++20 language standard, choose “ISO C++ Latest (/std:c++latest)” instead. Just note that support may be spotty or buggy.

Setting a language standard in Code::Blocks

Code::Blocks may default to a pre-C++11 language standard. You’ll definitely want to check and ensure a more modern language standard is enabled.

The good news is that Code::Blocks allows setting your language standard globally, so you can set it once (rather than per-project). To do so, go to Settings menu > Compiler:

Code::Blocks Settings > Compiler

Then find the box or boxes labeled Have g++ follow the C++XX ISO C++ language standard [-std=c++XX], where XX is 11, 14, or some other higher number (see the items inside the red box below for examples):

Code::Blocks C++11 setting

Check the one with the highest number (in the above image, that’s the C++14 option inside the red box).

Your version of Code::Blocks may also have support for upcoming (or just released) versions of C++. If so, these will be labeled Have g++ follow the coming C++XX (aka C++YY) ISO C++ language standard [-std=c++XX] (see the blue box above). You can optionally check these if you would like to enable features added in that version, but note that support may be incomplete (ie. some features may be missing).

See the list at the top of the lesson for a list of codenames to language standards mappings.

For example, if you want to enable C++17 capabilities and your settings doesn’t have a C++17 option, look for -std=c++1z (the code name for C++17).

Alternatively, you can go to the Other Compiler Options tab and type in -std=c++17.

Code::Blocks Other Compiler Options

Note

This will work if your compiler has C++17 support. If you’re using an older version of Code::Blocks and C++17 features don’t seem to work, upgrade your compiler.

Setting a language standard in g++

For GCC/G++ users

For GCC/G++, you can pass compiler flags -std=c++11, -std=c++14, -std=c++17, or -std=c++2a to enable C++11/14/17/2a support respectively.

Testing your compiler

After you enabled C++17 or higher, you should be able to compile the following code without any warnings or errors.

If you can’t compile this code, you either didn’t enable C++17, or your compiler doesn’t fully support C++17. In the latter case, please install the latest version of your IDE/compiler, as described in lesson 0.6 -- Installing an Integrated Development Environment (IDE).


1.1 -- Statements and the structure of a program
Index
0.11 -- Configuring your compiler: Warning and error levels

95 comments to 0.12 — Configuring your compiler: Choosing a language standard

  • Oliver Wayne

    Hello,

    when I attempt to compile using C++17, I get the following errors

    Not sure why this is -- this is off the command line, but i see the same error in xcode. Could anyone assist me?

    • Oliver Wayne

      As an update: I got this to work, but I had to take the following steps. First, I explicitly included the size and type in the array initialization.

      Then, I linked and compiled in two separate steps and explicitly included the standard library

      Could someone explain why each of those steps might be necessary, and how I could do the linking and compilng in one line?

      • nascardriver

        This is all you should need, with no extra linking steps. Note that gcc is a C compiler, g++ is for C++, this might be why you have to link stdc++ manually. "gcc" is often used to refer to gcc and g++.

        The type and length can be omitted since C++17 and gcc version 7. Your gcc might be too old.

        • Oliver Wayne

          Yes, that fixed it! I swapped to g++, then updated my g++ to the newest version and aliased g++ to the new version (since I guess Macs point to clang by default). Thanks a bunch!

  • Wyatt W

    For those using Xcode:

    1. In the upper left hand corner of the window, you will see a blue file named after what you called your project. Click it.

    2. In the list of setting, select "Build Settings", below it click "All" and "Combined".

    3. Scroll down until you see "Apple Clang - Language - C++"

    4. In that, you will see something named "C++ Language Dialect". Click the C++ type next to it.

    5. A list of Languages will pop up. Click "C++17 [-std=c++17]".

    This should work.

  • Michael Davey

    FYI, for future folk, I had to uninstall an old Visual Studio Express installation that appeared to be corrupt and VS 2019 and reinstall 2019 to get the code to compile without errors or warnings.

    I wound up having to use InstallCleanup.exe since VS Express uninstall, repair and update all failed with an error.  The procedure is explained in Microsoft's "Remove Visual Studio" VS Studio docs.
    I opted for the more complete uninstall -f switch.
    The re-installation went well with no problems.
    My projects/solutions were still there and recognized although I had backed them up just in case.

  • Louis Cloete

    Hi Alex/Nascardriver!

    Can you add a chapter about the GSL libraries after this one? I think any new C++ programmer should learn about them to make code safer and easier to write in idiomatic ways. Microsoft's implementation is open source and available for all three major desktop OSs: for MSVC in Windows and for LLVM in all of them. Github page is here: https://github.com/microsoft/GSL

    • nascardriver

      I agree with recommending reading the core guidelines. Using the GSL is probably also very good.
      When there is a lesson about something, we have to be able to answer questions. I haven't read the entire core guidelines and I barely used the GSL, needless to say, I can't answer questions about them.
      The day I feel confident enough about the GSL, I'll add a lesson.

    • Alex

      I added a note about GSL and the C++ Core Guidelines to the last chapter as topics for future exploration. Maybe in the future we'll add lessons directly with GSL.

  • Mithlesh Singh

    Ever since I created Hello World I'm unable to run the .exe file

    I'm using Code Blocks 32 bit version and have installed MinGW 32 bit version from its official website. I've re-installed both many times to avoid the issue but to no avail.

    I created the project and built it. No errors popped up in Code Blocks.

    I tried to run the "HelloWorld.exe" & got an error "libgcc_s_dw2-1.dll" is missing. I googled for the issue and copied the mentioned .dll file in the same folder containing "HelloWorld.exe" file.

    I tried to run again and got an error "libstdc++-6.dll" is missing. I repeated the above procedure and copied the .dll file in the same folder containing "HelloWorld.exe" file.

    I tried running it again but now the console window flashes and disappears immediately. I added the code mentioned in the chapter "0.8 — A few common C++ problems" for this issue and tried running again & it worked.

    Now, as per lesson "0.12 — Configuring your compiler: Choosing a language standard" I created another program to test the capabilities and function enabled in the compiler through previous chapters & did it without any error in Code Blocks but again while running the "Hello.exe" file both the previous .dll file errors popped up & I repeated the same and again the program just flashes and disappears immediately.

    Can someone help with a permanent fix? This time the code is too long & I'm not sure where will I add the code from chapter "0.8 — A few common C++ problems" for this issue. I don't have any knowledge of coding. Just wanted to try my luck to see If I could learn some C, C++ online with help of tutorials and practice.

    Thanks in advance!!!

    • Louis Cloete

      I don't know what you should do about the .dll errors. Maybe try Visual Studio, since you're on Windows?

      Else, you can try to go to Settings -> Compiler... and check under each tab what you see. I don't know what you should see, but I may be able to help if you can tell me what you see. It sounds like you're having something set up incorrectly. Your toolchain isn't finding the .dlls. This is probably because you need to tell it where to find it somewhere, and you didn't. It's not necessarily something you did wrong, it can sometimes happen in an installation.

      You should add the code from 0.8 at the end of main(), right before return 0;.

  • Jim Guth

    I've ran the test code at the bottom of this less by just deleting all the code in main.cpp and then pasting the file in, and it ran fine with CodeBlocks compiler GNU_GCC 64bit with compiler and compiler flag c++17 plus GNU extensions checked. I also added -std=c++2a below the -Wsign-conversion -Werror, warning from an earlier lesson.  

    However the Build log is showing in red: Process Terminated with status -1073741510 on the end with the time it took to run.

    Is it normal to have the build log shown RED at the end?

    • nascardriver

      I don't know where that message is coming from. If you can compile and run the code (output is
      hello 3 6
      3
      ), I'd say you can ignore the message.

  • Edmund Graves

    I am using Visual Studio 2017, and after making all the changes in the settings that you said to change in the "Configuring your compiler" lessons (0.9 - 0.12) including changing the C++ Language Standard to "ISO C++17 Standard (/std:c++17)", I ran the code at the end of this lesson, but it gave an error saying:

    Error    C1010    unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?

    I don't know if it is just something missing in the lesson or something else, but sure enough, when I added '#include "pch.h"' to the very beginning of the .cpp file, it ran just fine. Is this my IDE being out of date, or just a missing line of code?

    • Edmund Graves

      Nevermind. I hadn't set up the project according to how you instructed to do it in Lesson 0.7. I just set a project up in that way and it's working. I think I had set up a Windows Console Application. Sorry for the unnecessary comment.

      Thanks for making these lessons available!

  • Hello Newbie here, here's my message:

    [1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
    1>HelloWorld.cpp
    1>MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
    1>C:\Users\Larry\source\repos\HelloWorld\Debug\HelloWorld.exe : fatal error LNK1120: 1 unresolved externals]

    This is with the ISO C++17 Standard Language enabled.
    Is it still a compiler issue? I loaded the Visual Studio Community following the instructions. Thank you.

  • Nick

    "ISO C++17 Standard (/std:c++17)" does not work for some reason. Is there any way to fix it?

  • ChickenShack

    Hello, I am currently doing every tutorial both with Visual Studio and Code Blocks. For both I have chosen C++17. Visual Studio worked fine but Code Blocks did not. When compiling I get the following error.

    g++.exe: error: unrecognized command line option '-Wsign-conversion-Werror'; did you mean '-Wsign-conversion'?

    I think it has something to do with the 'other compiler options' -Wsign-conversion-Werror but I dont actually know anything.

  • mode

    I have visual studio express 2017 and it give me 27 error and whan i try to change the standard there are no choices so i write /std:c++17 by myself and it give me 14 error  for example :
    E0283    qualified name is not allowed    HelloWorld    
    E0325    inline specifier allowed on function declarations only    
    E0135    namespace "std" has no member "string_view"    
    E0040    expected an identifier    HelloWorld        
    E0020    identifier "iNumbers" is undefined    
    E0017    expected a ']'    
    E1593    cannot deduce 'auto' type (initializer required)        

    it's distinctive error
    Element <LanguageStandard> has an invalid value of "/std:c++17".

    • yash

      exactly same thing is happening to me

    • nascardriver

      I can't find information about VS 17 express. The errors look like it doesn't support C++17. Use a newer visual studio.

      • mode

        what is a newer visual studio that need same Properties
        i mean there are differences between mine and newer because my lab top has 4 Gb Ram

        • nascardriver

          Visual Studio 2019 Community needs 2GB RAM minimum (https://docs.microsoft.com/en-us/visualstudio/releases/2019/system-requirements), which is the same for VS2017, you can give that a try. Otherwise you'll have to use an IDE that isn't as memory hungry as VS, or even use an editor and compile manually.

  • sakib

    i can not run this in xcode

  • on #include <string_view> there is an error and ive updated code::blocks and it still won't work

    • nascardriver

      You have to update the compiler that code::blocks is using. See the big warning in lesson 0.6 https://www.learncpp.com/cpp-tutorial/installing-an-integrated-development-environment-ide/

  • Casey

    C++ newbie questions here. Is this supposed to happen in a new project? Should I just do this in the HelloWorld project? It does not seem to work in the HelloWorld project so I will try making it its own project but that is unclear.

    • abdelnour bentaieb

      no it should be fine to delete and paste the test program,as ffor why it s not working i found out that changing  disable language extensions to no under configaration properties->c/c++ -> language fixed the problem for me

      • nascardriver

        Don't enable language extensions. Enabling language extensions allows you to write non-standard code that doesn't work with other compilers. If the example code doesn't compile without language extensions, it means that your compiler doesn't support C++17.

  • Lifthra

    When you choosed the c++ language you did not use (all configuration), like you did for the errors, but Active(debug).
    What is the difference?

    • Alex

      My active configuration was the debug configuration, so this change would only impact my debug configuration. All configurations would make the change apply to both debug and release.

      You'd probably want to use All Configurations when changing your language standard since it wouldn't make sense to have debug compiled under one standard and release under another.

      I've updated the image to show All Configurations.

  • Daniel Jackson

    I am trying to run this program, after reinstalling the update for the compiler, and continue to get this message:

    ||=== Build: Debug in Learn C++ (compiler: GNU GCC Compiler) ===|
    D:\Programming Projects\Learn C++\Test 2.c|1|fatal error: iostream: No such file or directory|
    D:\Programming Projects\Learn C++\Testing Compiler.c|1|fatal error: array: No such file or directory|
    ||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

    • Daniel Jackson

      Never mind; found a transcription error; file did not have the correct extension. C++17 operational. Above program ran after I listened to the Debug Department.

  • prince

    Can you tell me what should be the result of the compiled program that you gave above

    • nascardriver

      The output doesn't matter. If you can compile the code, everything is working. Anyway

      • George Pitchurov

        It doesn't compile. Complains about #include <string_view>. I tried selecting future standard C++17, I tried putting a compiler option -std=C++17 and so on. I also upgraded gcc. I use codeblocks in linux Mint.

        • nascardriver

          You need at least gcc 7. You can check which version you have installed by running

          • Ben

            I'm having similar problems - compiler error messages:

            src/main.cpp:42:16: error: no viable constructor or deduction guide for deduction of template arguments of 'array'
                std::array arr{ 1, 2, 3 };
                           ^
            /Library/Developer/CommandLineTools/usr/include/c++/v1/__tuple:223:64: note: candidate function template not viable: requires 0 arguments, but 3 were provided
            template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                                           ^
            /Library/Developer/CommandLineTools/usr/include/c++/v1/__tuple:223:64: note: candidate function template not viable: requires 1 argument, but 3 were provided
            1 error generated.

            This is with Clang 10.0 on OSX - here is the output of clang --version:

            Apple LLVM version 10.0.0 (clang-1000.10.44.4)
            Target: x86_64-apple-darwin17.7.0
            Thread model: posix
            InstalledDir: /Library/Developer/CommandLineTools/usr/bin

  • Aditya

    I am using Code::Blocks, the 17.12 version and I got this error even after doing everything that is part of this tutorial,

    |3|fatal error: string_view: No such file or directory|

    Could it have anything to do with "..features added in that version, but note that support may be incomplete (e.g. some features may be missing)."
    What do I do??

    And also,
    Is it okay if I have zero knowledge of C++ before beginning these tutorials?

    • nascardriver

      The compiler you're using with code::blocks probably doesn't support C++17. There are currently no instructions for manually adding a compiler to code::blocks here.
      Googling for "code blocks C++17 windows" (Replace windows with your OS) should get you good instruction on how to add another compiler.

      Don't quit if you can't get it to work, you can still follow the majority of the tutorials with an old compiler.

      The tutorials are targeted at beginners, you don't need any prior programming knowledge.

  • Vitaliy Sh.

    Typo: two "[/note]"s is being visible near the end of the lesson.

  • Vitaliy Sh.

    Maybe your rather change "alt" to better fit the lesson's text: like a "CB: choose the stable/in-progress standard to follow" or smth?

  • Vitaliy Sh.

    Hi Sires!

    Empty "alt" attribute here.

  • Vitaliy Sh.

    ...
    (since it isn’t clear what year the standards will be finalized in until it actually happens).
    ...

    Maybe a typos (hard sentence to me):
    1) Maybe use "which" instead of "what"?
    2) No comma after "finalized in".

    Is my proposal from https://www.learncpp.com/cpp-tutorial/configuring-your-compiler-choosing-a-language-standard/comment-page-1/#comment-437277 bad?

    • Alex

      1) Generally "which" is correct when we're selecting from a limited choice, and "what" is used otherwise. What seems like the better choice here, since we aren't given a discrete set of options.
      2) Fixed. Thanks!

      Your other proposal isn't bad, it just didn't seem to be to be better than what was already written.

      • Vitaliy Sh.

        ``No comma after "finalized in"'' isn't fixed, somehow.

        • Alex

          As far as I can tell, it shouldn't have one, per https://www.roanestate.edu/owl/Commas.html

          • Vitaliy Sh.

            ...
            Note that language standards use code names prior to the standards being finalized (since it isn’t clear what year the standards will be finalized in until it actually happens).
            ...

            Note that language standards use code names (instead of years) prior to the standards being finalized (since the year of finalization isn't clear until it actually happens).

            ?
            PS: (If someone can't connect to www.roanestate.edu too: look up the https://www.lexico.com/en/grammar/punctuation or elsewhere).

  • Vitaliy Sh.

    ...
    to enable C++11/14/17 support respectively.
    ...

    Maybe add, just in case:
    The last "-std=" wins.

    OK:
    g++ -std=c++03 -std=c++11 test.cpp
    Err:
    g++ -std=c++11 -std=c++03 test.cpp

  • Vitaliy Sh.

    ...
    You can optionally check these if you would like to enable features in that version,
    ...

    Typo: either "in" instead of "from", or no "added" after "features".

  • Vitaliy Sh.

    ...
    C++ language standard [-std=c++XX]:, where XX is
    ...

    Possible typo: ":" before comma.

  • Vitaliy Sh.

    ...
        c++1x = C++11
        c++1y = C++14
        c++1z = C++17
        c++2a = C++20
    ...

    Possibly Typo: Capital "C" at right side from "=". Both g++ and clang++ are not accepted C++XX as a -std=.

  • Vitaliy Sh.

    ...
    Note that language standards use code names prior to the standards being finalized (since it isn’t clear what year the standards will be finalized until it actually happens).
    ...

    If apropriate, to:

    Note that prior to their finalization, the language standards are get referred by using the code names (since it isn't clear what year the standard will be finalized until it actually happens).

  • Vitaliy Sh.

    ...
    Generally, a compiler will pick a standard to default to (often not the most recently language standard).
    ...

    Typo: either no "published" between "recently" and "language", or "recently" like to be "recent".

    Maybe also change to:
    Generally, the compiler will pick a default one (which is often ...

  • Aymen Chehaider

    Nice tutorial Alex. Could you post some guidelines / steps on how to use the last compiler using MSYS2. Iknow that it may be out of scope or can find some clues by googling but will appreciate any stuff on how to configure MSYS2.

    Thank you in advance

Leave a Comment

Put all code inside code tags: [code]your code here[/code]