-g just adds debugging info (source code references) into the .elf file and doesn't affect the code generated. All the extra info gets thrown away when you convert the .elf to a .uf2 or .bin so in that sense it's harmless (maybe increasing compile time fractionally). Leaving it on is useful if you like to look at disassembly of the compiler output (objdump -r -S) as you get to see the source code alongside the assembly.
-O0 is described as being "for debugging" as it keeps the code unoptimised and so the assembly generated exactly matches the C source that you wrote (but as a result is usually hopelessly inefficient).
There's nothing to stop you using a debugger (or objump -S) on code that's been compiled with -O2 or -O3, it's just that the resulting code will look odd and, for example, you may not be able to place a breakpoint on the statement you want because that statement no longer exists (the compiler noticed it was redundant and got rid of it).
I hardly ever use -O0 as the disadvantages outweigh the advantages. For the sake of some cosmetic improvement in the debugger, you have much slower code and more importantly you are debugging code which doesn't match what you are ultimately going to use for production. It's easy for bugs to be masked by -O0, where you accidentally invoked undefined behaviour but -O0 lets you get away with it.
-O0 is described as being "for debugging" as it keeps the code unoptimised and so the assembly generated exactly matches the C source that you wrote (but as a result is usually hopelessly inefficient).
There's nothing to stop you using a debugger (or objump -S) on code that's been compiled with -O2 or -O3, it's just that the resulting code will look odd and, for example, you may not be able to place a breakpoint on the statement you want because that statement no longer exists (the compiler noticed it was redundant and got rid of it).
I hardly ever use -O0 as the disadvantages outweigh the advantages. For the sake of some cosmetic improvement in the debugger, you have much slower code and more importantly you are debugging code which doesn't match what you are ultimately going to use for production. It's easy for bugs to be masked by -O0, where you accidentally invoked undefined behaviour but -O0 lets you get away with it.
Statistics: Posted by arg001 — Mon Oct 14, 2024 7:50 am