What does the -O2 option for gcc do?

eg. gcc -O2 file1.c -o file1.o

closed as off-topic by slm Jun 28 '18 at 2:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Requests for learning materials (tutorials, how-tos etc.) are off topic. The only exception is questions about where to find official documentation (e.g. POSIX specifications). See the Help Center and our Community Meta for more information." – slm
If this question can be reworded to fit the rules in the help center, please edit the question.


The -O options turn on compiler optimization. From man gcc:

   -O2 Optimize even more.  GCC performs nearly all supported
       optimizations that do not involve a space-speed tradeoff.  As
       compared to -O, this option increases both compilation time and the
       performance of the generated code.

For a list of the specific optimizations, refer to the manual pages.

Not the answer you're looking for? Browse other questions tagged or ask your own question.