The GCC compiler compiles and links language source programs such as C, C + +, and assembler to executable programs.
GDB is a powerful program debugging tool developed by GNU under the Unix/linux.
Linux does not have a suffix name concept. However, GCC distinguishes the type of input file according to the suffix of the file:
. C C Language Source code files
. A library file consisting of a target file
. C,. CC,. cpp C + + source files
. h header File
. I after preprocessing the C language file
. II Pre-processed C + + files
. o The compiled target file
. s Assembly source files
. S pre-compiled compiled source files
The program consists of C, C + + source code to the executable file through a total of four stages:
Preprocessing this phase mainly for the source of the header file expansion, macro substitution and other operations
Compile this phase to compile the pre-processed code into a. S assembly file
Compile this phase to translate the assembly document into an. O Intermediate code
Link this stage to link user files and library files, resulting in an executable file
The GCC format has the following two types:
First: GCC option destination file name source file name
Second type: gcc source file name option destination file name
GCC four stages of process
Pretreatment: GCC-E-o hello.i hello.c
GCC-E Hello.c-o hello.i
Compile: gcc-s-o hello. S hello.i
Gcc-s hello.i-o Hello. S
Compilation: Gcc-c-o hello.o hello. S
Gcc-c Hello. S-o hello.o
Link: gcc-o Hello hello.o
GCC Hello.o-o Hello
You can also produce an executable file with one command: Gcc-o Hello hello.c
GCC Hello.c-o Hello
GCC Common options:
Compilation options
Options content destination file format
-c ; Compile only, do not link . O
-e is only preprocessed and does not compile  &N bsp; . I
-s compiles only, does not assemble . S
-o file generate files named file
-g contains debug information in the executable
-v output compilation process and compiler version information
-static link Static library
-I dir add dir header file search directory
-l dir Add dir Library file search Directory
-iname link named lib name.so The library file
Warnings and error options
-ansi output of ANSI C compliant warning messages
-pedantic output ANSI C standard all warning information
-wall all warning messages provided by GCC
-W Close all warning messages
-werror turn all warnings into errors
Optimization options
-O or O1-level optimization to tell GCC to reduce code length and execution time
-o2 Two-level optimization, in addition to O1, additional adjustments, such as processor instruction scheduling
-o3 three-level optimization, in addition to the O2, but also to carry out cyclic expansion and other adjustment optimization
Before using GDB for program debugging, the GCC compilation option must have the-G option to add debug information to the executable file.
Command line input: GDB executable file to start gdb debug it
GDB command:
List or L [line number or function name] To view the specified location code, the line number will display the line in the center of the screen. If multiple files, you can enter the L file: line number or function name
Run or R running code
Break or B [line number or function name] [conditional expression] sets a breakpoint at a line or function, or the breakpoint takes effect when the condition is met
Info B View breakpoint Information
Delete Breakpoint Number Remove Breakpoint
Disable breakpoint number invalidates a breakpoint
Enable breakpoint number to take effect on a breakpoint
Step or S stepping into sub-function
Next or N stepping without entering sub-functions
Continue or C continues execution until the next breakpoint or program ends
Print or P expression or variable to view the value of an expression or variable
Set variable variable = value Modify Variable value
This article is from the "_ Conan conan_" blog, please be sure to keep this source http://goodhx.blog.51cto.com/9727085/1734760
Introduction to GCC and GDB under Linux