g++ compile options -g debug and -O optimization

34,941

You should not focus on the options but on what you want to achieve. My general comments regarding what you want to achieve:

  • usually there is a trade-off between fast and small. -O3 means fast. Os means as fast as possible without increasing the size.
  • you can use debug information (-g) with optimization (-O3), but the information is poor sometimes as there is not a direct connection between the source and the resulted code
  • if you strip the debug information (-s) from the executable, you will not be able to debug that executable easily (you can have the debug information saved separately but this is another story)

Always use the manual (accessed by typing in a shell "man g++", or by searching on the internet "man g++") and search for the options if you are curios what it does. If you have a higher level question, then you can ask (for example the difference between -g1 and -g3 is explained in the manual)

As a suggestion: use -O3. If you develop constantly the program and use GDB as debugger use: -ggdb -g3.

Edit: Regarding on when to apply them: usually you do not worry about phase it is, you just send the options and the g++ takes care of them. When you will want something more specific, then you can check more.

Share:
34,941
Jason
Author by

Jason

Okay, Good, Great!

Updated on July 03, 2020

Comments

  • Jason
    Jason almost 4 years

    I am not quite familiar with g++ compilers, especially the options, e.g., -s, -g, and -O.

    Firstly, can I ask when do these options take effect? During compile or link phase? I normally put all the options I need in both phases.

    Secondly, are there -g1, -g2, -g3 options? I know -g adds debugging info in the to executable. How do others differ?

    Thirdly, what does -s do? Do -s, -g, and -O3 work together? My target is to make the executable 1) run quickly, and 2) have small size if possible. What do you suggest?

  • Benjamin Bannier
    Benjamin Bannier almost 12 years
    GCC's info page (info gcc) is much more complete than its man page.