带有调试选项的 GCC 编译阶段

gcc compile stages with debug options

本文关键字:编译 GCC 调试 选项      更新时间:2023-10-16

我正在尝试研究GCC的不同编译阶段。

手动逐阶段编译

$ g++ -E main.cpp -o main.i                  # I1
$ g++ -S main.i -o main.s                    # S1
$ g++ -S main.i -o main.debug.s -ggdb -g3    # S2
$ as main.s       -o main.as.o               # O1
$ as main.debug.s -o main.as.debug.o         # O2

并完全使用"仅 g++"再次编译

$ g++ -c main.cpp -o main.gcc.o                  # G1
$ g++ -c main.cpp -o main.gcc.debug.o -ggdb -g3  # G2

在最后

main.as.omain.gcc.o 相同,没有调试信息

main.as.debug.omain.gcc.debug.o 非常不同

为什么?我错过了什么吗?

这是工具版本

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++-7
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/7.2.0/libexec/gcc/x86_64-apple-darwin13.4.0/7.2.0/lto-wrapper
Target: x86_64-apple-darwin13.4.0
Configured with: ../configure --build=x86_64-apple-darwin13.4.0 --prefix=/usr/local/Cellar/gcc/7.2.0 --libdir=/usr/local/Cellar/gcc/7.2.0/lib/gcc/7 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-7 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --enable-checking=release --with-pkgversion='Homebrew GCC 7.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-nls
Thread model: posix
gcc version 7.2.0 (Homebrew GCC 7.2.0)
$ as -v
Apple Inc version cctools-862, GNU assembler version 1.38

更新

使用 -v 和 -save-temps 选项,此处显示了等效的命令

#####
# $ g++ -E main.cpp -o main.i                # I1
$ cc1plus -E -quiet -v -D__DYNAMIC__ main.cpp -o main.i -fPIC -mmacosx-version-min=10.9.4 -mtune=core2
#####
# $ g++ -S main.i -o main.s                  # S1
$ cc1plus -fpreprocessed main.i -fPIC -quiet -dumpbase main.i -mmacosx-version-min=10.9.4 -mtune=core2 -auxbase-strip main.s -version -fdump-tree-all-graph -o main.s
#####
# $ g++ -S main.i -o main.debug.s -ggdb -g3  # S2
$ cc1plus -fpreprocessed main.i -fPIC -quiet -dumpbase maini -mmacosx-version-min=10.9.4 -mtune=core2 -auxbase-strip main.debug.s -ggdb -g3 -version -o main.debug.s
#####
$ as main.s       -o main.as.o               # O1
$ as main.debug.s -o main.as.debug.o         # O2
#####
# g++ -c main.cpp -o main.gcc.o                  # G1
$ cc1plus -E -quiet -v -D__DYNAMIC__ main.cpp -fPIC -mmacosx-version-min=10.9.4 -mtune=core2 -fpch-preprocess -o main.ii
$ cc1plus -fpreprocessed main.ii -fPIC -quiet -dumpbase main.cpp -mmacosx-version-min=10.9.4 -mtune=core2 -auxbase-strip main.gcc.o -version -o main.s
$ as -arch x86_64 -v -force_cpusubtype_ALL -o main.gcc.o main.s
#####
# g++ -c main.cpp -o main.gcc.debug.o -ggdb -g3  # G2
$ cc1plus -E -quiet -v -dD -D__DYNAMIC__ main.cpp -fPIC -mmacosx-version-min=10.9.4 -mtune=core2 -ggdb -g3 -fworking-directory -fpch-preprocess -o main.ii
$ cc1plus -fpreprocessed main.ii -fPIC -quiet -dumpbase main.cpp -mmacosx-version-min=10.9.4 -mtune=core2 -auxbase-strip main.gcc.debug.o -ggdb -g3 -version -o main.s
$ as -arch x86_64 -v -force_cpusubtype_ALL -o main.gcc.debug.o main.s
#####

哎呀,这是我微不足道的失败。

原始帖子

$ g++ -E main.cpp -o main.i                  # I1
$ g++ -S main.i -o main.s                    # S1
$ g++ -S main.i -o main.debug.s -ggdb -g3    # S2

我也需要使用调试选项调用 -E 并进入下一阶段......

应该改为...

$ g++ -E main.cpp -o main.debug.i  -ggdb -g3       # I2
$ g++ -S main.debug.i -o main.debug.s -ggdb -g3    # S2'

那么以下阶段将生成相同的机器代码...