How to set up Visual Studio Code for Executing and Debugging C++ Programs | Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the channel in this tutorial we will see how we can set up the visual studio code for writing executing and debugging c plus plus programs in order to continue with this video you need to download and install visual studio code and you need to download and install a tool set for c plus plus and we can install mingw64 in windows to get the gccg plus plus installed and also we need a debugger if you don't have all these programs then i have the tutorials i'm gonna put the link in the description box you guys can check that out and install those programs now before continuing with this video the first thing that we do is we will make sure that we have all the necessary programs installed in the computer so click on the start button type in cmd and open up the command prompt and in the command prompt type in g plus plus space minus minus version and you should get some version number displayed in here if you get an error message stating that g plus plus is not a recognized command or something like that then it means that the c plus plus tool set is not installed you need to install it once you get some version number displayed in here like this then it means you are good to go the next thing that we're gonna do here is we're gonna check the version of gdb that is the debugger installed in the computer so type in gdb space minus minus version and hit the enter button and again you will get some version number displayed if you get an error message here then it means you need to install this program the link for the tutorial is available in the description box okay now once we have these two programs available in the computer we can close the command prompt and we can open up the visual studio code so here i'm going to open up the visual studio code and the first thing that we do here is we will set up a workspace for writing and executing the c plus programs so i'm gonna go to file i'm gonna say add folder to workspace and here i'm gonna save the files and folders in the desktop if you want you guys can store it in any other location and here i'm gonna create a new folder so right click then new folder i'm gonna say c plus plus programming and after that we will open up this folder and here inside this folder we will create another folder new folder and i'm gonna say programs and after that we will select this folder and we will click on this add button now if we look at the explorer in here it is a first icon which says explorer when we hover on it you know we can see untitled workspace and we can see the folder that we have just added now the next thing that we do is we will go to file and we will say save workspace as and this time again i'm gonna go to desktop i'm gonna open up the c plus plus programming folder that i have created before and here i'm gonna save my workspace i'm gonna say cpp workspace you guys can give any name after that click on save and now we have the workspace and we have the folder now the next thing that we do is we will create a simple program now i'm going to select the programs folder which is available in this explorer and i'm gonna click on new folder so what i'm gonna do is inside this program folders whenever i create a program you know i'm gonna place them in separate folders okay so here now i'm gonna create a new folder i'm gonna say hello world click outside and the hello world folder is created now inside this folder we will create the file so select the hello world folder click on new file icon and here i'm gonna say hello world dot cpp dot cpp is the extension for the c plus plus files hit the enter button helloworld.cpp file is created and it has been opened here in the editor now i'm going to close this welcome window welcome tab and here now what we can do is we can write a simple c plus program it is a very simple program so i'm gonna forward this video okay now i have created a very simple program in this program we have defined a variable called as number and we have initialized it with the value of 0 and then after that we have changed the value of this number variable and then we are displaying that here and then we are displaying goodbye a very simple program now i'm just wondering why i gave the name hello world because you know there is nothing about hello world in this program but you know we will work with this okay now the next thing that we're gonna do here is we will install an extension so what we do is we will click on this extensions icon and here in this search bar we will search c c plus plus and it will display all the extensions matching for this search query and here the first extension is c c plus plus and it provides intelligence debugging and code browsing feature for cnc plus plus so we can install this extension and it has been developed by microsoft so we will install it and in the bottom right corner you guys can see the installation happening you have to wait for it to finish completely now the extension is installed and also you guys can see the tutorials for a particular compiler and platform so all these links are available in here now what we do is we will close this extension tab and also we will go back to the explorer now one thing that i forgot to mention and that is after writing the program make sure that you save the program and also you guys can go to file and make sure that autosave is selected so that whenever we make any modification to the program it will be saved automatically okay now the next thing that we're gonna do is we're gonna make sure that some specific c plus plus properties are used for all the programs that we're gonna be writing in this workspace to do that we will click on view we will click on command palette and here we will type in c slash c plus plus colon edit and we get two options one is edit configuration by using the user interface edit configurations using the json file now you can directly edit on the json file or you guys can use the user interface to do the same thing we're going to click on this user interface option edit configuration ui now once i click on that if you look at the programs folder then we have a new folder created called as dot vs code if i open up this folder now then we can see ccppproperties.json a new file is created this file will contain whatever the configurations that we specify for this workspace okay now here in this tab we can see the settings that we can make we are not going to make many changes but if you scroll down then you guys can see c plus plus standard uh gunu plus plus 14 is selected click on that and make sure that gunu plus plus 20 is selected whatever the latest standard is available select that one now after making that modification we will close this tab and we will come back to the helloworld.cpp file and now what we're gonna do is we're gonna close the visual studio code and we will open it up again close it open it up now here if you look at this program then i'm getting some error and the reason for that is i misspelled namespace so i have to make sure that you know i write the program properly now after correcting the mistake that i have made in this program the next thing that we have to do is we have to build an executable file from this source file and we have to run that executable file in order to see the output so here what we have to do is we have to set up a build task for generating the executable file for this program and we can do that by clicking on the terminal clicking on configure default build task now as the option says here whatever the build task that we're going to configure now it will be the default build task for the c plus plus programs okay we will click on it once we click on that it will open up the command palette and it will display some options now here it will display the compiler options now what we do is we will use the gunu c plus plus compiler which is the g plus plus so here we will select c c plus plus colon g plus plus dot exe so we will click on that option and once we do that in this dot vs code folder a new file is created called as tasks dot json and if you look at the contents of this file then we can see the type which is cpp build and also the label or the name of the task and then we can see the command used and also we can see the arguments passed for that command here if you want to make any modifications then you can make that uh here i'm gonna make a couple of modifications one is in this arcs section i'm gonna say minus g3 minus g is used for generating the default debug information minus g3 will make sure that the executable file generated will contain the maximum debug information after that i'm gonna add one argument and that is for displaying the warnings in the program for example in our program let us say we will create some variables and if we don't use it then the compiler will generate the warning for us and to specify that we will write in minus capital w a l minus wall and after that we will separate these arguments by using comma just like the way i'm writing in here okay after that save this uh file you know you can press the control s now after this i don't wanna make any other modification i'm going to close this and now what we can do is we can use the task that we have configured and we can you know generate the executable file but before that i will close the visual studio code and i will open it up again you know just to make sure that whatever the changes that we have made for these files the changes are applied okay now we will generate the executable file by using the task that we have created so here make sure that you are in the helloworld.cpp file and click on the terminal and click on run build task which will run the default task that we have configured previously otherwise if you want to have options of selecting the task then you guys can you know click on the run task which will display the different tasks that you have you know set here i'm going to click on run build task now in the terminal it says starting the build and it says build finished successfully and if you look at the hello world folder then we have an executable file here called as helloworld.exe now what we can do is we can run this executable file and we can get the output of this program so in order to run this executable file we need a terminal so i'm going to click on this plus in here which will provide us a new terminal and here if we look at the path then we are in this programs folder but the executable file that we want to run which is helloworld.exe is actually present inside a folder called as hello world so what we do is we will go into the hello world folder which is present inside programs so we will type in cd space and then hello world with this cd command you don't have to use the double quotes if the name contains space but i'm just using it and after that hit the enter button and now we are in this hello world folder and we can run this helloworld.exe now so we will type in the name of the file which is hello space world dot exe and this time since the name contains space we have to enclose that in double quotes otherwise you will get the message stating that hello is not a valid program or something like that okay after that hit the enter button it says number is 30 goodbye that is the output of our program now here what we are doing is we are using the task and we are building the executable file then we are opening up the terminal we are writing this command in order to see the output if you want to see the output as quickly as possible then you guys can use another method that i have mentioned in a separate video where we will use an extension called as code runner you have the option of clicking on a button it will build the program and it will run it for you now here if you don't want to use the extension and you want to know how we can set up a task for building the executable file and running that executable file at one try then we're gonna see that and for checking out you know how we can debug this program this step or this process that i'm doing at this moment is not required i just wanted to show you guys how you can set up a task you know which will build the executable file and it will run it for you in just one go so here what we do is we will uh open up the task.json which is present in the dot vs code folder and we can see the tasks and whatever the content that we have between these curly brace as i have selected here it is actually the content of a particular task so what we do is we will copy this task our task contents and then we will go to the end of this task after the closing curly paste we will add a comma we will hit the enter button we will go to the new line and then we will paste the content that we have copied in other words what we are doing is we are just duplicating the task which is created for us by the extension okay now what we do is we will make some modifications and uh here the first thing that we do is we will change the type and you know this is the second task that we have you know the duplicate code that we have so i'm gonna remove this type and i'm gonna specify shell after that we can change the label for example i can say uh run c plus plus program that is the name of the task and also we can specify the compiler used which is uh gunu c plus plus g plus plus okay after that uh we will not change the command value here but what we do is whatever the arguments that we have specified in here we will specify that along with the command so what we do we will copy this for example minus g3 and we will come to the end of this command add a space and we will paste that again we will add a space we will come back to this argument section copy the next argument we will come here back to command we will paste it similarly we will do it so i'm gonna copy this file i'm gonna paste it in here in all these arguments we will specify that along with the command instead of this args okay and we will separate them by using a space which is very important so i'm gonna copy minus o paste it in here add a space then i'm gonna copy this last argument here i'm gonna paste it now for the time being i'm gonna close this explorer by clicking on this explorer icon so that we can work with a large area now here what we are doing is we are calling this c plus plus compiler and we are passing all these arguments the first argument will tell the compiler to create the executable file with maximum debug information and this argument will tell the compiler to generate the warnings if there is any warning for the program and then this is the file that we want to compile along with the path and with this flag we are stating that we want to give a name for the executable file and this is the name for the executable file along with the path and the extension for the executable file is dot exe because in windows the executable files will have dot exe extension now this command here what it does is it generates the executable file but what we want to do is we want to run that executable file and to do that we will insert another command along with whatever the command that we are using here so to insert that we will add a space after this dot exe and we will type in and and then we will come back to this last section that we have in this args we will copy that and we will paste that here in the end which will look something like this so here make sure that you add this page properly and also you specify it just like the way i'm doing in here so what we are doing here is we are specifying two commands one this one is for building the executable file and after that this one is actually for running that executable file so we are combining those two tasks here and with this and we are separating multiple commands so if you wanted to execute one more command then here we have to use and and we have to specify that okay after that modification now since we have specified all these arguments in the command only we don't need the arks section so we will delete it completely so there is no args section and also we will delete this group section which will say that this is the default task you know this is not the default task whatever the task that we have configured before that is a default task so we will delete it like this so now we have a task here that we have created which will build the executable file and it will run it for us in one go so now we will go to the helloworld.cpp and we will open up the explorer and here i'm gonna delete this executable file so i'm gonna right click i'm gonna say delete then move to recycle bin okay now in this hello world folder we have the source file only now what we're gonna do is we're gonna use the task that we have set up in order to build the executable file and to run it in one go so make sure that you are in this helloworld.cpp file go to terminal and click on run task and now it will display the tasks that we can run we have two options one is the default task that we have configured in the beginning which is for building the active file and the second task that we have here is called as run c plus plus program by using g plus plus and this is the task that we have configured so now what we do is we will click on it now in terminal something happens but it will display some error messages now this error message is expected now why we get the error message here is if you look at the folder name and file name hello world then they contain space so here since we are executing a shell command the spaces will separate them so whenever we have space in the file name or folder name what we have to do is we have to enclose them in double quotes so we have to make that one modification so we will open up the tasks dot json and i'm going to close the explorer and here we will go to the task that we have set up that is run c plus plus program which is of type shell and here if you look at this command and you have to think where we might have space so this file name can contain space the executable name that we are providing along with the path that can contain space so what we do is we will place these values in double quotes but if i go to use a double quote here like this then it will display error that's because the command here that we are writing is actually placed or written by using double quotes so what we do here is we will escape the meaning of double quote by using backslash and then double quote so we will enclose the content of this file in double quotes backslash double quote after that we have minus 4 then we have this entity which will specify the name for the executable file along with its path so this can contain the space so we will place this between the double quotes so here in the beginning backslash double quote and after exe backslash double quote okay and also while running the program if the executable name contains spaces then it will not work so we have to place that in double quotes also so after and we will type in backslash double quote and after exe we will type in backslash double quote so that's it you have to take your time and make this modification very slowly you know make sure that you don't make any mistakes here after that save this task go back to the helloworld.cpp and open up the explorer now what we do here is we will run the task that we have set up so we will go to terminal we will say run task and now we will select run c plus plus program that is the task that we have configured now here it says executing the task so what it has done is it has generate the executable so we can see that in this hello world folder and also it has run that executable file so here we can see the output of the program this terminal is reused so um you know if we run it again without closing this terminal panel then all the contents of the previous run will be here okay so i'm gonna kill this terminal now and i'm gonna go to terminal run task and run c plus plus program by using g plus plus now you guys can see it has generated the executable file and it has run it for us so this is the output of the program okay now the last thing that we're gonna see in this tutorial is how we can debug this c plus plus program by using visual studio code so what we do is we will click on this run option or run icon which is present just above the extensions and once we do that we can see run and debug a button we will click on that button and here it will open up the command palette and it will display some options here we will use c plus plus gdb slash lldb because gdb is the debugger that we have installed so we will click on that now it will display the tasks that we have for building the executable file in order to debug the program it will uh it will build the executable file and then it will use that executable file for debugging the program so here we will go with g plus plus dot exe build and debug active file okay that is the task that we have set in the beginning the first task we will click on that and now what happens is in the terminal we can see build started build finished and now the debugger will be opened the program will be loaded for debugging and since we don't have a breakpoint the program has terminated its execution in the debug console you can see some information in the terminal you can see the output of the program and also in the left hand side we can see variable and other sections we will talk about that in just a minute okay now what i'm going to do is i'm going to close this panel and i'm going to go back to the explorer and if you look at the dot vs code folder now we have another file called as launch dot json this file contains the information for debugging the program if you want to make any modifications then you can make them for example one modification normally people do is uh stop at entry which means that when you debug the program the program will stop at the beginning of the program so it will stop at the main function then you can use the whatever the debugging commands available to you know go through the program and you guys can see what's happening by default it is set to false if you want you can change that one to true one more thing if you make any modifications here to you know whatever the files present in the dot ps code folder then i recommend you guys to close the visual studio code and open it up again okay i'm not going to make any modifications in here and now i'm going to go back to the helloworld.cpp that is the file containing the c plus plus program now the last thing that we're going to see here is how we can set up a breakpoint and how we can debug the program now if you take your mouse cursor to just before this line number then you guys can see a red dot appearing so if you left click on your mouse then it will place a break point in that particular line number for example i'm gonna set a breakpoint at this line number seven okay and after that i'm gonna go to this run and then now we can see the debug configuration selected for us so what we can do is we can click on this uh button which says start debugging when we hover on it so if i click on that what it does is it will generate the executable file first then it will load that in the debugger now this content is actually the output of the previous run so the output of the current run will appear here and also if you look at this program then the program has stopped at this line number seven that's because i have set the breakpoint at lane number seven if you go to the debug console then you guys can see that information here it says uh there is a breakpoint at line number seven so in this particular statement now in this uh variable section you guys can see the local variable and the values they are containing for example in this case the number variable is containing 0 now and also we can see the call stack section and also we have the buttons displayed in here for continuing for stepping over stepping into step out and stopping the debugger and all this stuff so now what we do is we will click on this step over and now what it does is it executes the line number seven and because of that the number variables value should be changed it should contain 30 now so if you look at this local variable value then number is number variable is containing 30. if you go to the terminal then nothing is displayed in here as the output because uh till now we have not displayed anything in this program again i'm going to go to step over i'm gonna click on this now it will execute this line number eight so after executing this line number eight uh we have the c out statement in here which will display the value of the number variable so that has been displayed in here in the terminal so number is 30 again i'm gonna click on this step over so it has printed goodbye we which we guys can see here and after that we are almost in the end of this program so what i'm gonna do is i'm gonna click on this uh continue which will uh you know continue till we have another breakpoint in the program but in this case we don't have any breakpoint so the program has terminated okay now this is the output that we guys can see in the debug console we can see the debug information and while debugging if you want to provide any debugging commands then you guys can specify that here okay so this is how we can set up the visual studio code for writing executing and debugging c plus programs if you like this video hit the like button if you don't like it hit the dislike button if you want to say something then write that in the comment box for more tutorials like this do subscribe to the channel thank you for watching i'll see you later in the next video
Info
Channel: LearningLad
Views: 54,519
Rating: undefined out of 5
Keywords: Visual Studio Code, VS Code, C++ Programming, Debug C++ Program, VS Code Debug C++ Program, Run C++ Program in Visual Studio Code, Set Up Visual Studio Code for C C++, C++ Ide, C++ Compiler, computer programming, programming for beginners, C++ Video Tutorial, mingw, g++, mingw-w64, mingw64, gdb, cpp
Id: rgCJbsCSARM
Channel Id: undefined
Length: 30min 11sec (1811 seconds)
Published: Fri Jan 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.