Skip to content

Instantly share code, notes, and snippets.

@corburn
Last active November 6, 2016 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corburn/917e3c1cd14c0da2aafaa6a3c776787f to your computer and use it in GitHub Desktop.
Save corburn/917e3c1cd14c0da2aafaa6a3c776787f to your computer and use it in GitHub Desktop.

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). See Options Controlling C Dialect.

An amendment to the 1990 standard was published in 1995. This amendment added digraphs and __STDC_VERSION__ to the language, but otherwise concerned the library. This amendment is commonly known as AMD1; the amended standard is sometimes known as C94 or C95. To select this standard in GCC, use the option -std=iso9899:199409 (with, as for other standard versions, -pedantic to receive all required diagnostics).

Conforming Freestanding Library

// https://gcc.gnu.org/onlinedocs/gcc-6.2.0/gcc/Standards.html#Standards
// The ISO C standard defines (in clause 4) two classes of conforming implementation.
// A conforming hosted implementation supports the whole standard including all the
// library facilities; a conforming freestanding implementation is only required to
// provide certain library facilities: those in <float.h>, <limits.h>, <stdarg.h>, and <stddef.h>;
#include <float.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>

// since AMD1, also those in <iso646.h>;
#include <iso646.h>

// since C99, also those in <stdbool.h> and <stdint.h>;
#include <stdbool.h>
#include <stdint.h>

// and since C11, also those in <stdalign.h> and <stdnoreturn.h>.
#include <stdalign.h>
#include <stdnoreturn.h>

// In addition, complex types, added in C99, are not required for freestanding implementations.

int main(int argc, char *argv[]) {
  return 0;
}

Freestanding vs Hosted Environment

The standard also defines two environments for programs, a freestanding environment, required of all implementations and which may not have library facilities beyond those required of freestanding implementations, where the handling of program startup and termination are implementation-defined; and a hosted environment, which is not required, in which all the library facilities are provided and startup is through a function int main (void) or int main (int, char *[]). An OS kernel is an example of a program running in a freestanding environment; a program using the facilities of an operating system is an example of a program running in a hosted environment.

GCC aims towards being usable as a conforming freestanding implementation, or as the compiler for a conforming hosted implementation. By default, it acts as the compiler for a hosted implementation, defining __STDC_HOSTED__ as 1 and presuming that when the names of ISO C functions are used, they have the semantics defined in the standard. To make it act as a conforming freestanding implementation for a freestanding environment, use the option -ffreestanding; it then defines __STDC_HOSTED__ to 0 and does not make assumptions about the meanings of function names from the standard library, with exceptions noted below. To build an OS kernel, you may well still need to make your own arrangements for linking and startup. See Options Controlling C Dialect.

...

GCC does not provide the library facilities required only of hosted implementations, nor yet all the facilities required by C99 of freestanding implementations on all platforms. To use the facilities of a hosted environment, you need to find them elsewhere (for example, in the GNU C library). See Standard Libraries.

Most of the compiler support routines used by GCC are present in libgcc, but there are a few exceptions. GCC requires the freestanding environment provide memcpy, memmove, memset and memcmp. Finally, if __builtin_trap is used, and the target does not implement the trap pattern, then GCC emits a call to abort.

If you are not using some other optimization option, consider using -Og (see Optimize Options with -g. With no -O option at all, some compiler passes that collect information useful for debugging do not run at all, so that -Og may result in a better debugging experience.

[DWARF] Version 4 may require GDB 7.0 and -fvar-tracking-assignments for maximum benefit.

GCC no longer supports DWARF Version 1, which is substantially different than Version 2 and later. For historical reasons, some other DWARF-related options (including -feliminate-dwarf2-dups and -fno-dwarf2-cfi-asm) retain a reference to DWARF Version 2 in their names, but apply to all currently-supported versions of DWARF.

  • -pedantic

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

-Werror
  Make all warnings into errors.
-Wfatal-errors
  This option causes the compiler to abort compilation on the first error occurred rather than trying to keep going and printing further error messages.
-Wall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment