Home > Development Tools > VSCode > How to run and debug code in VScode? Tutorial sharing

How to run and debug code in VScode? Tutorial sharing

青灯夜游
Release: 2022-12-20 22:14:17
forward
6293 people have browsed it

How to adjust VSCode in Linux environment? The following article will share with you the VScode debugging tutorial and introduce the setting methods of tasks.json and launch.json. I hope it will be helpful to everyone!

How to run and debug code in VScode? Tutorial sharing

Running environment:

VSCode 1.68.1

wsl: ubuntu subsystem

How to run and debug code in VScode? Tutorial sharing

Without further ado, let’s start directly. First, select the fourth option on the left taskbar. [Recommended learning: vscode tutorial, Programming teaching]Running and debugging, click to create launch.json

How to run and debug code in VScode? Tutorial sharing

The created interface is as shown in the picture above. Click Add Configuration in the lower right corner
How to run and debug code in VScode? Tutorial sharing

As shown in the picture above, select the firstc/c (gdb) to start

How to run and debug code in VScode? Tutorial sharing

At this time, the code shown in the picture above will be generated. Pay attention to the two places where I drew the arrows. The cwd is the working directory where our current file is located. Change the two places where I drew the arrows to be the same.

How to run and debug code in VScode? Tutorial sharing

After the modification, it is as shown in the picture above. The a.out behind is the name of the executable file we will debug later. You can also use the one generated by the system here. Change it to your own That's fine, next we start configuring tasks.json

How to run and debug code in VScode? Tutorial sharing

First go back to main.cpp, then click on the terminal in the menu bar above and select the last to configure the default Generate task

How to run and debug code in VScode? Tutorial sharing

#At this time, as shown above, select the second c/c :g to generate the active file

How to run and debug code in VScode? Tutorial sharing

As shown in the figure above, tasks.json is generated. Then we open launch.json to make a comparison. We first add a line of code "preLaunchTask" after launch.json. This line of code It means the name of the task that is run before launch. This name must be consistent with the task name in tasks.json. As shown in the figure below, the label in tasks.json must be exactly the same as the preLaunchTask in launch.json, because during execution Before launch.json, the system will first execute the contents of tasks.json based on this line of code. It can be simply understood that the code in tasks.json will help us compile and generate an executable file using g/gcc, and the code in launch.json It is to let the system debug our executable file.

How to run and debug code in VScode? Tutorial sharing

Next, you will see the file in tasks.json, as shown in the figure below. The command configuration specifies the compiler, usually gcc or g compiler, and then The following args are the compilation options after the compiler. Note that -g means compiling an executable file with debugging information. If this -g is missing, the generated executable file cannot be debugged. The following main.cpp and swap.cpp is the name of the file to be compiled, the -0 parameter specifies the name of the generated executable file, the next line is the location of the generated executable file in the current working directory, the name is a.out

How to run and debug code in VScode? Tutorial sharing

As shown in the figure below, everyone pays attention to the arrow part in the article. The two names must be the same. The one on the left is the generated executable file called a.out, and the one on the right is the debugging file called a. out, the name can be changed by yourself

So tasks.json is actually equivalent to helping us complete the operationg -g main.cpp swap.cpp -o a.out

How to run and debug code in VScode? Tutorial sharing

After configuring the above files, go back to main.cpp to set your own breakpoint, and then press the F5 key to debug. Success

How to run and debug code in VScode? Tutorial sharing

For more knowledge about VSCode, please visit: vscode Basic Tutorial!

The above is the detailed content of How to run and debug code in VScode? Tutorial sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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