Any time you use something compiler specific you’re screwing yourself in the long run. You never know how much you’re going to want to copy over to a new project. In my experience every piece of source you write will change compilers once unless you write everything from scratch for every project/team. GCC is a pretty common tool chain in embedded. ARM themselves are the contributor and maintainer for the arm port so it’s damn good. Also Microchip’s XC compilers are mostly gcc (the PIC16/18/24 compilers I believe are from HiTech). Answer from Bryguy3k on reddit.com
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › C-Extensions.html
C Extensions (Using the GNU Compiler Collection (GCC))
GNU C provides several language ... features in conditional compilation, check for a predefined macro __GNUC__, which is always defined under GCC. These extensions are available in C and Objective-C....
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › C_002b_002b-Extensions.html
C++ Extensions (Using the GNU Compiler Collection (GCC))
Next: GNU Objective-C Features, Previous: Built-in Functions Provided by GCC, Up: Introduction [Contents][Index] The GNU compiler provides these extensions to the C++ language (and you can also use most of the C language extensions in your C++ programs). If you want to write code that checks whether these features are available, you can test for the GNU compiler the same way as for C programs: check for a predefined macro __GNUC__.
🌐
Sergioprado
sergioprado.blog › home › gcc extensions to the c language
GCC extensions to the C language - sergioprado.blog
March 3, 2020 - All of the examples were compiled with GCC 7.4.0. The extensions are available without any extra compiler flag because by default GCC 7.4 compiles with -std=gnu11, which means support for the ISO C11 standard plus GCC extensions.
🌐
Debrouxl
debrouxl.github.io › gcc4ti › gnuexts.html
GNU C Language Extensions
This part of the documentation is a modified version of the C Extensions section of the GCC Manual. Therefore it is licensed under the GNU Free Documentation License. GCC4TI (like all GNU C compilers) provides several language features not found in ISO standard C.
🌐
Reddit
reddit.com › r/embedded › do you use gcc extensions in your c code?
r/embedded on Reddit: Do you use GCC extensions in your C code?
May 25, 2023 -

I just learnt today that function-like macro such as the one below is a GCC extension (Statement Expression extensions) and not part of standard C..

#define min(x, y) ({ \
    typeof(x) _x = (x); \
    typeof(y) _y = (y); \
    _x < _y ? _x : _y; \
})

Do you usually avoid this kind of gcc extension stuff when you program for embedded systems?

Well an obvious answer is it depends if your code is supposed to run on development platforms that are not part of gcc-toolchain. In that case please enlighten me how widely used is gcc in the first place, or if non-gcc compilers will usually try to support popular gcc extensions as well.

Thanks.

🌐
GNU
gcc.gnu.org › extensions.html
GCC extensions - GNU Project
This is a list of (experimental) extensions to GCC which, for one reason or the other, did not (yet) make it into the official source tree.
🌐
Ticalc
tigcc.ticalc.org › doc › gnuexts.html
GNU C Language Extensions
Likewise, use __imag__ to extract the imaginary part. This is a GNU extension; for values of floating type, you should use the ISO C99 functions crealf, creal, creall, cimagf, cimag and cimagl, declared in <complex.h> (not yet available in TIGCC) and also provided as built-in functions by GCC.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.1 › gcc › C-Extensions.html
Using the GNU Compiler Collection (GCC)
Vector Extensions: Using vector instructions through built-in functions. Other Builtins: Other built-in functions. Target Builtins: Built-in functions specific to particular targets. Pragmas: Pragmas accepted by GCC.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-9.1.0 › gcc › C-Extensions.html
Using the GNU Compiler Collection (GCC): C Extensions
Next: C++ Extensions, Previous: C++ Implementation, Up: Top [Contents][Index] GNU C provides several language features not found in ISO standard C. (The -pedantic option directs GCC to print a warning message if any of these features is used.) To test for the availability of these features ...
Find elsewhere
Top answer
1 of 1
22
  • 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.

🌐
University of Colorado Boulder
home.cs.colorado.edu › ~main › cs1300 › doc › gnu › gcc_5.html
Using and Porting the GNU Compiler Collection (GCC) - Extensions to the C++ Language
However, side effects in X or Y may cause unintended behavior. For example, MIN (i++, j++) will fail, incrementing the smaller counter twice. A GNU C extension allows you to write safe macros that avoid this kind of problem (see section Naming an Expression's Type).
🌐
O'Reilly
oreilly.com › library › view › linux-system-programming › 0596009585 › apa.html
A. GCC Extensions to the C Language - Linux System Programming [Book]
Appendix A. GCC Extensions to the C LanguageThe GNU Compiler Collection (GCC) provides many extensions to the C language, some of which have proven to be of particular... - Selection from Linux System Programming [Book]
🌐
Berkeley EECS
people.eecs.berkeley.edu › ~necula › cil › cil010.html
GCC Extensions
Attributes for functions, variables and types. In fact, we have a clear specification (see Section 6.4) of how attributes are interpreted. The specification extends that of gcc.
🌐
Labor-liber
labor-liber.org › en › gnu-linux › development › extensions
GCC and File Extensions - Development with GNU/Linux - labor-liber
An alternative is to use the -x option of GCC to specify the language of input files explicitly. -x none is the same as the default behaviour, and causes files to be handled according to their extension.
🌐
Pepas
leopard-adc.pepas.com › documentation › DeveloperTools › gcc-4.2.1 › gcc › C-Extensions.html
C Extensions - Using the GNU Compiler Collection (GCC)
Next: C++ Extensions, Previous: C Implementation, Up: Top · GNU C provides several language features not found in ISO standard C. (The -pedantic option directs GCC to print a warning message if any of these features is used.) To test for the availability of these features in conditional ...
🌐
GNU
gcc.gnu.org › onlinedocs › gcc › Vector-Extensions.html
Vector Extensions (Using the GNU Compiler Collection (GCC))
Next: Builtins for Atomic Memory ... by GCC [Contents][Index] On some targets, the instruction set contains SIMD vector instructions which operate on multiple values contained in one large register at the same time. For example, on the x86 the MMX, 3DNow! and SSE extensions can be used ...