https://linux.die.net/man/1/g++ has a section specifically on debugging. It says
GCC allows you to use -g with -O
I think -g adds additional debug info into the binary to help debuggers; -O turns on complier optimization.
By default, our binaries are compiled with “-g3 -O2”. When I debug these binaries, I can see variables but lines are rearranged in source code, causing minor problems. See my blog posts on gdb.