Knowing compile-time options within source code?

Go To Last Post
4 posts / 0 new
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is there a way to tell which compiler options have been specified on the command line, so as to take appropriate action or issue warnings?


I'm interested in know whether "-flto" was specified for avr-gcc.

I notice that util/delay.h doesn't figure our whether something other than -O0 has been specified; I guess that's not a good sign.


  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One way to do this is -save-temps -fverbose-asm. If I do this then for main.c I get main.s that has:


    .file    "main.c"
__SP_H__ = 0x3e
__SP_L__ = 0x3d
__SREG__ = 0x3f
__tmp_reg__ = 0
__zero_reg__ = 1
 ;  GNU C99 (AVR_8_bit_GNU_Toolchain_3.6.1_1750) version 5.4.0 (avr)
 ;     compiled by GNU C version 4.7.4, GMP version 5.0.2, MPFR version 3.0.0, MPC version 0.9
 ;  GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
 ;  options passed:  -fpreprocessed main.i -mn-flash=1 -mno-skip-bug
 ;  -mmcu=avr5 -auxbase-strip main.o -g2 -O1 -Wall -std=gnu99
 ;  -funsigned-char -funsigned-bitfields -ffunction-sections
 ;  -fdata-sections -fpack-struct -fshort-enums -fverbose-asm
 ;  options enabled:  -Wmisspelled-isr -faggressive-loop-optimizations
 ;  -fauto-inc-dec -fbranch-count-reg -fchkp-check-incomplete-type
 ;  -fchkp-check-read -fchkp-check-write -fchkp-instrument-calls
 ;  -fchkp-narrow-bounds -fchkp-optimize -fchkp-store-bounds
 ;  -fchkp-use-static-bounds -fchkp-use-static-const-bounds
 ;  -fchkp-use-wrappers -fcombine-stack-adjustments -fcommon -fcompare-elim
 ;  -fcprop-registers -fdata-sections -fdefer-pop -fdwarf2-cfi-asm
 ;  -fearly-inlining -feliminate-unused-debug-types -fforward-propagate
 ;  -ffunction-cse -ffunction-sections -fgcse-lm -fgnu-runtime -fgnu-unique
 ;  -fguess-branch-probability -fident -fif-conversion -fif-conversion2
 ;  -finline -finline-atomics -finline-functions-called-once -fipa-profile
 ;  -fipa-pure-const -fipa-reference -fira-hoist-pressure
 ;  -fira-share-save-slots -fira-share-spill-slots -fivopts
 ;  -fkeep-static-consts -fleading-underscore -flifetime-dse
 ;  -flto-odr-type-merging -fmath-errno -fmerge-constants
 ;  -fmerge-debug-strings -fmove-loop-invariants -fomit-frame-pointer
 ;  -fpack-struct -fpeephole -fprefetch-loop-arrays -freg-struct-return
 ;  -fsched-critical-path-heuristic -fsched-dep-count-heuristic
 ;  -fsched-group-heuristic -fsched-interblock -fsched-last-insn-heuristic
 ;  -fsched-rank-heuristic -fsched-spec -fsched-spec-insn-heuristic
 ;  -fsched-stalled-insns-dep -fschedule-fusion -fsemantic-interposition
 ;  -fshow-column -fshrink-wrap -fsigned-zeros -fsplit-ivs-in-unroller
 ;  -fsplit-wide-types -fssa-phiopt -fstdarg-opt
 ;  -fstrict-volatile-bitfields -fsync-libcalls -ftoplevel-reorder
 ;  -ftrapping-math -ftree-bit-ccp -ftree-ccp -ftree-ch
 ;  -ftree-coalesce-vars -ftree-copy-prop -ftree-copyrename -ftree-dce
 ;  -ftree-dominator-opts -ftree-dse -ftree-forwprop -ftree-fre
 ;  -ftree-loop-if-convert -ftree-loop-im -ftree-loop-ivcanon
 ;  -ftree-loop-optimize -ftree-parallelize-loops= -ftree-phiprop
 ;  -ftree-pta -ftree-reassoc -ftree-scev-cprop -ftree-sink -ftree-slsr
 ;  -ftree-sra -ftree-ter -funit-at-a-time -fvar-tracking
 ;  -fvar-tracking-assignments -fverbose-asm -fzero-initialized-in-bss

Presumably you could then have a post build script (Python?) that parsed the "options passed:" from this?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That requires running the compiler twice and possibly fixing it if the output format changes.

Another possibility is to use a common file from which one generates

both a header file to #include and an @file to give to the compiler.

Iluvatar is the better part of Valar.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah.  For the LTO case I'm interested in, I can do something like:


void LTO_Not_Enabled(void) __attribute__ (( error("") ));
extern char bitmasks[];  // The array I want accessed "constantly"
  if (bitmasks[0] == IMPOSSIBLE_VALUE)


For the LTO case, the external array will be available at link time, and the LTO_Not_Enabled() clause will never be referenced.

If LTO is not disabled, the compiler will need to actually generate code for the test that includes the call, and it will generate an error.


Presumably, any global extern variable could be used; it should never actually need accessed by code, so it won't make it to the final binary anyway.

But it's vaguely satisfying to use a variable that has something to do with the function being performed.