CMake_02_如何编译可调试文件

软件开发过程中,调试是必不可少的环节之一,让可执行文件”明牌“执行,不会漏过每一行代码,每一个变量的信息。从而帮助开发者快速定位到问题点。

  • 先看下没有调试信息的可执行文件是什么样子?
root@localhost:~/testWorkSpace/CPP11/vector$ readelf -S ./build/move 
There are 30 section headers, starting at offset 0x32e0:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .interp           PROGBITS         0000000000000238  00000238
       000000000000001c  0000000000000000   A       0     0     1
  [ 2] .note.ABI-tag     NOTE             0000000000000254  00000254
       0000000000000020  0000000000000000   A       0     0     4
  [ 3] .note.gnu.build-i NOTE             0000000000000274  00000274
       0000000000000024  0000000000000000   A       0     0     4
  [ 4] .gnu.hash         GNU_HASH         0000000000000298  00000298
       0000000000000024  0000000000000000   A       5     0     8
  [ 5] .dynsym           DYNSYM           00000000000002c0  000002c0
       0000000000000198  0000000000000018   A       6     1     8
  [ 6] .dynstr           STRTAB           0000000000000458  00000458
       000000000000019c  0000000000000000   A       0     0     1
  [ 7] .gnu.version      VERSYM           00000000000005f4  000005f4
       0000000000000022  0000000000000002   A       5     0     2
  [ 8] .gnu.version_r    VERNEED          0000000000000618  00000618
       0000000000000080  0000000000000000   A       6     3     8
  [ 9] .rela.dyn         RELA             0000000000000698  00000698
       0000000000000138  0000000000000018   A       5     0     8
  [10] .rela.plt         RELA             00000000000007d0  000007d0
       00000000000000a8  0000000000000018  AI       5    23     8
  [11] .init             PROGBITS         0000000000000878  00000878
       0000000000000017  0000000000000000  AX       0     0     4
  [12] .plt              PROGBITS         0000000000000890  00000890
       0000000000000080  0000000000000010  AX       0     0     16
  [13] .plt.got          PROGBITS         0000000000000910  00000910
       0000000000000008  0000000000000008  AX       0     0     8
  [14] .text             PROGBITS         0000000000000920  00000920
       0000000000000512  0000000000000000  AX       0     0     16
  [15] .fini             PROGBITS         0000000000000e34  00000e34
       0000000000000009  0000000000000000  AX       0     0     4
  [16] .rodata           PROGBITS         0000000000000e40  00000e40
       0000000000000005  0000000000000000   A       0     0     4
  [17] .eh_frame_hdr     PROGBITS         0000000000000e48  00000e48
       00000000000000dc  0000000000000000   A       0     0     4
  [18] .eh_frame         PROGBITS         0000000000000f28  00000f28
       00000000000003c0  0000000000000000   A       0     0     8
  [19] .gcc_except_table PROGBITS         00000000000012e8  000012e8
       0000000000000019  0000000000000000   A       0     0     1
  [20] .init_array       INIT_ARRAY       0000000000201d50  00001d50
       0000000000000010  0000000000000008  WA       0     0     8
  [21] .fini_array       FINI_ARRAY       0000000000201d60  00001d60
       0000000000000008  0000000000000008  WA       0     0     8
  [22] .dynamic          DYNAMIC          0000000000201d68  00001d68
       0000000000000210  0000000000000010  WA       6     0     8
  [23] .got              PROGBITS         0000000000201f78  00001f78
       0000000000000088  0000000000000008  WA       0     0     8
  [24] .data             PROGBITS         0000000000202000  00002000
       0000000000000018  0000000000000000  WA       0     0     8
  [25] .bss              NOBITS           0000000000202020  00002018
       0000000000000118  0000000000000000  WA       0     0     32
  [26] .comment          PROGBITS         0000000000000000  00002018
       0000000000000029  0000000000000001  MS       0     0     1
  [27] .symtab           SYMTAB           0000000000000000  00002048
       0000000000000a08  0000000000000018          28    48     8
  [28] .strtab           STRTAB           0000000000000000  00002a50
       000000000000077e  0000000000000000           0     0     1
  [29] .shstrtab         STRTAB           0000000000000000  000031ce
       0000000000000110  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)

使用gdb工具调试运行上述文件,只能看到一行行指令,是没有代码的。

  • 再看下带有调试信息的可执行文件
root@localhost:~/testWorkSpace/CPP11/vector$ readelf -S ./build/move 
There are 36 section headers, starting at offset 0xb8c8:

Section Headers:
  [Nr] Name              Type             Address           Offset
       Size              EntSize          Flags  Link  Info  Align
  [ 0]                   NULL             0000000000000000  00000000
       0000000000000000  0000000000000000           0     0     0
  [ 1] .interp           PROGBITS         0000000000000238  00000238
       000000000000001c  0000000000000000   A       0     0     1
  [ 2] .note.ABI-tag     NOTE             0000000000000254  00000254
       0000000000000020  0000000000000000   A       0     0     4
  [ 3] .note.gnu.build-i NOTE             0000000000000274  00000274
       0000000000000024  0000000000000000   A       0     0     4
  [ 4] .gnu.hash         GNU_HASH         0000000000000298  00000298
       0000000000000024  0000000000000000   A       5     0     8
  [ 5] .dynsym           DYNSYM           00000000000002c0  000002c0
       0000000000000198  0000000000000018   A       6     1     8
  [ 6] .dynstr           STRTAB           0000000000000458  00000458
       000000000000019c  0000000000000000   A       0     0     1
  [ 7] .gnu.version      VERSYM           00000000000005f4  000005f4
       0000000000000022  0000000000000002   A       5     0     2
  [ 8] .gnu.version_r    VERNEED          0000000000000618  00000618
       0000000000000080  0000000000000000   A       6     3     8
  [ 9] .rela.dyn         RELA             0000000000000698  00000698
       0000000000000138  0000000000000018   A       5     0     8
  [10] .rela.plt         RELA             00000000000007d0  000007d0
       00000000000000a8  0000000000000018  AI       5    23     8
  [11] .init             PROGBITS         0000000000000878  00000878
       0000000000000017  0000000000000000  AX       0     0     4
  [12] .plt              PROGBITS         0000000000000890  00000890
       0000000000000080  0000000000000010  AX       0     0     16
  [13] .plt.got          PROGBITS         0000000000000910  00000910
       0000000000000008  0000000000000008  AX       0     0     8
  [14] .text             PROGBITS         0000000000000920  00000920
       0000000000000512  0000000000000000  AX       0     0     16
  [15] .fini             PROGBITS         0000000000000e34  00000e34
       0000000000000009  0000000000000000  AX       0     0     4
  [16] .rodata           PROGBITS         0000000000000e40  00000e40
       0000000000000005  0000000000000000   A       0     0     4
  [17] .eh_frame_hdr     PROGBITS         0000000000000e48  00000e48
       00000000000000dc  0000000000000000   A       0     0     4
  [18] .eh_frame         PROGBITS         0000000000000f28  00000f28
       00000000000003c0  0000000000000000   A       0     0     8
  [19] .gcc_except_table PROGBITS         00000000000012e8  000012e8
       0000000000000019  0000000000000000   A       0     0     1
  [20] .init_array       INIT_ARRAY       0000000000201d50  00001d50
       0000000000000010  0000000000000008  WA       0     0     8
  [21] .fini_array       FINI_ARRAY       0000000000201d60  00001d60
       0000000000000008  0000000000000008  WA       0     0     8
  [22] .dynamic          DYNAMIC          0000000000201d68  00001d68
       0000000000000210  0000000000000010  WA       6     0     8
  [23] .got              PROGBITS         0000000000201f78  00001f78
       0000000000000088  0000000000000008  WA       0     0     8
  [24] .data             PROGBITS         0000000000202000  00002000
       0000000000000018  0000000000000000  WA       0     0     8
  [25] .bss              NOBITS           0000000000202020  00002018
       0000000000000118  0000000000000000  WA       0     0     32
  [26] .comment          PROGBITS         0000000000000000  00002018
       0000000000000029  0000000000000001  MS       0     0     1
  [27] .debug_aranges    PROGBITS         0000000000000000  00002041
       0000000000000150  0000000000000000           0     0     1
  [28] .debug_info       PROGBITS         0000000000000000  00002191
       0000000000003fe5  0000000000000000           0     0     1
  [29] .debug_abbrev     PROGBITS         0000000000000000  00006176
       0000000000000755  0000000000000000           0     0     1
  [30] .debug_line       PROGBITS         0000000000000000  000068cb
       0000000000000623  0000000000000000           0     0     1
  [31] .debug_str        PROGBITS         0000000000000000  00006eee
       000000000000351d  0000000000000001  MS       0     0     1
  [32] .debug_ranges     PROGBITS         0000000000000000  0000a40b
       0000000000000140  0000000000000000           0     0     1
  [33] .symtab           SYMTAB           0000000000000000  0000a550
       0000000000000a98  0000000000000018          34    54     8
  [34] .strtab           STRTAB           0000000000000000  0000afe8
       000000000000077e  0000000000000000           0     0     1
  [35] .shstrtab         STRTAB           0000000000000000  0000b766
       000000000000015e  0000000000000000           0     0     1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  l (large), p (processor specific)

好像没啥区别,过滤下看看

root@localhost:~/testWorkSpace/CPP11/vector$ readelf -S ./build/move | grep debug
  [27] .debug_aranges    PROGBITS         0000000000000000  00002041
  [28] .debug_info       PROGBITS         0000000000000000  00002191
  [29] .debug_abbrev     PROGBITS         0000000000000000  00006176
  [30] .debug_line       PROGBITS         0000000000000000  000068cb
  [31] .debug_str        PROGBITS         0000000000000000  00006eee
  [32] .debug_ranges     PROGBITS         0000000000000000  0000a40b

可执行文件中,正是多加了这些字段信息,才让GDB工具可以“明牌”运行。

  • 看到了目标效果,绕回本文主题,CMake工具如何生成带有调试信息的执行文件呢?

在CMake.txt文件中,设置如下编译参数就可以了(适用于CMake 3.14.7 版本)

add_definitions("-Wall -g")
set(CMAKE_BUILD_TYPE "Debug")

你可能感兴趣的:(CMake,Tools,cmake)