gcc/g++ Compiler and GDB debugger

Source: Internet
Author: User
Tags definition contains execution expression functions include valid gdb debugger

GCC and g++ are GNU C & C + + compiler gcc/g++ in a total of 4 steps to perform the compile work

1. Preprocessing, generating. I files [preprocessor CPP]
2. The preprocessed file will not be converted into assembly language, generating files. s[compiler Egcs]
3. Files with assembly into object code (machine code) generate. o file [assembler as]
4. Connect target code, generate executable program [linker LD]

[Detailed parameters]
-X language filename
Sets the language in which the file is used, invalidates the suffix name, and is valid for more than one later. That's the root.
According to the Convention C language suffix name is. c, and C + + suffix name is. C or. cpp, if
You are very personality, decide your C code file suffix name is. Pig Haha, then you need to use this
parameter, which works on the file names that follow, unless the next argument
of use.
Can I use any of the following parameters?
' C ', ' objective-c ', ' c-header ', ' C + + ', ' cpp-output ',
' Assembler ', and ' assembler-with-cpp '.
See English, you should be able to understand.
Example usage:
Gcc-x C Hello.pig

-X None filename
Turn off the last option, which means that GCC automatically recognizes the file type according to the filename suffix
Example usage:
Gcc-x c Hello.pig-x None hello2.c

-C
Only activate preprocessing, compilation, and assembly, that is, he only makes the program into obj files
Example usage:
Gcc-c hello.c
He will generate. O's obj file

-S
Activating preprocessing and compiling only means compiling the file into assembly code.
Example usage
Gcc-s hello.c
He will generate the assembly code for. S, and you can use a text editor to see

-E
Activate preprocessing only, this does not generate a file, you need to redirect it to an output file
Surface.
Example usage:
GCC-E hello.c > Pianoapan.txt
GCC-E hello.c | More
Look slowly, a Hello word also wants to work with 800 lines of code

-O
Set the target name, by default, GCC compiled the file is a.out, it is difficult to listen, if
You and I have the same feeling, get rid of it, haha
Example usage
Gcc-o hello.exe hello.c (oh, Windows used to)
Gcc-o hello.asm-s hello.c

-pipe
Using a pipeline instead of a temporary file in a compilation, there may be some questions when using non-GNU assembler tools
Problem
Gcc-pipe-o Hello.exe hello.c

-ansi
Turn off the features in GNU C that are incompatible with ANSI C and activate the proprietary features of ANSI C (including the prohibition of a
Some ASM inline typeof keywords, as well as preprocessing macros such as Unix,vax,

-fno-asm
This option implements part of the functionality of the ANSI option, which prohibits the use of asm,inline and typeof as
Key words.

-fno-strict-prototype
Only works on g++, using this option, g++ will not have an explicit function for functions that do not take arguments
The number and type description of the arguments, not without arguments.
and GCC regardless of whether or not to use this parameter, will have no parameters with the function, that the city does not explicitly say
Type of Ming

-fthis-is-varialble
is to the traditional C + +, you can use this when the general variable use.

-fcond-mismatch
Allow the second and third parameter types of the conditional expression to not match, and the value of the expression will be a void type

-funsigned-char
-fno-signed-char
-fsigned-char
-fno-unsigned-char
These four parameters are set to the char type, which determines that the char type is set to unsigned char (former
two parameters) or signed char (latter two parameters)

-include file
Contains a code that, in short, is a file that requires another file to be
With it, the function is equivalent to using #include<filename> in your code.
Example usage:
GCC Hello.c-include/root/pianopan.h

-imacros file
Expands the file's macro to the gcc/g++ input file, and the macro definition itself does not appear in the input file
In

-dmacro
Equivalent to the #define macro in C language

-dmacro=defn
Equivalent to the #define MACRO=DEFN in C language

-umacro
Equivalent to the #undef macro in C language

-undef
Cancel the definition of any nonstandard macro

-idir
When you are using #include "file", gcc/g++ will first look up your head in the current directory
File, if not found, he went back to the default header file directory to find, if using-I developed the directory, he
Go back to the directory you made and look for it in the usual order.
For the directory lookup that the #include<file>,gcc/g++ will make to-I, I can't find it, and then I will go to the department
The default header file directory lookup of the EC

-i-
is to cancel the function of the previous parameter, so it is generally used after-idir

-idirafter dir
Find failure in the directory of-I, and refer to this directory to find.

-iprefix Prefix
-iwithprefix dir
Generally used together, when-I directory lookup failed, will go to the Prefix+dir under Search

-nostdinc
Make the compiler no longer system default header file directory inside the change file, general and-I joint use, clear
Position of the qualifying header file

-nostdin C + +
The rule does not search in the standard path specified by g++, but still searches in other paths. This option creates
libg++ Library Use

-C
In the preprocessing, do not delete the annotation information, general and e-use, and sometimes analysis of the program, with this very
Convenient for

-M
Generates the information associated with the file. Contains all source code that the target file relies on
You can use gcc-m hello.c to test it, it's very simple.

-mm
Same as the one above, but it ignores dependencies caused by #include<file>.

-md
Same as-M, but the output will be imported into the. d file

-mmd
Same as-mm, but the output is imported into the. d file.

-wa,option
This option passes option to the assembler; if there is a comma between option, option is divided into multiple selections
Item and then pass it to the Assembly

-wl.option
This option passes option to the connector, and if there is a comma between option, option is split into multiple selections
Item, and then pass it to the connecting program.


-llibrary
Create a library to use when compiling
Example usage
Gcc-lcurses hello.c
Compiling programs using the Ncurses library

-ldir
When compiling, search the path of the library. such as your own library, you can use it to create a directory, or
The compiler will only look in directories in the standard library. This dir is the name of the directory.

-o0
-o1
-o2
-o3
4 levels of compiler optimization options,-o0 means no optimizations,-o1 defaults,-O3 optimization level most
High

-G
Just the compiler, when compiling, the production bar is information.

-gstabs
This option claims debugging information in the stabs format, but does not include GDB debugging information.

-gstabs+
This option claims debugging information in the stabs format and contains additional debugging information that is available only to GDB.

-ggdb
This option will generate the debug information that GDB can use as much as possible.
-static
This option disables the use of dynamic libraries, so the things that are compiled are generally very large and do not require anything
Dynamic Connection library, you can run.
-share
This option will use dynamic libraries as much as possible, so the resulting files are relatively small, but require the system to be run by dynamic libraries.
-traditional
Trying to get the compiler to support traditional C language features


The GNU debugger is called GDB, and the program is an interactive tool that works in character mode. In the X Window system, there is a gdb
Front-end graphics tool, called XXGDB. GDB is a powerful debugger that can accomplish the following debugging tasks:
* Set breakpoints;
* Monitor the value of the program variable;
* Procedures for single step implementation;
* Modify the value of the variable.
Before you can use the GDB debugger, you must compile the source file with the-G option. The cflags variable can be defined as follows in Makefile:
Cflags =-G
The following commands are typically used when running the GDB debugger:
GDB Progname

Type help at the GDB prompt to list the categories of commands, with the main categories:
* Aliases: Command alias
* Breakpoints: definition of breakpoint;
* Data: view;
* Files: Specify and view the file;
* Internals: Maintenance orders;
* Running: procedure implementation;
* Stack: Call stack view;
* Statu: Status View;
* TRACEPOINTS: Tracking program execution.
Type help followed by the command's taxonomy name to obtain a detailed list of the commands.

#DENO #
GDB's Common commands
Table 1-4 Common GDB commands
Command interpretation
Break NUM sets a breakpoint on the specified line.
BT shows all the call stack frames. This command can be used to display the order in which functions are called.
Clear deletes breakpoints that are set on a specific source file, on a specific line. The usage is: Clear filename:num.
Continue continues to execute the program being debugged. This command is used in a program because it handles a signal or breakpoint
Causes the runtime to stop.
Display EXPR Displays the value of an expression each time the program stops. An expression consists of a variable defined by a program.
The file file loads the specified executable file for debugging.
Help NAME displays assistance information for the specified command.
The info break displays the current list of breakpoints, including the number of times the breakpoint was reached.
Info files Displays the details of the file being debugged.
Info func Displays all the function names.
Info Local Displays the information about the locals in the function.
Info prog shows the execution status of the debugged program.
Info var displays all global and static variable names.
Kill terminates the program that is being debugged.
The list shows the source code snippet.
Make runs the Make tool without exiting GDB.
Next executes one line of source code forward, without stepping into other functions.
Print expr Displays the value of the expression expr.

1.8.5 GDB Usage Example
-----------------
List an error C source program bugging.c
-----------------
#include
#include

static char buff [256];
static char* string;
int main ()
{

printf ("Please input a string:");
Gets (string);

printf ("\nyour string is:%s\n", string);
}
-----------------
The above program is very simple, the purpose is to accept user input, and then the user's input to print out. The program uses an initial
The string address string that was initialized, so after compiling and running, a Segment Fault error occurs:
$ gcc-o test-g test.c
$./test
Please input a STRING:ASFD
Segmentation fault (core dumped)
To find out what's wrong with the program, we take advantage of GDB and follow the steps below:
1. Run the GDB bugging command to load the bugging executable file;
2. Execute the loaded bugging command;
3. Use the where command to see where the program went wrong;
4. Use the list command to view code near the call gets function;
5. The only factor that can cause an error in the gets function is the variable string. View the value of a string with the print command;
6. In GdB, we can directly modify the value of the variable, as long as the string to a valid pointer value on it, we
Set breakpoints at line 11;
7. The program re-run to stop at line 11th, at which point we can use the Set variable command to modify string values;
8. Then continue running and you will see the results of the correct program running.

#DEMO #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.