How to enable c11 on later versions of gcc? - c

I currently use gcc 4.6.3. My understanding is that gcc by default uses the gnu89 standard and I would like to enable C11, the latest C standard. I tried:
[pauldb#pauldb-laptop test ]$ gcc -std=c11 -o test test.c
cc1: error: unrecognised command line option ‘-std=c11’
I replaced c11 with gnu11 and I get the same error. What is the correct way to enable the latest C standard for gcc?
(Note: I'm interested in the latest C standard and not the latest C++ one.)

The correct option is -std=c11.
However, it is not available in gcc 4.6. You need at least gcc 4.7 to have this option supported. In some older versions like gcc 4.6, the option -std=c1x was available with experimental (i.e., very limited) support of C11.
Note that the current version of gcc is gcc 8.2.

Just to let you know GCC 4.9.x has far more complete support than older versions. If you really need to use this feature, please switch to anything 4.8+
Here is the support status -- https://gcc.gnu.org/wiki/C11Status

gcc 5.2.0 works with command line option ‘-std=c11’

Inside a .spec file :
%define gcc_ver %(if [[ $(gcc -dumpversion) > 4.7 ]]; then echo 1; else echo 0; fi)
# Do we use c11 ?
%if 0%{?gcc_ver} < 1
%global std_c11 0
%else
%global std_c11 1
%endif
# if the configure of the package supports it add :
%if %{std_c11}
--enable-cxx11 \
%endif

Related

Is there a command line involving gcc to know which --std=xxx it is using?

I can choose to call
gcc --std=c99 toto.c -o toto.elf
But in my case, I would like to know which --std is used by default when calling
gcc toto.c -o toto.elf
Note for closing request:
I refute the idea that this topic is a duplicate, in fact, what I want is not only knowing what the default --std is but also what --std is currently used and how make difference between std=gnu11 and std=c11.
Sure that my first post was leading people in error.
i found this gcc -dM -E - < /dev/null | grep 'STDC_VERSION'
Everything prior version 5.0.0 has -std=gnu90 as default.
Everything between version 5.0.0 and 8.0.0 has -std=gnu11 as default.
Everything past 8.0.0 has -std=gnu17 as default.
So you only need to check --version. However, the __STDC_VERSION__ should also correspond to the -std=cxx even when compiling with GNU extensions.

what does "-std=standard" in gcc info page mean?

I want to know what C language standard is my current installation of gcc following by default. I ran info gcc and it gave -std=standard.
What does -std=standard mean?
I was expecting something like -std=gnu11
OS: linux mint 19.1
gcc version: 7.4.0,
gcc came pre-installed with the OS
I looked into online manual here:
https://gcc.gnu.org/onlinedocs/gcc-7.4.0/gcc/Standards.html#C-Language
but I didn't find any default standard named "standard". Instead I found that it should be "gnu11"
info gcc response:
...
OPTIONS
Option Summary
Here is a summary of all the options, grouped by type. Explanations
are in the following sections.
Overall Options
-c -S -E -o file -x language -v -### --help[=class[,...]]
--target-help --version -pass-exit-codes -pipe -specs=file
-wrapper #file -fplugin=file -fplugin-arg-name=arg
-fdump-ada-spec[-slim] -fada-spec-parent=unit -fdump-go-spec=file
C Language Options
-ansi -std=standard -fgnu89-inline
-fpermitted-flt-eval-methods=standard -aux-info filename
-fallow-parameterless-variadic-functions -fno-asm
...
The word "standard" is in a cursive font. It's a template, it's not a "default gcc standard gcc uses by default", it's a template to be filled by the user with available option, the word "standard" stands there for gnu11 or c11 or for the standard you want to specify.
The same is with -fpermitted-flt-eval-methods=standard or -fno-builtin-function. You should specify the function that gcc should not use builtin for. This is a list of available options for C language in gcc, not list of default options gcc uses.
You can find the standard gcc is using by inspecting __STDC_VERSION__ macro. Or check the documentation. Quoting the link, for gcc 7.2.0:
The default, if no C language dialect options are given, is -std=gnu11.
In the context of -std=standard, standard is meant to represent one of the values listed in the Options Controlling C Dialect. It isn't meant to literally be the string standard; that results in an error:
gcc: error: unrecognized command line option ‘-std=standard’

What is the version of C used in gcc

What is the version of C used in GCC? C99, C11 or C90? I thought was the GCC use C99 but I was mistaken:
for(int i = 0; i < 100; i++){
...
}
error: ‘for’ loop initial declarations are only allowed in C99 mode.
According to 2 Language Standards Supported by GCC:
The default, if no C language dialect options are given, is -std=gnu90; this is intended to change to -std=gnu11 in some future release.
I believe the default is -std=gnu90. You could specify -std=c99 in your compiler flag to support this.
By default gcc uses C90 with GNU extension, this is all covered in the gcc docs Language Standards Supported by GCC, the flag for this would be -std=gnu90:
The default, if no C language dialect options are given, is -std=gnu90; this is intended to change to -std=gnu11 in some future release.
If you want C99 support than you should use -std=c99, although this does not mean gcc will not use extensions, so if you want to receive a warning when gcc using an extension you need to add -pedantic and -pedantic-errors to turn that into an error:
to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings)
Use -std=99
Before C99, you had to define the local variables at the start of a block. C99 imported the C++ feature that you can intermix local variable definitions with the instructions and you can define variables in the for and while control expressions.

Why gcc gives warning: implicit declaration of function qsort_r?

I do include<stdlib.h> where qsort_r is given. And I use gcc -std=c99 -O3 myfun.c -o myfun to compile.
It compiles, links and runs well. I don't know why I got this warning and what is potential risk of this warning ?
BTW, my compiler is gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-1ubuntu1)
It does so because you use -std=c99 , there's no qsort_r function in stdlib.h in c99.
Use -std=gnu99 to make the extensions available, or add a #define _GNU_SOURCE to your source files before including the header files.
qsort_r is not supported by C99. Specification says nothing about it.
Language Standards Supported by GCC:
By default, GCC provides some extensions to the C language that on rare occasions conflict with the C standard1. See Extensions to the C Language Family. Use of the -std options listed above will disable these extensions where they conflict with the C standard version selected. You may also select an extended version of the C language explicitly with -std=gnu89 (for C89 with GNU extensions) or -std=gnu99 (for C99 with GNU extensions). The default, if no C language dialect options are given, is -std=gnu89; this will change to -std=gnu99 in some future release when the C99 support is complete. Some features that are part of the C99 standard are accepted as extensions in C89 mode.
1. Emphasis is mine

Compile with Clang and use GETTEXT

I have a automake enabled project which I would like to compile with clang. I have added a configure option to enable clang:
AC_ARG_ENABLE([clang],
[AS_HELP_STRING([--enable-clang],[use clang instead of gcc as C compiler.])])
#Use C99 compilation mode
if test "x$enable_clang" = "xyes"; then
# clang uses c99 mode by default, so we just set CC to clang and we are done
CC="clang";
else
# We do not need to set CC as the default is gcc, but we need to set it to
# use C99 compilation mode
CFLAGS="$CFLAGS -std=c99";
fi
Further up in the file I have also the following two macros, to enable gettext functionality:
AM_GNU_GETTEXT_VERSION([0.18.1])
AM_GNU_GETTEXT([external])
If these two macros are present then configure ignores that the CC variable is set to clang and falls back to gcc. I have to comment out the gettext macros and then clang is used.
Obviously there is some problem with GETTEXT and clang. Am I using the wrong macro, or is clang not able to use the gnu gettext library? How can I fix this?
Have you tried to set CC to clang before you check for libraries (like gettext)? – Some programmer dude
Nice that worked. – lanoxx

Resources