How to use C11 standard in Code::Blocks

14,458

Solution 1

Since the GCC 5.x versions run with -std=gnu11 by default, Code::Blocks must be doing something (such as passing -ansi or -std=gnu90) to the compiler to make it work differently.

Investigate all the options that are sent to the compiler. Find a way to have Code::Blocks show you the exact incantation it uses when compiling. Then work out how to fix it.

Options that are used are:

-Wall -Wextra -Werror -Wstrict-prototypes -Wconversion -std=gnu11 \
-O0 -g -ansi `pkg-config --cflags gtk+-3.0`

The -ansi is doing the damage; it is equivalent to -std=c90 or perhaps -std=gnu90 — it explicitly undoes -std=c11 or -std=gnu11.

Solution 2

Am just a learner (beginner - very new) but I hope this might help (though the thread is old).

(code visible in the image is example 12.2 -- forc99.c from Stephen Prata C Primer Plus)

code_blocks_16.01

I created new flag

Settings -> Compiler -> (under general)Right click on general -> New Flag -> Enter details from image -> Ok After that check the box of the new flag just created. (worked for me)

[If you look in the image there is option for -std=c99 (just above the one I created), You can use that option for c99 support.]

(By the way the gcc version for code::blocks 16.01 mingw 32bit, I use this, is 4.9.2)

Solution 3

Assuming Codeblocks 13.12 for Windows, it comes with an older version of GCC (4.7.1) that doesn't support C11.

  • Manually download the latest version of the Mingw 64 compiler (I don't think Mingw32 is maintained to include GCC versions of C11).
  • Install it. It will end up in some obscure folder like C:\Program Files\mingw-w64\x86_64-4.9.1-win32-seh-rt_v3-rev1\mingw64.
  • Add the above path in Codeblocks, Settings -> Compiler -> Toolchain executables tab -> Compiler's installation directory. Click the "auto detect" button.
  • In the same tab, check that the C compiler is x86_64-w64-mingw32-gcc.exe (since you now might have multiple installations of GCC on your computer) and that the make program is mingw32-make.exe.
  • In the tab Compiler settings, right click on the list of compiler flags and select "New flag". For "name" type in C11, for compiler flags type in -std=c11. Click ok and check the new C11 option you just created.
  • Also to ensure C11 conformance, check the option "treat errors as the warnings demanded by ISO C..." (-pedantic-errors). Check the option "Enable all common compiler warnings" (-Wall).

Solution 4

Go to Settings-> compiler-> Compiler Flags-> General-> tick the box next to " Have g++ follow the c++11 ISO" DONE!!!

Share:
14,458
Michi
Author by

Michi

I just can not stop myself from programming :))

Updated on June 16, 2022

Comments

  • Michi
    Michi almost 2 years

    Like the Title says I need to make code::blocks to work with C11 and I can't figure out how to do it.

    I went to settings => compiler settings => Other options and I added -std=c11 and tried also with -std=gnu11, both doesn't seems to work.

    I compiled gcc-5.2 and then I changed the default compiler (gcc-4.9) and still no result.


    When I try to compile the following program:

    #include<stdio.h>
    
    int main(void){
        int arr[] = {0,1,2,3,4};
    
        for(int i=0;i<5;i++){
            printf("%d ",arr[i]);
        }
    
        return 0;
    }
    

    I get the following:

    |6|error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode|
    

    But if I do it in terminal (ubuntu 15.04, 64BIT, gcc-5.2):

    ./install/gcc-5.2.0/bin/gcc5.2 program.c -o program
    

    Seems to work fine.

    My question is, how to make code::blocks to work with c11 ?