How to use C11 standard in Code::Blocks

匆匆过客 提交于 2019-12-17 21:59:37

问题


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 ?


回答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.




回答2:


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).



回答3:


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)

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)




回答4:


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




回答5:


I also faced the same problem in my CodeBlock Project. I am using version CodeBlock Version:17.12.

I fixed with following steps:

  1. Go to Setting -> Copmpiler Tab.
  2. "Global Compiler Settings:" pop is openend.
  3. Check Selected Compiler : " GNU GCC Compiler"
  4. Select "Compiler Flag" Tab.
  5. Select following check boxes:
    • Have g++ follow the C++11 ISO C++ language standard
    • Have gcc follow the 2011 ISO C language standard
  6. Save the settings.
  7. Now i am able to Complie C++11 code.

Hope it works for you guys as well.




回答6:


Go to Settings -> Compiler settings

Make sure global compiler settings on the left tab is selected.

On the right tab, click on Compiler settings tab. Under general, check the box against "Have gcc follow the 1999 ISO C language standard [-std=c99]




回答7:


The reason is because you are declaring i in for loop. Try separating it like below:

#include<stdio.h>

    int main(void)

        {
            int arr[] = {0,1,2,3,4};
            int i;
            for(i=0;i<5;i++){
                printf("%d ",arr[i]);
            }

            return 0;
        }

It will work. Happy coding:)




回答8:


go to "setting" -> "compiler", then see the option follow "c++11 ISO inc compiler" setting tab.



来源:https://stackoverflow.com/questions/34163283/how-to-use-c11-standard-in-codeblocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!