gcc preprocessor

3.13 Controlling the Preprocessor

Tersified by DGG
-E Expand macros and do not invoke the compiler.
Dependency Makefile
-M Output rules suitable for make describing the dependencies of the main file, containing the object file (without directorys), colon, and the names of included files, including those from -include or -imacros

If there are many included files then the rule is split into several lines using \-newline.
The rule has no commands. Does not suppress the debug output, such as -dM.
Use -MF file to avoid mixing debug output with the dependency rules or $DEPENDENCIES_OUTPUT
Implies -E and -w to suppresses warnings

-MM Like -M
omits files from system directories, header files included.
 gcc -M -MG -MM main.c app_led.c
 main.o: main.c app_config.h app_button.h ruuvi_boards.h \
  ruuvi_driver_error.h ruuvi_task_button.h ruuvi_task_gpio.h app_comms.h \
  ruuvi_interface_communication.h app_heartbeat.h ruuvi_driver_sensor.h \
  app_led.h ruuvi_interface_gpio.h app_log.h app_power.h app_sensor.h \
  ruuvi_interface_communication_radio.h ruuvi_task_sensor.h \
  ruuvi_interface_environmental_mcu.h ruuvi_interface_tmp117.h main.h \
  ruuvi_interface_gpio_interrupt.h ruuvi_interface_log.h \
  run_integration_tests.h ruuvi_interface_power.h \
  ruuvi_interface_scheduler.h ruuvi_interface_timer.h \
  ruuvi_interface_watchdog.h ruuvi_interface_yield.h ruuvi_task_flash.h \
  ruuvi_task_led.h ruuvi_task_adc.h
app_led.o: app_led.c app_led.h ruuvi_driver_error.h \
  ruuvi_interface_gpio.h ruuvi_boards.h ruuvi_task_led.h testingMacros.h \
  ruuvi_interface_log.h
Angle brackets or quotes in an #include do not determine whether that header appears in -MM dependency output.
-MG With -M or -MM
assumes missing header files are generated files and adds them to the dependency list .
The dependency filename is taken directly from the #include directive without prepending paths.
gcc -M -MG main.c 
main.o: main.c app_config.h app_button.h ruuvi_boards.h \
  ruuvi_driver_error.h ruuvi_task_button.h ruuvi_task_gpio.h app_comms.h \
  ruuvi_interface_communication.h app_heartbeat.h ruuvi_driver_sensor.h \
  app_led.h ruuvi_interface_gpio.h app_log.h app_power.h app_sensor.h \
  ruuvi_interface_communication_radio.h ruuvi_task_sensor.h \
  ruuvi_interface_environmental_mcu.h ruuvi_interface_tmp117.h main.h \
  ruuvi_interface_gpio_interrupt.h ruuvi_interface_log.h \
  /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdlib.h \
  /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/Availability.h \
…
  /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/secure/_common.h \
  run_integration_tests.h ruuvi_interface_power.h \
  ruuvi_interface_scheduler.h ruuvi_interface_timer.h \
  ruuvi_interface_watchdog.h ruuvi_interface_yield.h ruuvi_task_flash.h \
  ruuvi_task_led.h ruuvi_task_adc.h
Suppresses preprocessed output,
-MF file With -M or -MM
specifies file to write the dependency output to.
-Mno-modules Disable dependency generation for compiled module interfaces.
-MP for CPP Instructs CPP to add a target for each dependency (other than the main file), causing each to depend on nothing. These work around make errors if header files are removed without updating the Makefile
Typical output:
 test.o: test.c test.h 
    test.h:
-MT target Change the target of the rule from dependency generation.
Default: CPP takes the name of the main file and replaces the suffix.
For multiple targets, use multiple -MTs
For example
-MT '$(objpfx)foo.o' results in
         $(objpfx)foo.o: foo.c
-MQ target Like -MT but quotes characters which are special to Make.
-MQ '$(objpfx)foo.o' results in:
          $$(objpfx)foo.o: foo.c
The default target is automatically quoted, as if it were given with -MQ.
-MD -E (expand macros (?) ) is not implied. determines file based on -o.
the driver uses its argument with a suffix of .d, otherwise name without directories and suffix replaced with .d
With -E , -o specifies the dependency output file
Without -E, each -o specifies a target object file.
Used to generate a dependency output file as a side effect of the compilation process.
-MMD Like -MD also supress system headers
-fpreprocessed the input has been preprocessed, suppressing macro expansion, trigraph conversion, escaped newline splicing, and processing of most directives.
Removes comments, file preprocessed with -C without problems. The preprocessor acts as a tokenizer for the front ends.
Implied if the input has extension .i, .ii or .mi.
-fdirectives-only Do not expand macros.

With -E preprocessing is limited to directives such as #define, #ifdef, and #error, etc.
-dD is implied.

Definition of command line and most builtin macros is disabled.
Macros such as __LINE__, which are contextually dependent, are handled normally.
Enables compilation of files previously preprocessed with -E -fdirectives-only.

-fpreprocessed, takes precedence over -E . This enables full preprocessing of files previously preprocessed with -E -fdirectives-only.

-fmacro-prefix-map=old=new Files in directory old expand __FILE__ and __BASE_FILE__ as if the files were in new .
Changes an absolute path to a relative path by using . for new which result in reproducible builds that are location independent.
Affects __builtin_FILE() . See -ffile-prefix-map.
-fpch-deps When using precompiled headers (see Precompiled Headers), causes the dependency-output flags to list the files from the precompiled headers dependencies.
Default : only the precompiled header are listed and not the files that were used to create it, as those files are not consulted when a precompiled header is used.
-fpch-preprocess use a precompiled header (see Precompiled Headers) with -E.
Inserts #pragma GCC pch_preprocess "filename" in the output to mark the place where the precompiled header was found.
When -fpreprocessed is in use, the PCH is loaded.
Off by default, output is only suitable as input to GCC. switched on by -save-temps.
It is safe to edit the filename if the PCH file is available in a different location.
The filename may be absolute or relative.
Do not use this #pragma.
-fnoworking-directory linemarkers are output for current working directory at the time of preprocessing.
After the initial linemarker, a second linemarker with the current working directory followed by two slashes.
GCC uses this directory, when its present in the preprocessed input, as the directory emitted as the current working directory in some debugging information formats.

Implicitly enabled if debugging information is enabled,
Inhibited with the negated form -fno-working-directory.
If -P is present in the command line, has no effect, since no #line directives are emitted .

-P Inhibit generation of linemarkers when input is not C code,
-A -predicate=answer Make ( cancel ) an assertion with the predicate predicate and answer answer.
Comment Handling
-C Comments are passed to the output file, except for comments in processed directives, which are deleted along with the directive. Causes treatment of comments as tokens.
For example, comments appearing at the start of a directive line, become an ordinary source line, since the first token on the line is no longer a #.
-CC Like -C, Do not discard comments, including during macro expansion.
comments within macros are also passed // comments inside a macro are converted to /* … */

Generally used to support lint comments.

-traditional-cpp With -E or when invoking CPP explicitly, perform as pre-standard C preprocessors, not as ISO C

-trigraphs
 Trigraph:       ??(  ??)  ??<  ??>  ??=  ??/  ??'  ??!  ??-
    Replacement:      [    ]    {    }    #    \    ^    |    ~
default, ignored unless in standard-conforming modes . See -std and -ansi
-H Display the name of header and precompiled header files indented to show how deep in the #include stack it is.
Precompiled header file is displayed with ...! .Invalid with ...™
Macro processing
-Ftrack-macro-expansion
              [1|2]
Display macro expansions.
gcc -E -Ftr main.c > 0
> grep LOGA 0

Emit diagnostic about the macro expansion stack when an error occurs.
Probably unnecessary(ed) level of precision of token location tracking decreasing memory .
1 tracks tokens locations in a degraded mode for the sake of minimal memory overhead.
In this mode all tokens resulting from the expansion of an argument of a function-like macro have the same location.
2 tracks tokens locations completely, default.
-dM With -E generate a list of #define directives for macros defined during the execution, including predefined macros.
To display all predefined macros.
 touch foo.h
 cpp -dM foo.h
#define _LP64 1
#define __APPLE_CC__ 6000
#define __APPLE__ 1
#define __ATOMIC_ACQUIRE 2
…
#define __strong 
#define __tune_core2__ 1
#define __unsafe_unretained 
#define __weak __attribute__((objc_gc(weak)))
#define __x86_64 1
#define __x86_64__ 1i
Without -E a synonym for -fdump-rtl-mach . See (gcc)Developer Options.
-dD As with -dM but does not include the predefined macros and it outputs both #define and the result of preprocessing to stdout
-dN names of macros are output
-dU #undef directives are output for macros tested but undefined at the time.
As with -dD except only macros that are expanded or whose definedness is tested, are output when they are used or tested.
-dI Output #include directives in addition to the result of preprocessing.

Other letters are interpreted by the compiler and are ignored.

Special options
-Xpreprocessor option Pass option as an option to the preprocessor to supply system-specific preprocessor options that are not recognized.
For an option that takes an argument, use -Xpreprocessor twice, once for the option and once for the argument.
-no-integrated-cpp Perform preprocessing as a separate pass before compilation.
default, GCC performs preprocessing as an integrated part of input tokenization and parsing.
With this the appropriate language front end is invoked twice, once for preprocessing only and once for compilation. may be useful in with the -B or -wrapper to specify an alternate preprocessor or perform additional processing of the program source between normal preprocessing and compilation.
-fmax-include-depth=depth Set the maximum depth of the nested #include. default: 200.
-flarge-source-files Adjust GCC to expect large source files, at the expense of slower compilation and higher memory usage. After a large number of lines diagnostics do not include column numbers and -Wmisleading-indentation ceases to work.
The compiler outputs a message when this happens.
Passing -flarge-source-files increases the number of lines GCC processes before it stops tracking columns.
Character sets
-fexec-charset=charset Set the execution character set, used for string and character constants. default: UTF-8. charset can be any encoding supported by the system’s iconv library routine.
-fwide-exec-charset=charset Set the wide execution character set, used for wide string and character constants. The default is UTF-32 or UTF-16, whichever corresponds to the width of wchar_t. As with -fexec-charset, charset can be any encoding supported by the system’s iconv library routine; avoid encodings that do not fit exactly in wchar_t.
-finput-charset=charset Set the input character set, used for translation from the character set of the input file to the source character set used by GCC.
Default : UTF-8. overridden by the locale or this command-line option, which takes precedence
-pthread Define macros required for the POSIX threads .
Use this option for both compilation and linking.
-ftabstop=width Width of tabs for column numbers. default : 8. (don't user TAB characters)
-fextended-identifiers Accept universal character names and extended characters in identifiers.
default for C99 (and later C standard versions) and C++.
-fdollars-in-identifiers Accept $ in identifiers.
-fno-canonical-system-headers Do not shorten system header paths with canonicalization.
Defining macros during Preprocessor execution
-D name Define name as a macro, with definition 1.
-D name=definition definitions are processed as if they appeared during translation phase in a #define.
The definition is truncated by embedded newlines . Surround the argument list with parentheses before the equals sign as in
 -D'name(args…)=definition' 
Escape shell meta-characters.
-U name Undefined name

-D and -U are processed in the order -imacros file and -include file are processed after all -D and -U options.

-include file Process as if #include "file" appeared as the first line of the primary source .
The preprocessors working directory is searched searched for file then the remainder of the #include "" search chain as normal.
-imacros file Like -include except no output is forwarded from file.
Allows acquiring macros from a header without also processing its declarations. -imacros iis processed before -include.
-undef Do not predefine any system-specific or GCC-specific macros. The standard predefined macros remain defined.

Version information

On Raspberry pi
gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/6/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Raspbian 6.3.0-18+rpi1+deb9u1' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=arm-linux-gnueabihf- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-armhf/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-armhf --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-armhf --with-arch-directory=arm --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 6.3.0 20170516 (Raspbian 6.3.0-18+rpi1+deb9u1) 
Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/8/lto-wrapper Target: arm-linux-gnueabihf Configured with: ../src/configure -v --with-pkgversion='Raspbian 8.3.0-6+rpi1' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=arm-linux-gnueabihf- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libquadmath --disable-libquadmath-support --enable-plugin --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --disable-werror --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf Thread model: posix gcc version 8.3.0 (Raspbian 8.3.0-6+rpi1) on mac os version 12.0
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin21.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Original version GNU.org

There are additional options to control search paths for include files documented in Directory Options.

Options to control diagnostics are listed in Warning Options.