Set Up C++ Development With Visual Studio Code on Ubuntu 22.04 | VSCode C++ Development Basics

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 add the support for GCC and G plus plus on your Visual Studio code editor so first of all you need to have the GCC and G plus plus compiler on your Ubuntu operating system so let me show you how you can add the support for GCC and G plus plus and then we are going to see how we can set GCC and C plus plus uh compiler on our Visual Studio code editor so using GCC you can compile C programs and using G plus plus you can compile C plus plus programs on your Ubuntu operating system so let's see how we can add the support for both of these compilers so first of all you need to open your terminal you can open the terminal by pressing Ctrl alt t at the same time or you can go to Applications and then open the terminal from here also once the terminal is open you need to give these two commands so the first command is sudo apt update and then press enter and then give your ubuntu's password which you use to login to your Ubuntu operating system so what this command is going to do is it's going to update the package index on your Ubuntu operating system just wait for this command to finish so once this command is finished let me clear the terminal you can clear the terminal by giving this command clear and then press enter which is going to basically clear your terminal right so the First Command was sudo apt update once this command is finished the second command we need to give here is sudo apt install build Essentials so this command is going to install the build essential development package which automatically adds the support for GCC and G plus plus right so just give this command sudo apt install build essential and then press enter and then press y to continue and then press enter and just wait for this command to finish so once these two commands finished successfully let me clear the terminal and now you can give the GCC command or G plus plus command to test that so to test the version of GCC you have on your Ubuntu operating system you can just write this command GCC hyphen hyphen version and then press enter and it's going to give you some outputs like this right so it says GCC Ubuntu and then the version of GCC which we have installed is 11.3.0 similarly you can check the version of G plus plus so just write G plus plus hyphen hyphen version and it's also going to give you the version of G plus plus on your Ubuntu operating system so once you have installed GCC and G plus plus using build Essentials let's create a very simple C plus plus file let's say and check whether it's working using visual studio code now just open your terminal once again so just press Ctrl alt T to open the terminal and let's say I want to create a folder which will be called let's say work folder right so I'm going to just write mkdir and then name my folder as work right and then press enter and when you give LS command you can see this work folder is created so now I can either write code and then the name of the folder which is work and then press enter and it's going to open this work folder in Visual Studio code editor so you can see this folder is opened in Visual Studio code so you can either give this command to open this folder in Visual Studio code or what you can do you can give CD and then you can see the into your work folder and inside your folder where you want to create or see your C plus plus programs you can just write code and then space dot okay so once you go inside your folder you can just give code space Dot if you want to open any folder using visual studio code you need to write code and that path of your folder right so because the path of the work folder is was in the current directory itself so I have written work once you are inside the folder using CD command you can just give code space dot in order to open that folder right so I have given code space Dot and once again it's going to open the same work folder once this work folder is created you can create a new file by just clicking on this new file icon or you can click on file and then new file and then create a new file okay so let me create a c plus file here and here if you see this kind of warning you can just check this checkbox and click on yes I trust the author right so let's create a new file by clicking on this option and then I'm going to name my file as test.cpp you can also give the extension test.cc to your C plus file but for Simplicity I'm going to name my extension as test dot CPP so just press enter and this file will be created as soon as you create a cpb file Visual Studio code recognizes that you are trying to create a C plus plus program and it's going to suggest you to install some extensions so you can see do you want to install the recommended C and C plus plus extension pack and this extension is from Microsoft itself which is the company behind Visual Studio code right you can either click on install here or you can click on show recommendations let me click on show recommendations here and what it's going to do is it's going to show me all the C and C plus recommendations in the extensions tab right if you don't see any recommendation or if you don't see any pop-up here let me just close this and let's say you don't see any pop-up when you create your test.cpp file you can click on this extensions section here and here you can just write C and then press enter and it's going to also give you the popular extension for C language or you can write C plus plus and it's going to give you the popular extension for C plus plus language the top extension is generally the most popular and most trusted so just click on the top option and then you can see who is the developer of this extension as you can see it's developed by Microsoft and how many downloads are there right so it has around 47.8 million downloads at the time of making this video just click on install which is going to install this extension which means that Visual Studio code will add the support for C and C plus plus and what does this do so you can see what are the features it gives you intellisense debugging capability and many other good features on your Visual Studio code for C and C plus plus once this extension is installed you will be able to see disable uninstall and switch to pre-release buttons If you want to uninstall this extension at any time you can uninstall it by using uninstall button so now this extension is installed right let's close this and go to our c plus file and you can also click on the Explorer option for now where you can see the name of all the files inside your work folder now let's create a very simple C plus plus program in order to compile it so I'm going to just write a very simple C plus plus program C plus plus program you can find anywhere on internet so I'm not going to show you how to write the C plus program just write a very basic C plus plus program this program is just printing hello world right so in order to compile and run your program just click on this small arrow in front of this icon and then click on run C and C plus plus file which is going to ask you to choose your compiler and once you have added the compiler using the build Essentials command you can see Visual Studio code detects your G plus plus compiler you can see here and you can choose this compiler so just choose the compiler which says G plus plus build and debug active file I'm going to just choose this one which is going to choose my compiler and then it's going to compile my C plus file and it's going to give me the output of that c plus file now you can see everything is worked successfully and there is this new file which is created which is called test right so generally when you compile a C plus plus program it's going to create a binary file which is an executable file which you can run to get the output of your C plus file in our case it's going to print hello world right so the compilation is successful once the compilation is successful you will be able to see this icon is changed now and here it says run C or C plus plus file so once again click on this uh icon and this time you will see that the output is printed here which is hello world if you want to uh run your executable file manually you can click on this plus sign here which is going to open a new bash terminal or if you have multiple terminal versions you will see those bash or let's say zsh or shell here listed right so you can open that particular uh terminal using this option right here you can just write dot forward slash and the name of your executable file in our case it's test right this output is test right so dot forward slash test and then press enter and it's going to give us the output of our executable file which is hello world so using this button you can either debug your file or run your file so let's try to debug our file so let's modify this C plus pro program a little so I'm going to declare three variables num1 comma num 2 comma sum so basically what I want to do here is I want to uh do or have the sum of two numbers right then I'm going to just say num 1 is equal to 10 let's say num 2 is equal to 20 and then the sum will be the addition of num 1 plus num 2 okay so I have added this logic so that I can explain you the debugging once you have changed your code just press Ctrl s to save your file and then let's add a breakpoint here here here here and here okay so in order to add the breakpoint you can just go here where you will see red dot just click on that and the breakpoint is added you can also go to this debug section so just click on debug and here you will see all the breakpoints which you have added you can disable those breakpoints or just toggle active breakpoints from here or completely add a new breakpoint so the list of breakpoints are listed here you can click on run and debug from here also or you can click on the boxes or C plus plus from here also I'm going to click this option which is going to start the debugging of my file right so you can see the debugging has been started now if you know your C plus plus programs when you declare any variables without the initialization first of all they take some garbage value right so this is what is happening here number one have this garbage value of 32 767 num2 also have the garbage value and then some also have the garbage value right let's move forward in this debugging logic you will also see when you start the debugging these buttons on the top first button is for continue second is for step over third is for step into fourth is for step out and then you can restart your debugging or stop your debugging using these two buttons let's tap over so I'm going to click on step over and then once we step over that means this line of code has been executed the program execution stops on the next line once this line of code is executed you can see num1 is equal to 10 because now we have assigned a value to num1 earlier it has the garbage value but num2 value stays the same because this line of code is not executed so let's move forward now this line of code is executed so num2 is 20 but still the sum is the garbage value because our program execution stops at this line of code once we move forward this line of code is executed and the value here is 30 right 10 plus 20 is equal to 30 you will be able to see that value here right let's move forward and now the hello world will be printed for example and then you can move forward here and then program is fully executed right you can also see the debug console here and the output of your program or what's happening in your debug console everything you will be able to see here okay so this is how you can run your code debug your code and compile and execute your code on your Ubuntu operating system using visual studio code editor I hope you enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 32,070
Rating: undefined out of 5
Keywords: Install the C++ Linux workload in Visual Studio, Debug C++ in Visual Studio Code, Create C++ cross-platform projects in Visual Studio, Using C++ on Linux in VS Code, How to Setup Visual Studio Code for C/C++ in Ubuntu 22.04
Id: oQ9E_LFoo9U
Channel Id: undefined
Length: 17min 5sec (1025 seconds)
Published: Thu Jun 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.