How To Setup C/C++ for VSCode | Setting Up VSCode For C/C++ Programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how you can set up C and C plus plus support on your Visual Studio code editor on your windows 11 operating system so for this you will need two things first one is obvious which is Visual Studio code so if you haven't installed Visual Studio code I will recommend you to install it it's a very easy and simple process and if you don't know how to install Visual Studio code I will put the link of the video in which I have shown how you can install and use Visual Studio code and once you have Visual Studio code we need a second tool which is called a compiler right so a compiler will help you to compile your CNC plus code on any operating system so for Windows we are going to install the Min GW compiler so let's start with that so first of all open your favorite browser and search for bin GW and in the search result you will see some websites so don't click on the first website which you will see here which is from Source Forge you need to open this website which says Min GW hyphen w64.org so just click on this link which says Min GW hyphen w64.org and once this website opens you will be able to see the overview here and on this website you can click on the downloads link here so here you will be able to see all the download link for bin GW for different operating systems so the one we are looking for is the windows version so we are going to click on this link which says Min GW w64 builds so just click on this link which is going to open this kind of window again and from here you can download the Min GW bills from Source Force so you need to click on this link now right so sourceforge link was shown directly in the search result but you need to go through this website in order to come to this website and once you are redirected to the sourceforge.net website you will see that Min GW 64-bit installer exe file will be downloaded on your windows 11 operating system so once this file is downloaded I'm going to click on this file and then you will see this warning which says do you want to allow this app from unknown publisher to make changes on your device I trust this so I will click on yes here and I'm going to minimize this browser and you can see Min GW installer has been started on my windows 11 operating system so on the first window I just need to click on next here which is going to start the installation here you can choose the version for Min GW so I'm going to choose the top most option which is 8.1.0 at the time of making this video you can also choose the architecture I'm going to choose x8664 and then you can choose the threads so generally for Threads I always choose posix but you can also choose win32 also also exception you have two options I will leave it as default and then you have the build revision I will leave it as default also and then click on next and this will be the location where your Min GW compiler will be installed so you can see it will be installed in the program files Min GW directory will be created and then this folder will be created right so if you don't have the good reason to change this location just leave it as default and also you can leave this checkbox as checked which is going to create a shortcut for your Min GW compiler tool so I'm going to click on next now which is going to start the download of all the files and packages for your mingw tools so just wait for this process to reach to the 100 Mark so now after some time I can see this window which says click next to continue the installation so I'm going to click on the next and now I can see this message which says Min GW has been successfully installed and I can click on finish to complete the installation so let me click on the Finish button so once I click on the Finish button Min GW is installed on my Windows operating systems so now once mingw is installed first of all we need to know the location where Min GW is installed so at the time of installation you might have noted the location so let me show you the default location where Min GW will be installed so you need to go inside the C directory and then go inside program files and then you will be able to see this Min GW hyphen W 64 bit folder here and then this folder will be there so this is the location where Min GW is installed then you need to go inside Min gw64 and here under the bin folder you will see all the files which will help you to compile your C or C plus plus programs okay so go until the bin folder right and you need to copy this path now we need to set the environment variables for Min GW so that we can use GCC or G plus plus command on our Windows command prompt or Powershell or terminal so for that what you need to do you just need to click on search and then search for edit EnV and it's going to show you this result which says edit environment variables for your account and you can see this window is going to open right now if you can't find this edit environment variable window using the search option then you can always open the control panel and then once control panel opens you need to view by category here and then click on system and security and then once again click on system here which is going to open the settings app and here it's going to show you an about section this link which says Advanced system settings so just click on that and then click on environment variables which is also going to open the same window so you can choose any one of the method so once this environment variables window opens select path under system variables and then click on edit and then click on new here okay so it's going to allow you to add a new environment variable and then just copy the path until the bin folder inside your Min gw64 bit folder okay so this path is until bin folder okay and once you copied it you can just add this path here inside your environment variables and then just press enter which is going to add your path and then click OK here and then click OK and then once again click on OK and now you can close all the windows here and now we can test our GCC and G plus plus versions and commands right so you can open the command prompt by just pressing CMD and then open the command prompt from the result and once the command prompt is open here you can just write GCC space hyphen hyphen version and then press enter and if you see some result which looks like this that means GCC command is working successfully on your windows 11 operating system similarly you can also check the G plus plus command which is also used to compile your C plus plus programs so it's also going to show you this kind of result here so once you see this kind of result that means your GCC and G plus plus commands are working so once you have installed the Min GW compiler and you have set the environment variables for GCC and G plus plus we are ready to create our first C plus plus code so let's start with the very basics so what I'm going to do is I'm going to open my uh command prompt so for that I'm going to just search for CMD and then press enter which is going to open my command prompt and then let's say I'm going to create a folder in which I want to uh have all my C plus related files right so let's say I want to create this folder on my desktop I'm going to just CD to my desktop first of all and then I'm going to create a folder and on command prompt to create a folder you can give this command mkdir and then let's say I want to name my folder as my CPP app and then I'm going to just press enter which is going to create the folder with this name you can also right click here and then create the folder uh if you want you don't need to create it with the help of command prompt so once this folder is created I'm going to CD in this folder and then when I type on the command prompt code space Dot and then press enter it's going to open Visual Studio code and this folder will be opened in Visual Studio code from which I have given this Command right so currently I am in my CPP app folder in the command prompt right and when I gave this command it's going to open this folder in Visual Studio code here I can create a CPP file so let us create a very basic C 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's 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 buy 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 topmost 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 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 iostream 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 IO 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 am 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 your windows 11 operating system they are just opened in your Visual Studio code so they come Integra created 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 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 break points 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 breakpoint 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 one have zero 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 dot dot 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 for C and C plus plus development on your windows 11 operating system I hope you've enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge2
Views: 32,189
Rating: undefined out of 5
Keywords: how to run C++ code in visual studio code, how to run C++ program in visual studio code terminal, visual studio code C++, C++ extension for visual studio code, how to install pip in visual studio code, C++ download, visual studio code download, C++ interpreter vscode
Id: T0ncwxf-rFs
Channel Id: undefined
Length: 27min 44sec (1664 seconds)
Published: Sat Mar 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.