Search

0.10 — Configuring your compiler: Compiler extensions

The C++ standard defines rules about how programs should behave in specific circumstances. And in most cases, compilers will follow these rules. However, many compilers implement their own changes to the language, often to enhance compatibility with other versions of the language (e.g. C99), or for historical reasons. These compiler-specific behaviors are called compiler extensions.

Writing a program that makes use of a compiler extension allows you to write programs that are incompatible with the C++ standard. Programs using non-standard extensions generally will not compile on other compilers (that don’t support those same extensions), or if they do, they may not run correctly.

Frustratingly, compiler extensions are often enabled by default. This is particularly damaging for new learners, who may think some behavior that works is part of official C++ standard, when in fact their compiler is simply over-permissive.

Because compiler extensions are never necessary, and cause your programs to be non-compliant with C++ standards, we recommend turning compiler extensions off.

Best practice

Disable compiler extensions to ensure your programs (and coding practices) remain compliant with C++ standards and will work on any system.

Disabling compiler extensions

For Visual Studio users

To disable compiler extensions, right click on your project name in the Solution Explorer window, then choose Properties:

Solution Explorer Properties

From the Project dialog, first make sure the Configuration field is set to All Configurations.

Then, click C/C++ > Language tab, and set Disable Language Extensions to Yes (/Za).

Disable Language Extensions

For Code::Blocks users

Disable compiler extensions via Settings menu > Compiler > Compiler flags tab, then find and check the -pedantic-errors option.

Disable Language Extensions

For GCC/G++ users

You can disable compiler extensions by adding the -pedantic-errors flag to the compile command line.


0.11 -- Configuring your compiler: Warning and error levels
Index
0.9 -- Configuring your compiler: Build configurations

24 comments to 0.10 — Configuring your compiler: Compiler extensions

  • sasaki

    i got most of it. i mean i hope i did. but i still don't get how to write all those coding in each line.

  • Elijah

    almost none of the options in HelloWorld project properties shown in your screenshots appear in mine. am I dong something wrong? did I not install the right package?

    all that pops up is
    Common properties
         Startup Project
         Project Dependencies
         Code Analysis Settings
         Debug Source Files
    Configuration Properties
         Configuration

    also, I can't seem to set the platform to active(win32)

    thank you if anyone gets around to helping me out

    best regards

    • Alex

      Were you able to create your project as a win32/windows console project in the first place?

      Also make sure you're right clicking on the project line in the solution explorer (in the screenshot, the one that says "ConsoleApplication9"), not the solution line (in the screenshot, the one that says "Solution ConsoleApplication9 (1 project)")

  • Cas

    For mac users we were told to get xcode as our compiler but now there is no instruction for it. Please help. I came to learn C++ but now can't get through Chapter 0.

  • Quackky

    I just finished disabling language extensions for the HelloWorld project. Do I have to do it again every time for future projects?

  • Got the extensions disabled (Codeblocks) but there was another option checked already which was:
    Have g++ follow the C++ I4ISOC++ language standard[std=C++I4]
    Should that be left checked?
    Sorry if is a dumb question but I know nothing about working with a compiler and only have used perl and php and want to have as much right as possible.

  • Leo

    How can i disable the complier extensions on Xcode? I have searched sound a bit and can't find anything on it. Is there something better I could be using on a mac? I could even install windows on another portion or something it it would be easier to use windows. What do you think?

  • Qluefqen

    Hi, Alex,

    In keeping with your advice to experiment, I've tried using this command-line compiler line:

    That works fine with g++, but when I try any variant of it with gcc, I get:

    Also, I found that this works best for me and would ask for your comments:

    I just find it easier to read and my compiler seems to have no problem with me not putting the semicolon at the end of every line. Am I screwing up my learning in any way?

  • Hiruna

    Alex when I build solution after I give Yes(/Za) to disable language extentions in visual studio 2017 , It show an error, in it they say that (/Za) is an invalid value to disable language extentions

  • bibek

    i really could not understand how to disable compiler extensions in G++
    CAN U PLEASE CLARIFY IT

  • Khang

    Hi Alex,

    If I disabled the compiler extension in Visual Studio 2015, when I use a header guard and include it inside my main file:
    Header file (header.h):

    Main file (main.cpp):

    The compiler will throw an error telling me that #endif is illegal, but if I delete it, it will tell me that #endif is missing. Can you please tell me what is the problem here? Thank you.

    • Alex

      I've having no issues compiling your code with compiler extensions disabled (substituting in a template class for // some code here)
      If you remove all of the "// some code here", does it work?
      Does it work if you disable compiler extensions again?

      • Khang

        I think I have found the problem, it seems like when I disable the compiler extension, I must press enter after the "#endif" preprocessor to make a newline.

        If I enter a newline, the program will compile, and vice versa.

        On the other hand, my "main.cpp" file doesn't need any empty line on Visual Studio 2015 with the compiler extension off, but when I tried it on C-free 5.0, it will cause an error and tell me it need a newline at the end. (The raw options for C-free compiler is "-g -DDEBUG -pedantic-errors")

        This have confused me a lot, if the compiler extension is unnecessary and potentially dangerous, why did they include it in the compiler in the first place?

        • Hi Khang!

          TJ Seabrooks and Rakete1111 posted a nice explanation on stackoverflow ( https://stackoverflow.com/a/72409/9364954 ).
          Omitting the line feed at the end of a file could cause problems prior to C++11. Every C++11 and newer compiler should automatically add the line feed and not complain.

  • Hi Alex!

    To my understanding -pedantic only disables extensions that prevent standard-conform programs from compiling and issues warnings when other extensions are used, but allows compilation. -pedantic-errors disables all extensions.

Leave a Comment

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