Linux C + + program Debugging basics (GCC,G++,GDB,CGDB,DDD)

Source: Internet
Author: User


When writing a program, there are often some problems, such as some variables are not calculated as we expected, then we need to debug the program. This article mainly introduces the debugging tools that are debugged in the Linux/C + + system.

Write the program under Linux, C + + main compiler has GCC/G++,ICC, such as me, such as poor yards, most like GCC, very big reason is he free! Therefore, we take gcc/g++ as an example to introduce the main debugging tools.

The following sections are introduced:

1, the work before commissioning

2. Select the Debugging tool

3. Commissioning steps

Point me, please vote for me!

Work prior to commissioning

The compiler needs to produce code that can be debugged during the compilation phase to be debugged by the debugger. You can do the following:

Gcc-g-ggdb./yourcode.c-o Yourapp.exe

This compiles the code to produce symbolic information for GDB debugging.


Choosing a debugging Tool

GDB was the debugging tool we were going to use at the time, but he was using it in the command line and it was a bit inconvenient.


Cgdb and DDD are all based on GDB, and they just add an interface that's easier to interact with. Cgdb is also used in the command line, using the same way as GDB, just added the code display interface.


DDD is also developed by GNU, and he is based on GDB, but uses a visual interface that is easier to operate than cgdb.


In some cases, I may not have x-sever on Linux, so there is no graphical display, and we can only use the Debug tool with the command line, so gdb and cgdb are more widely used. In general, I use CGDB, and his operation is quite similar to GDB, so it's easy to get started.


Debugging

Use the following command to start the program.

Cgdb./youapp.exe

Note that here we are using CGDB, if you do not have CGDB installed, you should install it first! Ubuntu users are installed in the following ways:

sudo apt-get install Cgdb

After starting the software, several commonly used commands are familiar:

1, set the breakpoint command, the program executes when the breakpoint will stop, after the execution stop can be checked for variable values.

Break [Line_number]

For example: Break youcode.cpp:32, this command means to set a breakpoint at the 32nd line of code at Youcode.cpp, and the program will stop when it runs.


2, displays the value of the variable, the program can print the value of the variable when it stops at the breakpoint.

print [Var]

For example, print A, which represents the value of the print variable A. Print can also be abbreviated as p.


3, step execution, the program stops at the breakpoint, use the single-step debugging command to let the program step-by-step execution. This task can be accomplished using the next command, which can be abbreviated to n.


4, into the internal, in the debugging process, if you want to enter the inside of a function, you can use the Step command.


5. Continue to the next breakpoint. Use the Continue command to complete this task.


The above command is a common command for GDB debugging and a command that should be mastered. If you want to know more about how to use these commands, use the Help command to see the relevant assistance.





Related Article

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.