Home >Development Tools >VSCode >How to run and debug code in VScode? Tutorial sharing
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!
Running environment:
VSCode 1.68.1
wsl: ubuntu subsystem
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
The created interface is as shown in the picture above. Click Add Configuration in the lower right corner
As shown in the picture above, select the firstc/c (gdb) to start
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.
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
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
#At this time, as shown above, select the second c/c :g to generate the active file
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.
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
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
After configuring the above files, go back to main.cpp to set your own breakpoint, and then press the F5 key to debug. Success
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!