How to debug C + + programs with GDB under Linux

Source: Internet
Author: User

Original address: http://blog.csdn.net/wfdtxz/article/details/7368357

GDB is a powerful UNIX program debugging tool released by the GNU Open source organization. Perhaps, you prefer that graphical interface, such as VC, BCB and other IDE debugging, but if you are in the UNIX platform to do software, you will find GDB this debugging tool than VC, BCB graphical debugger more powerful features. The so-called "inch, the ruler is short" is this truth. In general, GDB is the main help you complete the following four aspects of the function:

    1. Start your program and you can run the program as you wish, in accordance with your custom requirements.
    2. Allows the program to be debugged to stop at the breakpoint you have specified for the reset. (Breakpoint can be a conditional expression)
    3. When the program is stopped, you can check what happens in your program at this time.
    4. Dynamically change the execution environment of your program.

From the above, GDB and the General debugging tool is not the same, basically is to complete these functions, but in the details, you will find GDB this debugging tool is powerful, you may be more accustomed to the graphical debugging tools, but sometimes, the command line debugging tools have a graphical tool can not be done. Let's see.

GDB Basic Commands list:

Instance:

1 Create a new source file VI swap.cc

The contents of the source file are as follows:

#include <iostream>
using namespace Std;
void swap (int &a,int &b)
{
int tmp;
Tmp=a;
A=b;
b=tmp;
}

int main ()
{
int i,j;
cout<<endl<< "Input" int number: "<<endl;
cin>>i>>j;
cout<< "before Swap (), i=" <<i<< "j=" <<j<<endl;
Swap (I,J);
cout<< "after swap (), i=" <<i<< "j=" <<j<<endl<<endl;
return 0;
}

Direct copy-paste build source file

2. Generate executable file g++-g-o swap swap.cc, Note that you must use the- g parameter, the compilation will add debugging information, or you cannot debug the execution file

3. Start Debugging gdb swap

3.1 View Source file List 1, enter repeat last instruction

3.2 Set Debug breakpoint Break 16, set breakpoint on line 16th,Info Break View breakpoint information (also use abbreviation i b)

3.3 Commissioning Run input run or R

3.3 Single-Step debugging,step or S into function interior

3.4 View variable print B or P b

3.5 Viewing function Stacks bt, Exit Function finish

3.6 Continue running until the next breakpoint or main function ends continue or C

3.7 Exit Debug Input Q

How to debug C + + programs with GDB under Linux

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.