GCC and GDB debugging summary

A, GCC:

gcc and g ++ is c / c ++ compiler.

Format: gcc [options] file ......

The main options:

Option Meaning

-v view the version of gcc compiler, gcc detailed procedure when performing display;

-o specifies the output file name is file, this name can not be with the same name as the source file;

-E preprocessing only, not compile, assemble, link;

-S Compile only, do not assemble, link;

-c compiler and assembler, not a link;

-g generate symbol information necessary for symbolic debugging tools (GNU's gdb), you want the source code debugging, you must add this option.

The specific process: a C / C ++ files to be pretreated (preprocessing), compiler (compliation), assembler (assembly) and connection (linking) to become an executable file.

Pre-generated files .i [preprocessor CPP] 
to convert the file into the pretreatment assembly language, generating file .s [egcs is compiler] 
has changed assembler object code (machine code) generated .o file [ assembler as] 
the connection object code, executable program generated [linker LD] 
source file:

(1) Pretreatment

g++ -E -o GCC.i GCC.c

File containing pretreatment (the include) inserted into the source file, the expanded macro definition, select the code to be used according to the conditional compilation command, and finally the output code into a ".i" file awaiting further processing.

GCC.i document reads as follows (values ​​are listed in part, to see the number of lines on the look out ...):

(2) Compile

gcc -S -o GCC.s GCC.i

The compiler is c / c ++ codes (such as the above ".i" file) into assembly code, file contents as follows:

(3) compilation

gcc -c -o GCC.o GCC.s

The second step of assembly code compilation translated into machine code outputted meet certain format on the general performance of the system Linux ELF object file (the OBJ file), the file shown in part as follows:

(4) Links

gcc -o GCC GCC.o

Links are generated will be compiled OBJ files, system libraries OBJ files, library files are linked, and ultimately generate platform-specific executable program can run in.

If you want to step operation is successful, then you can use: gcc GCC.c (unspecified output file, the default is a.out) or gcc -o GCC GCC.c

gcc and g ++ difference:

Let me talk about concepts: GCC: GNU Compiler Collection (GUN Compiler Collection), it can compile C, C ++, JAV, Fortran, Pascal, Object-C, Ada and other languages.

gcc GCC is the GUN C Compiler (C compiler)

g ++ is the GCC GUN C ++ Compiler (C ++ compiler)

The main difference:

1. For * .c * .cpp file and, gcc and cpp files were compiled as c (c syntax strength and cpp are not the same);

2. For * .c and * .cpp files, g ++ compiler is unified as cpp file;

3. When using g ++ compiler files, g ++ will automatically link the standard library STL, and gcc will not automatically link STL;

4. gcc when compiling C file, the predefined macros can be used is relatively small;

5. gcc when compiling cpp file / g ++ when you compile c file and cpp files (this time called gcc and g ++ compilers are cpp file), will add some additional macros are as follows:

#define __GXX_WEAK__ 1
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 4
#define __EXCEPTIONS 1
#define __private_extern__ extern

6. When compiler c ++ file gcc, in order to use the STL, need additional parameters -lstdc ++, but this does not mean gcc -lstdc ++ and g ++ equivalents, not only the difference between them;

Two, GDB debugging:

If you want to GDB debugger, then, you need: gcc -g -o GCC GCC.c

gdb commonly used commands
(. 1) gdb executable file: a file represented debug
(2) b Function Name / Number of lines: setting a breakpoint or several rows in front of a function
(3) run / r: represents runs , if the program being debugged, then, that for debugging again
(4) n / next: indicates execute the next line
(5) l / list: List Code default 10 rows (the current vertical position of the total 10 rows)
    List line number : list of 10 vertical line number of the line source
    list function name: function names are listed down a total of 10 source line
(6) s / step: represents a single step, into the function
(7) p / x variable name: press 16 binary value of the output variables
      / d: decimal press
      / o: octal
(8) set var variable name = value: the value of a variable
(9) bt (backtrace): View function call parameters and levels, abbreviated bt
(10) q / quit: exit
(11) finish: continuous operation function returns the current date, and then waits for a command to stop
(12) continue / c: jump to the next breakpoint, or jump to the observation point
(13) ptype variable name: You can see the type of a variable, abbreviated as pt
(14) Watch
    Action: to monitor the status of a general variable / memory address (which may be expression),
                as can monitor this variable / value whether a program memory read / write situation.
    There are three ways:
    . 1) .watch expr (specified variable / memory address / expression)
        once the value of expr change, the program will stop.
    2) .rwatch expr
        when expr is read, the program stopped.
    3) .awatch expr
         when expr is read or written, stop the program.

    Use watch steps:
        1) setting a breakpoint at break where the variables to be observed;.
        2) run execution until the breakpoint;.
        3) using the watch set watchpoints;.
        4) continue observation using the observation point set. Is there a change.
(15) start: program execution starts, stops in front of the main function of the first statement waits for a command
(16) info watchpoints: List all the observation points
    info breakpoints: See all breakpoints currently set
(17) d / delete [breakpoinsts num ] [Rang ...]         
      d / the delete: delete all breakpoints
      d / delete num: num to delete breakpoints breakpoints
      d / delete num1-num2: delete breakpoints breakpoints num1-num2 of
(18) enable num: When num No. breakpoint
(19) disable num: num number close breakpoint
(20) u / until: end the current cycle
 

Reference:

https://www.cnblogs.com/zhangsir6/articles/2956798.html
https://blog.csdn.net/czg13548930186/article/details/78331692
https://www.cnblogs.com/oxspirt/p/6847438.html
https://blog.csdn.net/nyist_zxp/article/details/81429615

Guess you like

Origin blog.csdn.net/BlueBirdssh/article/details/93628060