fedora 29 工作站 x86_64 上的 gcc --version gcc (GCC) 8.3.1 20190223 (Red Hat 8.3.1-2) 手册。
不同的版本,但我认为它很有帮助。
调试程序的选项
...
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.
...
控制优化的选项
...
-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.
...
所以,我们可以看到-Og 是优化选项之一。 如果您没有使用其他优化选项,请考虑使用 -Og 和 -g。
示例:
#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;
}
编译:
[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
然后就可以看到编译后文件的大小了:
[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
然后您可以使用readelf(GNU readelf 版本 2.31.1-13.fc29) 重新检查这些文件中的调试信息。
[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
您可以看到仅通过-Og选项编译的文件中没有调试信息。您还可以使用readelf --debug-dump= 选项查看更多信息。例如readelf --debug-dump=aranges,info sample.g。还有readelf --headers sample.g | grep debug
见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]
可以使用gdb来检查:
[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)
然后你会得到 (no debugging symbols found) 文件,sample.Og。
4.8.x
对于 4.8.x 文档,-Og 未在 Debugging-Options 部分中提及,仅在 Optimize-Options 部分中介绍。