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

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 the c program now if you are just interested in writing a c program and executing it as quickly as possible then i have another method available where we will be using the code runner extension and the link for the tutorial that i have made will be in the description box you guys can check that out here we will open up the visual studio code set up the workspace set up the task and also we will see how we can debug the code now in order to continue with this video you need to download and install visual studio code and also you need to download and install the c tool set along with the debugger now if you don't know about it then i have the tutorials again the link will be in the description box so now before continuing first we will make sure that we have all the tools required so open up the command prompt and here we will type in gcc space minus minus version and hit the enter button and you will see some version displayed in here if you get an error message stating that gcc is not recognized as an internal or external command then it means that gcc is not installed if it is installed then you have not set the path enrollment variable properly so you have to watch my tutorial and you have to set up everything now after that we will make sure that we have a debugger installed we will be working with the gdb debugger so we will check whether it is installed or not so we will type in gdb space minus minus version and here also we will see a version number displayed you know if you get an error message stating that it is not an recognized command or something like that then again it means that it is not installed and you have to install it okay so now here we have the tools required so what we can do is we can open up the visual studio code and set it up so i'm gonna click on the start button and i'm gonna launch the visual studio code now here it is opening with the welcome screen the first thing that i'm gonna do is i'm gonna click on the file and i'm gonna say add folder to workspace click on that now you can choose whatever the location that you want but in this case i'm gonna save all these files in my desktop and here i'm to create a new folder and i'm going to say c programming and after that i'm going to open up this folder i'm going to create another folder and i'm going to say programs and after that i'm gonna select this folder and i will click on add now here in this explorer window we can see the programs folder if the explorer window is not visible then you guys can click on this icon which says explorer okay now the next thing that we're gonna do is we're gonna click on file and we will say save workspace as and again i'm gonna go to desktop and this time i'm gonna open up the c programming folder that we have created and here i'm gonna save the workspace we can give any name i'm gonna say c programming and we will say workspace now we can give any name and after that we will click on save so now if you look at the explorer here then we have the workspace called as c programming workspace and then we have the programs folder now we will select this programs folder and we will click on this new folder icon and here we can create a folder and what i'm going to do is i'm gonna create a folder and inside that folder i'm gonna create a file which has the code written in the c language so here i'm gonna say hello world and also make a note that i'm adding space here between hello and world your file and the folder name you know if you want you can have the spaces otherwise you can write it in just one single name okay now click outside and we have this folder again we will click on this new file and now hello space world dot c which will be the file uh you know which will contain the code written in the c language now i'm going to close this welcome tab and here i'm going to write a very simple c program and i'm going to fast forward that one so here i have written a very simple program which will ask the user to enter his age and then it will display your ages in whatever the value the user is gonna enter now what we have to do is we have to generate an executable file from this source file and after that we have to run that executable file now before continuing what we do is we will install an extension so here we will click on this extensions icon and it will display installed and popular extensions here we will search for c c plus plus and the first extension that we see here is called as c c principles and it has been developed by microsoft and we will click on that it provides the uh cc plus plus intelligence debugging and code browsing features so what we do is we will install this extension so i'm going to click on this install button now it will install the extension and in the bottom right corner you can see the installation happening so you just have to wait for the visual studio code to download and install this extension completely now this um progress bar has been disappeared and it means that the extension has been installed completely now we will click on the explorer icon in here and we go to the hello world.c and also here i'm going to close the extension cc plus plus tab okay now we have the source file so now what we have to do is we have to generate an executable file so what we do is we will set up a build task which will build an executable file from this source file so we will click on the terminal and we have to click on this configure default task and keep in mind we just have to do it only once you know the first time when we are setting it up once we have set up everything then we just have to write the program select the task that we want to run and everything will happen for us okay so now here we can see cc plus plus gcc.exe build active file it is displayed here in this command palette and it will also display the compiler that you have installed it may display the path also okay now what we have to do is we have to click on this option now what it does is inside the programs folder which is present in our workspace it will create another folder called as dot vs code and inside that folder we will have task.json file this file will contain the build task now here we can see some details about in this task for example the type is cpp build and we can see the label and also we can see the command used the arguments used if you want you can pass in more arguments in here for example it says our default debug information with this minus g you know if you want we can change that one to minus g3 which will mean that the executable will contain the maximum debug information and also if you want to pass in any other arguments then we can specify for example uh what i want to do is i'm going to say if there are any warnings in the program i want them to be displayed so here i'm gonna specify that one here minus wall and after that we will add a comma in here so just like the way i'm doing here if you want you can also specify the specific c version to be used for building the executable file otherwise it will choose whatever the default c version used you know we can change that one later by using another file which will apply for the entire workspace and we will talk about that later okay after that what i'm gonna do is i'm gonna save this one and also go to the source file which is hello world.c in this case go to file and make sure that autosave is selected so that you know whenever we make any modifications the file will be saved automatically now what we can do is we can use the task that we have created and build an executable file now before building an executable file what i want you to do is close the visual studio code and open it up again so that whatever the new files that we have created everything will be loaded properly okay close it and relaunch now what we do is we will click on the terminal and we will go to run build task or you guys can use the shortcut click on that now what it does is it builds the executable file by using the task that we have mentioned if you look at the file explorer then in the hello world folder we have an executable file called as hello world.exe i misspelled world here but you know that's okay just the name of the file okay so now we have the executable file what we have to do is we have to run this executable file so for that what we need is we need a terminal so i'm going to click on this plus icon in here which will create a new terminal for us and here now we are in this programs folder and the executable file that we want is actually present inside another folder called as hello world so first we have to go to the hello world folder so we will type in cd space hello world if i hit the enter button here then we are in the hello world folder now if it generates an error then you just have to type in cd space and then in double quotes type in hello space world so now we are in the hello world folder where we have the executable file so what we have to do is we have to write the name of the executable file in here to run it so it will be hello space wr old that's because i misspelled it and now if i hit the enter button it says it is not a recognized command because the name contains a space so i have to write that in double quote so i'm gonna type in this name in between the double quotes so it will become one single entity now and if i hit the enter button it says enter your age i'm gonna say 30 it says your age is 30. so what we have done here is we have generated an executable file and and also we have run that executable file uh one simple trick that i want to show is if you don't want to build the executable file first then you know going to the terminal and typing all these things what you can do is you can set up the task in such a way that it will build the executable file and also it will run it directly so to do that what we do is we will open up the task.json file that we have in this dot vs code so i'm going to open it up in here and here we can see task and we can see the information specified between the curly braces so this one so what we're gonna do is we're gonna copy everything which is present from one curly brace to another curly brace here like this um after that we will copy that uh before that i'm gonna close this explorer so that you know we can work with uh more space in here after that we go to the end here we will add a comma then we go to the next line and we will paste the content that we have copied okay now we will go to the second part in here and we will change something so we will change the type here from cpp build to shell okay after that we will change the label we will change the value and we can say run this c program you know we can give any name because we are creating this task now and after that we have the command and we have the arguments so what we have to do here is we have to place these arguments along with the command here so what we do is after this gcc.exe we will add a space and we will copy these contents and we will paste them in here so it will be minus g3 space minus wall i'm going to copy that i'm going to paste it in here after that we have dollar file copy that and paste it in here we have negative o copy that paste it in here and by the way make sure that you separate them by using a space after the -4 we have a space and after that i'm going to copy this and i'm going to paste it in here and after that add a space type in and just like the way i'm doing in here and paste this last part again so it will be like this so this last part will actually contain the executable file name along with the path and and what we are doing here is we are stating two commands to be executed one is build the c program generate the executable file and after that call that executable file in here by using this okay now we will come back here and since we have specified these arguments in this command only we don't need the args we're gonna remove that and after that here we will remove this group also okay after that save it so now we have this task which we have created now we will go to the hello world and also i'm gonna open up the explorer and uh you know we can delete this uh executable file so i'm gonna right click delete it move to the recycle bin so in this hello world folder now we have only hello world.c so now what we're gonna do is we're gonna try to run this program by using the task that we have created so click on the terminal click on the run task and now we can see we have two options one is the build task which was created by the extension automatically and then we have run this program task that we have configured i have given this name you guys may have given a different name click on that now here it says we have some error but it's okay it is expected we can solve this so what happens here is since the folder name and the file name contains the space when we write this command here it treats them as separate entities so whenever we have the folder name and file name with space we need to place them between double quotes so we just have to make this one modification if the folder name and the file name doesn't contain any space then this will work but if it has a space then we get this error and by the way for debugging the program this is not required i just wanted to show you guys um if i have taken around five minutes of your life then sorry about that okay now we're gonna go to the task.json again i'm gonna close the explorer and here what we're gonna do is whenever we have something which can contain space we place them in double quotes so we're gonna go to the task that we have configured which is the task which has the type shell and label is run this c program so here this file name which is displayed in here after minus wall and it can contain space so we have to place the value of that file name in double quotes and the thing is this command value is actually specified in double quotes so we have to escape the double quote that we use in here so we will use the backslash we will type in double quote again after file backslash double quote take your time observe everything cleanly and do it after negative o we have folder name and the file name with the extension and this can contain the uh spaces so we have to place them in double quotes so after the negative oh we will type in backslash double quote after the exe we will we will add backslash double quote and here also when we call the executable that we want to run it will specify the folder name along with the file name so you know that can contain the space so we will add that after and backslash double quote after exe backslash double quote that's it after that save it now we will go to helloworld.c and here we will open up the explorer and we can see hello world folder and what i have done is uh i have deleted the executable file which was present in the hello world folder okay now we will click on the terminal we will say run task again we have two tasks run this c program then uh the task to build the executable file so we will click on run this c program and now you guys can see it says enter your age i'm gonna say 30 and that's it you know we have the output of the program so you know if you set up this task then you don't have to use the extensions like core runner or something okay so every time whenever you want to run the program go to terminal run task and select the task that you want to run so you will get the output okay now we will see how we can debug this program so what we do is just above the extension icon we can see run click on that and it will display a button called as run and debug click on that and it will open up the command palette and we have two options one is gdp lldb or debug using the windows if you remember we have the gtb installed so we will choose uh c plus g db lldb so we will choose that one and now it will say the task to choose for building the executable file for debugging so here we will choose the task that the cc plus plus extension has created for us if remember that is build and debug active file okay so we will choose that now it will start the build it will finish the build it will create the executable file it will launch the debugger and here in the debug console we can see the debug information and if you go to the terminal then we can see the output of the program now it says interview age i'm gonna say 20 hit the enter button it says your age is 20 that's the output of the program the program has run successfully and also in the debug console we can see the debug information now here i'm going to click on the explorer icon and what has happened here is in the dot vs code folder another file is created called as launch dot json here i'm gonna close this panel and i'm gonna open up the launch.json file and you can see the information here um we don't have to change anything in here but if you want to do any changes then you can do it in here okay so this is the launch file automatically generated for us by that cc plus plus extension so now we will see how we can debug by using a breakpoint now here what i'm going to do is i'm going to add a breakpoint let us say i'm going to add the break point here in this line number seven so if you take your mouse just before the line number then you guys can see a red dot appearing if you left click in your mouse it will place that breakpoint in that particular point okay i'm going to add the breakpoint here in this printf statement and after that what we do is we will click on the run you know we go to the uh this debug section and and here we will click on this option which says start debugging click on that so what it does is it generates the executable file and then it will start debugging so now here if you click on the debug console then we can see the debugging information and in terminal it says enter your age i'm going to enter 30 and after that i'm gonna hit the enter button in here now you guys can see it has hit the break point because i have mentioned the breakpoint here at line number seven if we go to the debug console then we can see it says thread has hit a breakpoint at lane number seven and also we can see the uh more information in here for example we can see the values present in the local variable in this case we have age edges containing 30 and also we can see the call stack and all this stuff and also we can see the options like continue step into step over and step out and all these options so here i'm gonna click on step over now if i come back to the terminal section then it will display your age is 30 and since we are in the end of this program i'm going to click on this continue option which will run the program until it encounters the next breakpoint and in this case we don't have any breakpoint so the program will terminate so this is how we can you know quickly set up the visual studio code for debugging the program and one last thing that we're gonna talk in this tutorial is if you want to use a specific cc plus plus configuration then you can do that now i'm gonna remove this breakpoint in here now what you can do here is if you want you can specify a particular cc plus plus settings to use to do that we will click on the view command palette type in c slash c plus plus colon edit and you have the option to edit the c c plus plus configurations you can directly edit the json file or you can use the graphical user interface you know which is ui so i'm going to open up the graphical user interface once we click on that if you look at the dot vs code folder a new file is created called as ccpp properties and here we can specify i know particular cc plus plus properties to use for example uh c standard used is uh gunu17 for c plus plus it is using a gunu 14. if you want you can change that one to uh gonna 20 and uh for c plus plus we have the latest standard available used so you know if you want to make any changes here then you guys can make that and you know after that you can run your program so this is it guys this is how we can use visual studio code for writing executing and debugging c 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,020
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, gcc, mingw-w64, mingw64
Id: r1zCtg_wqCA
Channel Id: undefined
Length: 25min 17sec (1517 seconds)
Published: Tue Jan 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.