Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 567
jason.Z Avatar asked Sep 12 '12 10:09

jason.Z


1 Answers

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.

like image 70
vladmihaisima Avatar answered Oct 20 '22 04:10

vladmihaisima