Home > Common Problem > body text

How to use debug in c language

anonymity
Release: 2019-04-25 10:14:15
Original
13492 people have browsed it

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

How to use debug in c language

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;
}
Copy after login

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 ; 
 }
Copy after login

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.

How to use debug in 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.

How to use debug in c language

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.

How to use debug in c language

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template