Debugging C++ Program in Visual Studio Code (VSCode)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so let's create a very basic C plus plus file I'm going to name my file as main dot CPP here and then press enter which is going to create this file now as soon as I create the C plus file Visual Studio code is going to recognize that and it's going to show me some recommendations you can see do you want to install recommended extension for C plus plus so here you can click on this option don't click on install but because we want to see which extensions we are going to install so I am going to click on show recommendations here and here it is going to open this extensions tab right so on the left hand side you have this bar and when you just hover over this extensions icon here you will be able to see that it's for extension so just click on this and then you will be able to ah see many extensions for C and C plus plus right so right now the recommended extension by Visual Studio code for us is this one which is created by Microsoft you can see and the number of downloads are also given here so around 3.3 million downloads are done for this extension so if you ah do not see this recommendation you can always go to this extensions tab here and then search for C plus plus and once you search for C plus plus it's going to show you all these recommendations right so the first the top most option is going to be the option which you will require so you can see this one and it's from Microsoft once again and for this extension you can see the number of downloads are 27 million and what it does it provides the support for C and C plus plus intellisense debugging and code browsing so we are going to install the C slash C plus plus extension first of all and just click on the install button which is going to start the installation process of this extension same we are going to do for this one which is called C slash C plus plus extension pack so just choose this extension also which is called C slash C plus plus extension pack and then click on install which is going to install nine essential C and C plus plus related packs right so we have already installed the CNC plus plus intellisense debugging and code browsing extension in addition it's going to install all these extensions so that we can run our C or C plus plus code on Visual Studio code editor right so if you want to use C make or make or any other kind of tool to build your C plus plus program this extension is going to install all the related extensions for that so you can see all the extensions in this pack are installed and now you will be able to see disable and uninstall option and let's write some code so first of all we are going to include this IO stream package so I'm going to just write include and you can see at every point you will get the intellisense to complete your code so when I just type this angle bracket it shows me all the options and then when I just type IO I can see all these packages which starts from i o right I need this i o stream so I'm going to select this and it's going to just add the ending angle bracket also then I'm going to create the main method which is simple int Main and then these parentheses and then these curly brackets here I just want to let's say print hello world so I'm going to just write STD colon colon c out and then I want to print hello world so once you have written your code you can save your code by just pressing Ctrl s which is going to save your code and then you are ready to run your code so how you can run your code on your Visual Studio code editor so in order to run your code you have the option to open the terminal and run your code there or you can just select this terminal option and you have this run task run build task so I'm going to select this option which says run build task you can also press Ctrl shift B for running this build task so let me just click on this and as soon as I click on this option I will see all these options here so for building my C plus plus program I will require G plus plus dot exe file right if you need to compile the C program so if this file name was main.c file and you wanted to compile the C program you could have chosen GCC dot exe right so for C plus plus we use G plus plus and for C programs we use GCC so in my case I am going to use this option which says G plus plus exe build active file and what it is going to do is going to build my file and you can see build finished successfully so my build was finished successfully and you can see this main dot exe file is generated so now I just need to run this exe file and see what is the output right so for this I can open a new terminal you can open the new terminal by just selecting this terminal option and then click on new terminal or you can click on this plus button which is going to open the new terminal now as you can see the type of terminal which is opened right so by default because Powershell is the default terminal on my windows 11 operating system so it has opened Powershell but let's say I want to open the normal command prompt right I can open it by just clicking on this arrow button and then I can select command prompt from here and it's going to open the command prompt you can see which type of terminal or command prompt is open on the right hand side in this list so I have this Powershell terminal open I have this command prompt terminal open and it's similar to opening your CMD on your Windows operating system or Powershell on your Windows operating system so you can see this Windows Powershell so it's similar to opening these Powershell or command prompt on you your windows 11 operating system they are just opened in your Visual Studio code so they come integrated with your Visual Studio code from here I select CMD option so I want to run my exe file using command prompt so I can just type the name of my exe file which is main dot exe and then press enter which is going to print the output here which is Hello World in my case right so that's how you can run your program using Powershell so if you want to use Powershell so for the Powershell you just need to write Dot and then the backslash and the name of your exe which is main dot exe file and then press enter which is also going to show you the hello world result so you can run your exe file using command prompt also and from the Powershell also now let me add some more code and I will show you how you can debug your C plus program on Visual Studio code so very simple nothing complicated about this program I just want to show how you can debug your C plus code so in order to debug your C plus plus code you uh select this option which is called run and debug and here you can see a few options so you can click on run and debug button here which is going to run your program in a debugger you can also create a launch.json file to customize your debugging options right so for now what I am going to do I am going to just add a few break points here and you will see the list of all the breakpoints in this list under breakpoint so so you will be able to see the list of all the breakpoints you can see it's in the main.cpp file and then they are at line four five and six right you can disable all the breakpoints by just pressing this button which says toggle active breakpoints and it's going to disable all these breakpoints it's not going to remove the breakpoints but these breakpoints will be disabled you can also remove all these breakpoints by just pressing this option which says remove all breakpoints and it's going to remove all the breakpoints so let me add these breakpoints once again and let's run this debugger so I'm going to click on run and debug button and once again Visual Studio code is going to ask us which debugger we want to use right so depending upon the debugger available on your windows 11 operating system you can choose from those debuggers because we have installed the Min GW as a compiler on our Windows operating system we are going to choose the first option which says C plus plus GDB lldb okay so I am going to select this option and this is going to give me once again these options right so now as I said for C plus plus programs you choose the G plus plus dot exe file and for C programs you can choose GCC dot exe files right so I'm going to select the G plus plus dot exe file for C plus plus and it's going to run my program in the debugger and you can see it's starting the debugger here and now my program is running in the debug mode so how can I understand that my program is in debug mode you will be able to see this specific color for debugging right so this bar color is changed uh for the debugging and you will be able to see all these options for debugging so from this option you can continue to uh the next code and then using this you can step over in the code and then you can step into the code and then step out of the code and then you can rerun your debugging once again and you can stop your debugging using this icon now because we have added this breakpoint our program execution is stopped at this line 4 which is our first break point and on the left hand side you will be able to see the list of all variables and the values for these variables so right now the variable 1 have 0 value variable to have 16 and variable sum has zero value because at this point these numbers are not assigned to our variables C plus plus is going to assign the random numbers to these variables right that's why you see all these random numbers now when I just click on step over and move to the next breakpoint what happens here you can see num1 is equal to 5 here right I can also watch for some specific variables let's say I want to watch the variable called sum which is this variable I can type the name of the variable and then press enter and then I can watch that variable specifically right so now the value of num1 is 5 because this line is executed but still this line is not executed so still num2 has been assigned any random number in our in my case it's 16 but it can be any number because we haven't assigned 10 here because we are on this line when we go to the next line by clicking on step over you can see the number 10 is assigned to num2 variable right but still the value of sum is 0 because at this point our program execution is stopped at this point so still the assignment is not ah has not been taken place so when I step over to the next line I can see the sum value which is 15 and you can see I was watching this variable also so this variable value is also 15 right so this is how you can step over and step into a method or step out of the method using these buttons and you can keep the track of all these local and Global variables on this section and then you also have the call stack so right now we are in the thread one which is our main thread if we were using multiple threads you will be able to see in which thread what is happening here in the call stack right so this is how you can debug your code you can stop this code by clicking on this stop icon here and you will come out of your debugger right and you can see the output of your debugger in this debug console right so you will be able to see where your program execution is where you have stopped in your program so we we stopped at break point three break point four so once I have chosen my debugger I can and next time I can always use this option from here from this list and then I can run the debugger okay again once you select all these options which I have shown you and when you go to the Explorer section Visual Studio code is going to create this dot vs code file and here this launch dot Json file is important where all the options which you have chosen for debugging are saved so here in this launch.json I can see the configuration the name of the configuration is G plus plus dot exe which is the same name here right and you can provide multiple configurations in this launch file that's why you can give the name to every configuration the type is CPP d b g for CPP debugger request is launch and then which file you want to launch this means we are in the current directory and the file name is the same name as your CPP file name dot exe file and you can see which debugger you you are using GDB and then you have the debugger path so in our case this debugger path is at this location so all the information about your debugger is there in your launch.json file so you can always change this configuration if you want to use some other debugger right let's say Microsoft debugger you can change this path for the debugger from here and then use the other debugger right you also have this tasks dot Json file where you will be able to see uh the commands you can see the command which is going to be used to run your program c g plus plus dot exe file is there and then you will be able to see the options for compiling or building your C plus code here so let's say I want to run my debugger once again so from the next time I can just click on this green start debugging button and it's going to start my code in the debugging mode so now once again you can see I am in the debugging mode so this is how you can set up visual studio code
Info
Channel: ProgrammingKnowledge
Views: 52,513
Rating: undefined out of 5
Keywords: how to debug c++ in visual studio code, debug c++ code online, debugging in c++ programming examples, how to debug c++ in visual studio code mac, launch.json vscode c++, vscode debug c++ linux, c++ debugger linux, how to debug in visual studio code c#
Id: 2VokW_Jt0oM
Channel Id: undefined
Length: 17min 23sec (1043 seconds)
Published: Mon Apr 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.