Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable GNU C extensions?

Tags:

c

gcc

As you can see in the following code, I've introduced a nested function within main():

#include <stdio.h>

int main(){
 int a=5;
 printf("%d\n",a);
 {
  int a=10;
  printf("%d\n",a);
 }
 printf("%d\n",a);

 //Nested function
 int main(int a){
 if(a>0)printf("%d\n",a--);
 return 0;
 }

 main(7);
 return 0;
}

As far as I could understand I used the -std=c99 flag in gcc to "disable" the unnecessary extensions, but I did not get any error.

gcc temp3.c -std=c99 -o temp3.out

Where have I made the mistake?

like image 247
Mayank Verma Avatar asked Aug 14 '16 07:08

Mayank Verma


People also ask

What is GNU extension?

GNU provides many extensions to the C and C++ languages, and the ARM compiler supports many of these extensions. In GNU mode, all the GNU extensions to the relevant source language are available. Some GNU extensions are also available when you compile in a nonstrict mode. To compile in GNU mode, use --gnu .

Which option is used to turn off options features not compatible with C90?

For example, -std=c90 turns off certain features of GCC that are incompatible with ISO C90, such as the asm and typeof keywords, but not other GNU extensions that do not have a meaning in ISO C90, such as omitting the middle term of a ?: expression.

What is GNU C in Linux?

The GNU Compiler Collection, commonly known as GCC, is a set of compilers and development tools available for Linux, Windows, various BSDs, and a wide assortment of other operating systems. It includes support primarily for C and C++ and includes Objective-C, Ada, Go, Fortran, and D.

What are language extensions in C?

Most C compilers have one or more "extensions" to the standard C language, to do things that are inconvenient to do in standard, portable C. Some examples of language extensions: in-line assembly language. interrupt service routines.

How do I disable extensions that I don't want to use?

Here's how to disable extensions and plug-ins that you don't want to use with Chrome. The easiest way to access the extensions settings is through the menu. Select the three-dot menu (located on the upper-right corner of Chrome).

How do I disable extensions and plug-ins in chrome?

Here's how to disable extensions and plug-ins that you don't want to use with Chrome. The easiest way to access the extensions settings is through the menu. Select the three-dot menu (located on the upper-right corner of Chrome). Select More Tools, then choose Extensions.

How to manage Chrome extensions?

Next to the extension you want to manage, either uncheck the Enabled box to disable the Chrome extension or click the trash button to remove it. The icon for disabled extensions that are still installed turns black and white, and they can be re-enabled in the future. The verbiage next to the checkbox changes from Enabled to Enable.

How do I enable Chrome extensions on a Mac?

An alternative way to access the extensions settings on a Mac is to go to the menu bar, select Chrome > Preferences, then, in the Chrome Settings menu, select Extensions. The Extensions page lists the extensions installed on Chrome. A blue or gray toggle switch indicates whether or not the extension is enabled.


1 Answers

  • Add -pedantic and -Werror to the command line.

Using GCC 6.1.0 on Mac OS X 10.11.6, with your original code in a file ped73.c and my default compilation options, I get:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition ped73.c -o ped73 
ped73.c:3:5: error: function declaration isn’t a prototype [-Werror=strict-prototypes]
 int main(){
     ^~~~
ped73.c: In function ‘main’:
ped73.c:3:5: error: old-style function definition [-Werror=old-style-definition]
ped73.c:13:6: error: ‘main’ takes only zero or two arguments [-Werror=main]
  int main(int a){
      ^~~~
ped73.c:13:6: error: ‘main’ is normally a non-static function [-Werror=main]
$

Renaming the nested function to nested and using int main(void), I get:

$ gcc -O3 -g-std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes 
>     -Wold-style-definition -o ped73
$

Using the extra option -pedantic I get:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
>     -Wold-style-definition -pedantic ped73.c -o ped73 
ped73.c: In function ‘main’:
ped73.c:13:2: error: ISO C forbids nested functions [-Werror=pedantic]
  int nested(int a){
  ^~~
cc1: all warnings being treated as errors
$

Then what's the point of -std=c99?

The -std=c99 flag disables the GNU extensions that GCC thinks should be disabled — such as POSIX versions, etc. See C Dialect Options for the meaning of -std=; see Warning Options for the meaning of -pedantic.

-Wpedantic
-pedantic

Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std option used.

Valid ISO C and ISO C++ programs should compile properly with or without this option (though a rare few require -ansi or a -std option specifying the required version of ISO C). However, without this option, certain GNU extensions and traditional C and C++ features are supported as well. With this option, they are rejected.

-Wpedantic does not cause warning messages for use of the alternate keywords whose names begin and end with __. Pedantic warnings are also disabled in the expression that follows __extension__. However, only system header files should use these escape routes; application programs should avoid them. See Alternate Keywords.

Some users try to use -Wpedantic to check programs for strict ISO C conformance. They soon find that it does not do quite what they want: it finds some non-ISO practices, but not all—only those for which ISO C requires a diagnostic, and some others for which diagnostics have been added.

A feature to report any failure to conform to ISO C might be useful in some instances, but would require considerable additional work and would be quite different from -Wpedantic. We don't have plans to support such a feature in the near future.

Where the standard specified with -std represents a GNU extended dialect of C, such as ‘gnu90’ or ‘gnu99’, there is a corresponding base standard, the version of ISO C on which the GNU extended dialect is based. Warnings from -Wpedantic are given where they are required by the base standard. (It does not make sense for such warnings to be given only for features not in the specified GNU C dialect, since by definition the GNU dialects of C include all features the compiler supports with the given option, and there would be nothing to warn about.)

And there's also a different option that gives pedantic errors:

-pedantic-errors

Give an error whenever the base standard (see -Wpedantic) requires a diagnostic, in some cases where there is undefined behavior at compile-time and in some other cases that do not prevent compilation of programs that are valid according to the standard. This is not equivalent to -Werror=pedantic, since there are errors enabled by this option and not enabled by the latter and vice versa.


There are multiple questions about which GCC compiler options to use, including:

  • Recommended gcc warning options for C
  • What is the purpos of using -pedantic in GCC/G++ compiler
  • warning: implicit declaration of function when compiling C source

and no doubt there are many others that could be added to that list. Basically, the default options I use ensure that functions are declared before they are used (or, are defined as a static function before they are used), and that the function declarations have full prototypes — no empty parentheses () — and use the -Wall and -Wextra to spot a number of other routine problems, including mismatches between format strings and arguments to the printf() and scanf() families of functions.

like image 199
Jonathan Leffler Avatar answered Oct 12 '22 01:10

Jonathan Leffler