gcc
Common Options
-C only compiles non-linked, generate target file ". O"
-S compiles no assembly, generates assembly code
-E only precompiled, no other processing
- g contains standard debug information in an executable program
-o file to designate file files as output files
-V Prints the command line information and compiler version of the compiler's internal compilation procedures
-I dir add the dir directory to the search path list of the header file
GCC Library Options List
-static static compilation, that is, link static libraries, prohibit the use of dynamic libraries
-shared
1. Dynamic library files can be generated
2. Dynamically compile, link dynamic libraries as much as possible, and link static libraries with the same name only if there is no dynamic library (
Default Options, can be omitted)
-L dir adds the Dir directory to the list of search paths in the library file
-lname Link is called a libname.a (static library) or libname.so (Dynamic Library) library file. If two libraries are present, link them according to the compilation method (-static or-shared)
-fpic (or-fpic) generates target code that uses relative address-independent location. A dynamic library file is then typically generated from the PIC target file using the GCC-static option
Linking to a static library can make the user's program bloated and difficult to upgrade, but may be easier to deploy. Linking to a dynamic library makes the user's program lightweight and easy to upgrade, but it can be difficult to deploy
gcc Warning and error options list
-ANSI support for ANSI compliant C programs
-pedantic allows all warning messages listed in the ANSI C standard to be issued
-pedantic-error allows all error messages listed in the ANSI C standard to be issued
-W Close all alarms
-wall allows the issuance of all useful alarm information provided by GCC
-werror turns all alarm messages into error messages and terminates the compilation process when an alarm occurs
#include <stdio.h>
void Main ()
{
Long long tmp = 1;
printf ("Bad code\n");
return 0;
}
Gcc–ansi Warning.c–o Warning
WARNING.C: In the function "main":
Warning.c:7 Warning: In a function with no return value, "return" with return value
Warning.c:4 Warning: The return type of "main" is not "int"
This option does not find "long long"
Gcc–pedantic Warning.c–o Warning
WARNING.C: In the function "main":
Warning.c:5 Warning: ISO C90 does not support "long long"
Warning.c:7 Warning: In a function with no return value, "return" with return value
Warning.c:4 Warning: The return type of "main" is not "int"
Use this option to find out "long long"
Gcc–wall Warning.c–o Warning
Warning.c:4 Warning: The return type of "main" is not "int"
WARNING.C: In the function "main":
Warning.c:7 Warning: In a function with no return value, "return" with return value
Warning.c:5 WARNING: Unused variable "tmp"
Use this option to isolate the unused variable tmp, but it does not identify the invalid data type
GCC can also use options to specify warnings separately for common errors
Optimization Options-on
n is an integer n that represents the optimization level and its corresponding optimization effect may not be exactly the same, the more typical range is from 0 to 2 or 3;
"-O" is the main thread jump and deferred stack (Deferred stack Pops) two optimizations
"-o2" In addition to the completion of all "-o1" level of optimization, but also to do some additional adjustment work, such as processor instruction scheduling.
"-o3" also includes cyclic expansion and other optimizations related to processor characteristics.
Disadvantages:
While the optimization option can speed up the code, it is a big challenge for debugging.
Since the code is optimized, the variables originally declared and used in the source program are probably no longer in use, the control flow may suddenly jump to unexpected places, and the looping statements may become ubiquitous because of the loop expansion, all of which will be a nightmare for debugging. It is recommended that you do not use any optimization options when debugging, and only consider optimizing the program when it is finally released.
GCC architecture-related Options list
The-mcpu=type uses the appropriate CPU instructions for different CPUs. Selectable type with i386, i486, Pentium and i686
-MIEEE-FP comparison of floating-point numbers using IEEE standards
-MNO-IEEE-FP comparison of floating-point numbers without the use of IEEE standards
-msoft-float output contains target code for floating-point library calls
-mshort the int type as a 16-bit processing, equivalent to short int
-MRTD forcibly returns a function with a fixed number of function parameters with ret num, saving an instruction that invokes a function
GDB Debugger
#include <stdio.h>int sum (int); int main () { printf ("The sum of 1-50 is%d\n", SUM (50));} int sum (int n) { int i,sum; for (i = 1,sum = 0; I <= N; ++i) { sum + = i; } return sum;}
Gcc-g Gdbt.c-o GDBT
GDB GDBT
GNU gdb (gdb) Red Hat Enterprise Linux (7.2-60.el6_4.1)
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I686-redhat-linux-gnu".
For bugs reporting instructions, please see:
Reading symbols From/root/gdbt...done.
(GDB)
(1) View the file.
Type "L" (list) to view the loaded file
(gdb) l1#include <stdio.h>23int sum (int), 45int main () {6 printf ("The sum of 1-50 is%d\n", sum); 7}89int sum ( int n) {ten int i,sum; (gdb) one for (i = 1,sum = 0; I <= N; ++i) { sum + = i;13 }14 return sum;15} (GDB) Line number is out of range; GDBT.C has lines.
GDB lists the source code explicitly given the corresponding line number, so that you can greatly place the code positioning.
(2) Set breakpoints.
Setting breakpoints in GDB is very simple, just add the corresponding line number after "B" (this is the most common way, there are other ways to set breakpoints)
(GDB)
b
Breakpoint 1 at 0x8048402:file gdbt.c, line 12.
Setting a breakpoint with a line number means that the code stops before it runs to the corresponding row, as in the preceding example, the code is paused before the line 12th (and no line 12th is running)
(3) View breakpoint condition.
After the breakpoint is set, the user can type "info B" to see the setting of a breakpoint, and in GDB you can set multiple breakpoints.
(GDB)
Info b
Num Type Disp Enb Address What
1 breakpoint Keep y 0x08048402 in sum at Gdbt.c:12
The user type "Backrace" (only "BT") on the breakpoint to find the calling function (stack), which is used extensively in program debugging and is often used to troubleshoot errors or to monitor the call stack.
(GDB) bt
#0 sum (n=50) at Gdbt.c:12
#1 0x080483d9 in Main () at Gdbt.c:6
(4) run the code.
Then you can run the code, and GDB runs the code from the first line by default, type "R" (run) (you can add a line number after R if you want to start from the specified line in the program).
(GDB) R
Starting program:/ROOT/GDBT
Breakpoint 1, sum (n=50) at Gdbt.c:12
12 sum + = i;
(5) View the value of the variable.
After the program stops running, the programmer's job is to look at the value of the relevant variable at the breakpoint. Type "P" + variable values in GDB
(GDB) p sum
$ = 0
(GDB) P I
$ = 1
(GDB)
GDB displays the value of the variable with the "$N" tag before the corresponding value, which is the reference mark of the current variable value, so you can write the "$N" directly later if you want to refer to the variable again without having to write a lengthy variable name.
(GDB) p- $
$1
(6) Single step operation.
Single-step operations can use the command "n" (next) or "s" (step), the difference between them: if there is a function call, "s" will enter the function and "n" does not enter the function. Thus, "s" is similar to "step in" of tools such as Uisual, "n" is similar to "step over" in tools such as uisual.
(GDB) n
11 for (i = 1,sum = 0; I <= N; ++i) {
(GDB) s
Breakpoint 1, sum (n=50) at Gdbt.c:12
12 sum + = i;
(7) Delete Breakpoint
Delete num This num is the num of info b
(GDB) Delete 1 (d 1 same-effect)
(GDB) Info b
Num Type Disp Enb Address What
2 Breakpoint Keep Y 0x080483cd in main at Gdbt.c:6
(8) Recovery program run
After checking the required variables and stack conditions, you can use the command "C" (continue) to restore the program to normal operation. At this point, it will run out of the remaining programs and show the results of the remaining programs
(GDB) C
Continuing.
The sum of 1-50 is 1275
In GDB, the running state of the program has "run", "pause" and "stop" 3 kinds, where the "pause" state for the program encountered a breakpoint or a point of view, the program temporarily stopped running, and the address of the function, function parameters, the local variables inside the function will be pressed into the "stack" (stack). Therefore, in this state, you can view various properties such as the variable value of the function. However, after the function is in a "stopped" state, the stack is automatically undone, and it cannot view the various information.
GDB Basic Commands
GDB commands can be looked up by looking for help, because GDB has a lot of commands, so GDB's help divides it into many kinds (classes), and the user can find the corresponding command by further viewing the relevant class.
(GDB) help (abbreviated h)
List of classes of commands:
Aliases--Aliases of other commands
Breakpoints--Making program stop at certain points
Data--Examining data
Files--Specifying and examining files
Internals--Maintenance commands
Obscure--obscure features
Running--Running the program
Stack--Examining the stack
Status--Status inquiries
Support – Support facilities
Tracepoints--tracing of program execution without stopping the program
user-defined--user-defined commands
Type ' help ' followed by a class name for a list of commands in that class.
Type ' help all ' for the list of all commands.
Type ' help ' followed by command name for full documentation.
Type "Apropos word" to search for commands related to "word".
Command name abbreviations is allowed if unambiguous.
(GDB) H data
Examining data.
List of commands:
Append--Append target code/data to a local file
Append binary--append target code/data to a raw binary file
Append binary Memory--append contents of memory to a raw binary file
Append binary Value--Append the value of an expression to a raw binary file
Append memory--Append contents of memory to a raw binary file
Append value--Append the value of an expression to a raw binary file
Call-a function
Disassemble--disassemble a specified section of memory
Display--Print value of expression EXP each time the program stops
...
(GDB) H append
Append Target Code/data to a local file.
List of Append subcommands:
Append binary--append target code/data to a raw binary file
Append memory--Append contents of memory to a raw binary file
Append value--Append the value of an expression to a raw binary file
Type ' help append ' followed by APPEND subcommand name for full documentation.
Type "Apropos word" to search for commands related to "word".
Command name abbreviations is allowed if unambiguous.
(gdb) h append binary
Append target code/data to a raw binary file.
List of append binary subcommands:
Append binary Memory--append contents of memory to a raw binary file
Append binary Value--Append the value of an expression to a raw binary file
Type ' help append binary ' followed by append binary subcommand name for full documentation.
Type "Apropos word" to search for commands related to "word".
Command name abbreviations is allowed if unambiguous.
(gdb) h Append binary Value
Append the value of an expression to a raw binary file.
Arguments is FILE EXPRESSION. Writes the value of EXPRESSION
To the specified FILE in raw target ordered bytes.
user-known command name, directly type "help [command]" can also, tab can command completion。
The commands in GDB are divided into the following categories: Working environment-related commands, setting breakpoints and Recovery commands, source code viewing commands, viewing run data-related commands, and modifying run parameters commands
1. Work Environment related commands
GDB can not only debug the running program, but also to the program-related work environment for the corresponding settings, and even can use the command in the shell to carry out related operations, the function is extremely powerful
Set argsRun-time parameters specify run-time parameters, such as set args 2
Show argsView the set up run parameters
Path dirSet the run path of the program
Show PathsTo view the running path of a program
set Environment var [=value]Setting environment variables
show Environment [var]View environment variables
CD dirEnter the Dir directory, equivalent to the CD command in the shell
PWDShow Current working directory
shell Commandcommand commands to run the shell
2. Set breakpoints and Restore commands
Gdb
Common commands for setting breakpoints and recoveries in
Info bView the breakpoint you set
Break [FileName:] line number or function name < conditional expression >Set breakpoints
tbreak [filename:] line number or function name < conditional expression >Set a temporary breakpoint that is automatically deleted when it arrives
Delete [breakpoint number]Deletes the specified breakpoint, whose breakpoint number is the first column in "Info B". Delete all breakpoints If the default breakpoint number
disable [breakpoint number]Stop specifying breakpoints, and use "info B" to still see this breakpoint. Same as Delete, if the default breakpoint number stops all breakpoints
enable [breakpoint number]Activates a breakpoint that is disable stopped by activating the specified breakpoint
condition [Breakpoint number] < conditional expression >Modify the conditions of the corresponding breakpoint
Ignore [breakpoint number]<num>Ignore the corresponding breakpoint num times during program execution
StepThe single-step recovery program runs and enters the function call
NextSingle-Step recovery program runs, but does not enter function calls
FinishRun the program until the current function finishes returning
CContinue executing functions until the function ends or encounters a new breakpoint
There are several ways to set breakpoints in GDB: One is to set breakpoints on a line, and you can also set a function breakpoint and a conditional breakpoint.
1) Function breakpoint.
setting breakpoints by function in gdb simply
Name of functionAfter the command "B" is listed
(gdb) b sum
Breakpoint 4 at 0x80483f2:file gdbt.c, line 11.
(GDB) Info b
Num Type Disp Enb Address What
4 Breakpoint Keep Y 0x080483f2 in sum at gdbt.c:11
2) Conditional breakpoint.
The format of conditional breakpoints in GDB is:
b line number or function name if expression
(GDB) B 12
if i==2
Breakpoint 5 at 0x8048402:file gdbt.c, line 12.
(GDB) Info b
Num Type Disp Enb Address What
4 Breakpoint Keep Y 0x080483f2 in sum at gdbt.c:11
5 Breakpoint Keep Y 0x08048402 in sum at Gdbt.c:12
Stop only if i==2
3. GDB in Source view related commands
In GdB, you can view the source code to facilitate other operations
List < line number >|< function name >View the specified location code
file [file name]Load the specified file
Forward-searchForward search for regular expression source code
Reverse-searchBack-searching of regular expression source code
dir dirAdd the path dir to the beginning of the path of the source file search
Show DirectoriesDisplays the current search path for the source file
Info LineShow code loaded into GDB's memory
4. GDB view run data related commands
View running data in gdb refers to information about variables and expressions that can be viewed when the program is in the run or pause state
Printexpression | variable Viewer the value of the corresponding expression and variable when the program is run
x <n/f/u>View memory variable contents. where n is an integer representing the length of the displayed memory, and F represents the displayed format, u indicates the number of bytes to be requested from the current address
DisplayExpression settings in single-step or other cases, the content of the corresponding expression automatically displayed
BackTraceLook at the current stack, which means that the called functions have not returned
5. GDB Modify run parameters related commands
GDB can also modify the parameters at run time and keep the variable running as the user's current input value. It is set up in the following ways:
as you step through the process, type the command "Set variable = set Value"
Points to note when using GDB:
1. Be sure to include "-G" in the GCC compilation options.
2. You can view the value of a variable only if the code is in the run or pause state.
3. After setting a breakpoint, the program stops before the specified line.
Linux under Gcc.gdb Finishing