Manual of gcc --version gcc (GCC) 8.3.1 20190223 (Red Hat 8.3.1-2)
on fedora 29 workstation x86_64.
Different version but I think it is helpful.
Options for Debugging Your Program
...
If you are not using some other optimization option, consider using -Og with -g. With no -O option at
all, some compiler passes that collect information useful for debugging do not run at all, so that -Og may
result in a better debugging experience.
...
Options That Control Optimization
...
-Og Optimize debugging experience. -Og enables optimizations that do not interfere with debugging. It
should be the optimization level of choice for the standard edit-compile-debug cycle, offering a
reasonable level of optimization while maintaining fast compilation and a good debugging experience.
...
So, we can see -Og
is one of the optimization options. If you are not using some other optimization option, consider using -Og with -g.
Sample:
#include <stdio.h>
int main(int argc, char *argv[]) {
int n;
for (n=0; n<10; n++) {
printf("Print Number: %d\n", n);
}
return 0;
}
compile:
[user@localhost myctest]$ gcc sample.c -o sample
[user@localhost myctest]$ gcc sample.c -o sample.Og -Og
[user@localhost myctest]$ gcc sample.c -o sample.g -g
[user@localhost myctest]$ gcc sample.c -o sample.Og.g -Og -g
Then you can see the size of the compiled files:
[user@localhost myctest]$ ls -l --human-readable sample*
-rwxrwxr-x. 1 user user 18K Aug 10 19:43 sample
-rw-rw-r--. 1 user user 162 Aug 10 19:43 sample.c
-rwxrwxr-x. 1 user user 21K Aug 10 19:43 sample.g
-rwxrwxr-x. 1 user user 18K Aug 10 19:43 sample.Og
-rwxrwxr-x. 1 user user 21K Aug 10 19:44 sample.Og.g
Then you can use readelf
(GNU readelf version 2.31.1-13.fc29) to re-check the debug info in those files.
[user@localhost myctest]$ readelf --debug-dump=aranges sample
[user@localhost myctest]$ readelf --debug-dump=aranges sample.g
Contents of the .debug_aranges section:
Length: 44
Version: 2
Offset into .debug_info: 0x0
Pointer Size: 8
Segment Size: 0
Address Length
0000000000401126 000000000000003d
0000000000000000 0000000000000000
[user@localhost myctest]$ readelf --debug-dump=aranges sample.Og
[user@localhost myctest]$ readelf --debug-dump=aranges sample.Og.g
Contents of the .debug_aranges section:
Length: 44
Version: 2
Offset into .debug_info: 0x0
Pointer Size: 8
Segment Size: 0
Address Length
0000000000401126 0000000000000028
0000000000000000 0000000000000000
You can see there is no debug information in the file compiled only by -Og
option. You also can check more information with options of readelf --debug-dump=
. For example readelf --debug-dump=aranges,info sample.g
. And readelf --headers sample.g | grep debug
See man readelf
:
--debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=loc,=Ranges,=pubtypes,=trace_info,=trace_abbrev,=trace_aranges,=gdb_index,=addr,=cu_index,=links,=follow-links]
You can use gdb to check:
[user@localhost myctest]$ gdb sample.Og
GNU gdb (GDB) Fedora 8.2-3.fc29
Copyright (C) 2018 Free Software Foundation, Inc.
...
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from sample.Og...(no debugging symbols found)...done.
(gdb)
Then you get (no debugging symbols found)
for the file, sample.Og.
4.8.x
For 4.8.x documentation, -Og
is not mentioned in the section Debugging-Options, only introduced in section Optimize-Options.