Linux C/C ++ program debugging BASICS (GCC, G ++, GDB, CGDB, DDD), gdbcgdb
When writing a program, we often encounter some problems. For example, if some variable calculation results are not as expected, we need to debug the program. This document describes the main debugging tools used to debug C/C ++ in Linux.
Write programs in Linux. Major C/C ++ compilers include GCC/G ++, ICC, etc. For example, I am a programmer and I like GCC most, the major reason is that he is free! Therefore, we use GCC/G ++ as an example to introduce the main debugging tools.
The following content is introduced:
1. Work before debugging
2. Select a debugging tool
3. debugging steps
Click me. please vote for me!
Work before debugging
The compiler must generate code for debugging in the compilation phase before it can be debugged by the debugger. You can do the following:
gcc -g -ggdb ./yourcode.c -o yourapp.exe
In this way, code compilation can generate symbolic information for gdb debugging.
Select debugging tool
The debugging tool we used at the time of gdb, but it was not convenient to use in command line.
CGDB and DDD are both developed based on GDB. They only add interfaces that are easier to interact. CGDB is also used in the command line. It is used in the same way as GDB, but the code display interface is added.
DDD is also developed by GNU. It is based on GDB and only uses a visual interface, which is easier to operate than CGDB.
In some cases, X-Sever may not be installed on Linux, so there is no graphic display function. At this time, we can only use the debugging tool through the command line, therefore, gdb and cgdb are more widely used. Generally, I use CGDB, which is similar to gdb and is easy to use.
Debugging
Run the following command to start the program.
cgdb ./youapp.exe
Note that cgdb is used here. If you have not installed cgdb, you should first install it! The Installation Method for ubuntu users is as follows:
sudo apt-get install cgdb
After the software is started, several frequently-used commands should be mastered:
1. Set the breakpoint command. When a program encounters a breakpoint, it stops. After the breakpoint is executed, you can check the variable value.
break [line_number]
For example: break youcode. cpp: 32. This command sets a breakpoint at youcode. cpp line 32nd and stops running the program until now.
2. display the value of the variable. When the program stops at the breakpoint, it can print the value of the variable.
print [var]
For example, print a, which indicates printing the value of variable. Print can also be abbreviated as p.
3. One-step execution. After the program stops at the breakpoint, use the single-step debugging command to let the program be executed step by step. Use the next command to complete this task, which can be abbreviated as n.
4. Enter the internal environment. If you want to enter the internal environment of a function during debugging, you can use the step command.
5. continue to the next breakpoint. You can use the continue command to complete this task.
The preceding commands are commonly used for gdb debugging and must be mastered. If you want to know more about how to use these commands, use help command to view related help.
In linux, you can use gdb to debug c Programs. When you run the run Command, the system prompts that the permission is insufficient. How can this problem be solved?
Is root permission required? Try debugging with management permissions.
In linux, how does one compile C Programs to make them executable files? How to debug?
Compilation: gcc-o xxx. c
Debugging: gcc-o xxx-g xxx. c
Gdb xxx
Run
Break line (number of rows) or function name
Info view breakpoint
N next step
Print + variable print variable value
Q exit debugging
C. continue to the next breakpoint.