Home >Common Problem >How to use debug in c language
Simply speaking, there are two ways. One is source code debugging, which is to analyze the source code to find the bug location. Generally, printf() is used to print out the information of each step of program execution. The other is To debug executable files, you need to use a debugger.
1. Source code debug
is similar to the source code below. It mainly uses the information output when the program is executed to locate the location of the bug, and then modify it. Source code.
#include <stdio.h> void f() { ; } int main() { #ifdef _DEBUG printf("start main function!\n"); #endif void f(); #ifdef _DEBUG printf("leave main function !\n"); #endif return 0; }
Another case of using debug in C language
# 在代码中写入 #ifdef DEBUG #endif # 编译时用 gcc –DDEBUG –g –o *** ***.c 此时运行的结果是有debug信息的 ,gcc –o *** ***.c 无debug 信息 ,如 # include <stdio.h> int main () { int i=0 ; while (1) { printf ("hello world\t") ; i++ ; printf ("time=%d\n",i); #ifdef DEBUG if (i>10) break ; #endif } return 0 ; }
Open debug: gcc -DDEBUG -o debug debug.c
No need to debug (infinite loop): gcc - o debug debug.c
2. For executable file debugging, the commonly used debugging on the Windows platform is the debugging that comes with vs/vc. The other is the debugger windbg developed by Microsoft. gdb is commonly used on Linux platforms.
The debugger that comes with the IDE takes VC6.0 as an example. After writing the code, press F11 on the shortcut keyboard to enter debugging. At this time, right-click and select "go to disassembly" to view the program. disassembly code. Generally, in this case, it is mainly for learning disassembly of C language.
Windbg has many functions. It can debug source code, debug executable files, kernel debugging, and debug dump files. The more you use it, the more natural it will be. Familiar, to debug an executable file, just click "File", select "Open Executeable" in the pop-up dialog box, and then find the program you want to debug.
The Gdb debugger is commonly used in Linux. It is worth noting that to use gdb debugging, when using gcc or g to compile C/c files, you need to add the -g parameter. Only then can the symbol table be generated. The picture below is a screenshot of using gdb to analyze the distribution of variables in C. Let’s take a general look at what it looks like. You will become familiar with it after using it a lot, and you don’t need to learn it.
The above is the detailed content of How to use debug in c language. For more information, please follow other related articles on the PHP Chinese website!