1

I am trying to compile a opensource component from source code. I am compiling all C files in that component using gcc command.

When I pass options in order -O2 -Os, binary is in few KB's. But when I pass options in order -Os -O2 binary size is large.

I do know that order matter in case of including sub-directories or Linking libraries in gcc command. Why order matters for optimization arguments of gcc command ?

I am using gcc version 4.9.1.

Krishna M
  • 1,135
  • 2
  • 16
  • 32

2 Answers2

1

Because it's just using the last1 option it sees.


1. From the man page: If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
1

From the GCC man page:

If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.

You can't combine -O2 and -Os on the command line.

But here's the description of -Os:

Optimize for size. -Os enables all -O2 optimizations that do not typically increase code size. It also performs further optimizations designed to reduce code size.

Looks like -Os is already doing what you want.

Mat
  • 202,337
  • 40
  • 393
  • 406