The use of C development--gcc,gdb under Linux __linux

Source: Internet
Author: User

Linux under C Development- gcc , gdb usage

Author: zccst

The process of learning should be a process of accumulation, there is no, and less to much, but not like the monkeys down the mountain. September is the first time system learning gcc, GDB, makefile (see previous blog). Now as an integral part of the embedded system, the decision to learn again, feel a bit more understanding deepened.

GCC compiler

The use of the prerequisite editing tools: VI vim Emacs and so on.

Theory Chapter

GCC allows programmers to have the flexibility to control the compilation process. The compilation process can generally be divided into the following four phases, each of which invokes different tools for processing, as shown in Figure 9-18.

There are two formats for executable files in a Linux system. The first format is the a.out format, which is used in early Linux systems and in the original format of Unix systems. A.out from the default executable file name of the Unix C compiler. When you use a shared library, the a.out format can cause problems. Adapting the a.out format to a shared library is a very complex operation, and for this reason a new file format is introduced into the fourth version of Unix System 5 and the Solaris system. It is called an executable and connected format (ELF). This format is easy to implement shared libraries.

The ELF format has been used as a standard format by Linux systems. All binaries produced by GCC compilers are files in the ELF format (even if the default name of the executable file is still a.out). Older a.out-formatted programs can still run on systems that support ELF format.

Note: GCC supports a number of debugging and profiling options. The most common of these options are the-G and-PG options.

Practice Article

Usage format for gcc: gcc [options][filenames]

Where filenames is the program source file that you want to compile. Options see the main parameters of GCC below.

When GCC is used, GCC completes preprocessing, compilation, compilation, and connectivity. The first three steps generate the target file, when connected, link the generated target file into an executable file. GCC can be used to support different source program files for different processing, file format with the file suffix to identify.

VI hello.c

First, common steps:

For a simple program that has only one source file, it is often only compiled and run in two steps.

1, gcc hello.c-o hello

2./hello

Second, the GCC compilation process

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

HELLO.C (source code)

1, hello.i to generate preprocessing files,

The parameter is "-E" and the hello.c-> hello.i. Full command for GCC hello.c-o hello.i-e

2, Hello.s compiles the assembly file,

The parameter is "-S", the hello.i-> Hello.s. Full command for GCC hello.i-o hello.s-s

3, hello.o the assembly file into the target code,

The parameter is "-C", the Hello.s-> hello.o. Full command for GCC hello.s-o hello.o-c

4, Hello link target code, generate executable program,

Parameter none, put hello.o-> hello. Full command for GCC hello.o-o hello

./hello (Run)

Iii. main parameters of GCC

1, Overall parameters

-e precompilation only, no other processing

-S just compiles not assembler, generates assembly code

-C just compiles not linked, generates target file ". O"

-o file outputs the output file into file

-G contains standard debugging information in an executable program

-V Print command line information and compiler versions of the compiler's internal compilation process

-I add dir directory to the search path list of header files

-L dir add dir directory to search path list of library files

-static Link Static Library

-llibrary a library file with a connection named library

2, warning and error parameters.

-W Shutdown warning

-ANSI displays warning messages that do not conform to ANSI C standard syntax

-pedantic

-wall track debugging of powerful tools, and finally develop the habit of using this parameter.

3, Find options

GCC generally uses the default path to find header and library files. If the files are using a header file or library file that is not in the default directory, compile-time to specify their lookup path.

-I option: Specifies the search directory for header files

Example: Gcc–i/export/home/st–o test1 test1.c

-L Option: Specifies the search directory for the library file

Example: Gcc–l/usr/x11/r6/lib–o test1 test1.c

4, optimize the parameters.

The parameter "-on" is used to generate optimized code. where n is an integer representing the optimization level, a typical range from 0 to 2 or 3. The larger the number the higher the level of optimization, the faster the program runs. Commonly used-o2, because it in the optimization of length, compile time and code between the university to achieve a more ideal balance point. Comparison: 1-8.c (code slightly)

GCC 1-8.c-o 1-8

Time./1-8

GCC 1-8.c-o 1-8-o2

Time./1-8

Note: Optimization code should be avoided in the following situations.

(1) When the program is developed. The final generated code is considered optimized only at the end of software release or development.

(2) Limited resources. such as when memory resources are very tight (some real-time embedded devices).

(3) Tracking debugging. Optimization may remove, overwrite, or reorganize code, making tracing debugging extremely difficult.

gdb Debugger

Theory Chapter

GDB debugging is not a. C source file But an executable file, however, not all executables can be debugged with GDB. If you want the resulting executable file to be used for debugging, add the-G parameter when executing the GCC instruction compiler, specifying that the program contains debugging information at compile time. The debug information contains the type of each variable in the program and the address mapping in the executable file and the line number of the source code. GDB uses this information to associate the source code with machine code.

Practice Article

GDB use format: GDB filename

where filename is the executable file to debug. Running GDB in this way directly specifies the program you want to debug. This is exactly the same as executing the file filename (the file command: loading the executable that you want to debug) after you start GDB. You can also use GDB to check for a core file that was generated by an abnormally terminated program, or to connect to a running program.

GDB supports a number of commands and implements different functions, ranging from simple files to complex commands that allow you to check the contents of the stack being called.

1, edit the source file.

For example, VI 1-9

Add the following content

#include <stdio.h>

int min (int x, int y);

int main ()

{

int A1, a2, Min_int;

printf ("Please input the frist int number:/n");

scanf ("%d", &a1);

printf ("Please input the second int number:/n");

scanf ("%d", &A2);

Min_int = min (a1, a2);

printf ("The min number is:%d/n", min_int);

return 0;

}

int min (int x, int y)

{

if (x < y)

return x;

Else

return y;

}

2, compile with the option "-G", so that the compiled executable code contains debugging information.

GCC 1-9.c-o 1-9-G

3, into the GDB debugging environment.

GDB 1-9

Enter the GDB debug mode. In the GDB debugging environment, the prompt is "(gdb)."

4, debug the program with GDB.

(1) View source files

Syntax: ' l ' is the list abbreviation. list< line number >|< function name >. View source code, show 10 lines at a time

Command (GDB) l

(2) Set breakpoints

Syntax: Break line number | Function name < conditional expression >

This example allows you to enter a command

(gdb) b min Sets a breakpoint in the custom min function.

(GDB) b 17 function ditto

(3) View breakpoint information

Syntax: info Break # Info command is to display XX information, often have files,func,local,proc.

Command (GDB) Info b

(4) Running the program

Syntax: Run # Executes the program currently being debugged

Command (GDB) r

Note: GDB runs from the first line by default, and if you want to start running from the specified line in the occurrence, simply type "R" + line number.

(5) View variable values

Syntax: P variable name. When a program runs to a breakpoint, it is automatically paused, at which point the value of the specified variable can be viewed.

This example commands

(GDB) P A1

(GDB) P A2

(GDB) P Min_int

When debugging a program, if you want to modify the value of the variable, you can run to the breakpoint at the point where you enter the "Set variable = set value."

For example, assign a value of 11 to the variable "A2" and enter "set a2=11".

(6) Single Step debugging

Grammar:

"N" (next), if there is a function, does not enter the function call.

"S" (step), if there is a function, then enter the function call.

(7) Continue to run the program

Syntax: Continue

Command (GDB) c

(8) Exit the GDB debugging environment.

Syntax: Quit

Command (GDB) q

Other common commands are:

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.