Optimize C++ Development in VS Code: Harness the Power of tasks - 2024 Linux Edition

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone and welcome to this video in this one I am going to show you how to install C++ for use inside of Visual Studio code on a Linux system but before we actually look at that let's see what you can do after you are done with this video I am going to hop over on my visual studio code installation here and this is running on Linux if you do you name you see that it is a Linux system and we have project open you see we have a main CPP file we have a person class which is brought into the main CPP file we can compile and run this application if we want to compile it you can come to terminal and run task and choose whatever compiler you want to use in this case we have GCC configured so we're going to choose that and we it seems that we have a problem let's go to our tasks. Json file uh let's try to run that again uh terminal run task and use g++ now it's going to build successfully I don't know why it was giving me a hard time but now we can compile our program so now that we have it compiled we can run rooster which is our binary and it is going to say hello whatever this is the output from the application here if you want you can even hit F5 to run this is going to run the application in debug mode and you see that we have the output here if you want to debug you just have to set up a break point let's put that on line 18 this is going to do and uh we can start debugging how do you start debugging you can uh go to the debug button here and choose a debug configuration and run hit this run button here but if you are for shortcuts you can just hit F5 and that's going to start your debagging process it is going to build the binary and we are going to hit our break point on line 18 here once we hit the breakpoint you see that we can inspect things we have in our file for example you can hover over message and you can see things that are inside you can hover over world and see things that are inside and again you can go on message and see what we have inside and uh the most important thing is that you can step through things for example we can stdc out andl we can uh print from the person object P here and we see that this is going to work you can do these kind of things and you can inspect the local variables the registers you can do all kinds of crazy things this is what you will be able to do once you are done with this video so let's see how to set all this up we will be doing this things in three steps the first step is going to be to install a compiler on Linux it is possible to install the GCC compiler it is possible to install the clango compiler but Visual Studio code officially support by official supports I mean in the documentation they say that they support GCC and they are going to give you a good experience clang can also work but in my experience it gave me bad experience in debugging so I'm not going to show that to you maybe I can do a video later on let me know in the comments if you are interested but we will be using GCC on Linux here after we have a compiler installed we will install Visual Studio code and the C++ extension and after that we will set up our project to to build and debug and the configurations to do that will be stored in a few files one is going to be the tasks. Json file the other is going to be the launch. Json file before we even start let's go to the documentation for the extension that I will be showing you so let's go to the C++ extension here and go to the documentation and they have a few tutorials they have one for msvc on Windows GCC on Windows and GCC on l Linux this is what we want to use I have this opened in my browser and we will be using this as a reference you don't have to open this you can just watch the video and download the things that I tell you to download but if you want you can just copy this link and put that in your browser I am going to share this link in the description of the video if you need that they start by saying what we need installed we need Visual Studio code installed we need the C++ extension installed we will install these things later but we are going to install the compiler first which is what they say here that we need to make sure we have this is how they tell you to install the compiler so you can say GCV to see if you have it installed if you don't have it installed you can do sudo app to get update and then install bu essential on your system let's do that we are going to hop over into our terminal and run this command so let's do sudo app to get update because I am doing this on buntu so these commands are going to work I am going to put in my password and it is going to get any updates that are needed while it is running we can hop over back to the documentation and grab the command that we need which is going to install GCC so we can copy it I have trouble copying this my mouse is not cooperating let's H over back you see the update is done now we can put in the command to install boil essential and GTB this is going to boil to install the GCC compiler on Linux and give you the debugger and it is going to tell me that everything is installed on my system because I already installed those things but if you don't have them installed they are going to install on your system after we have that we can do GCC D- version as they say in the documentation we have a gcc1 installed we can do g++ version it is going to report the version that we have installed here we can also do GDB version and it is going to tell us the version and now we can be sure that we have GCC installed on this system here and we are ready to move on with the tutorial we are not going to completely follow the tutorial here we will be changing a few things if I find that interesting now we can move on to the step to install Visual Studio code because they say you need that you can hop over to the link that they have here and download and install Visual Studio code for whatever Linux distribution you are on after you do that you will start Visual Studio code and we can start Visual Studio code from our terminal let's try to do that so we go to the terminal and we say [Music] code and run like this this is going to start Vis Studio code and after you have it you will create a folder somewhere on your system uh let's CD into my home directory let's do LS and let's say BS code sandbox I think we can do that so mkd R vs code sandbox we can go and we should say mkdir not mder so let's do that mkdir once we do that we can CD into vs code sandbox and in in here we will create a folder let's make dur one tasks Linux this is what I am going to name this here once I have this folder I can open it inside of Visual Studio code I am going to go back to visual studio code and say file open folder and I am going to go in my home directory and find vs code sandbox and open this folder here this is going to be where we start doing our things the folder is currently empty but we can uh just create stuff in but before we do make sure that you have the C++ extensions that we need installed I forgot that I am happy that I remembered so go into the extensions tab here which says extensions and uh in the text bar here type in C++ and they will give you a suggestion by Microsoft so this is what you need to install for you it is going to say install now it's not saying install because I already have this installed on this Linux box but you will install this this is the only thing you need to start using C++ inside of Visual Studio code once you have this installed you will need to go back to the Explorer tab okay and create a file that we will be using here hit the plus button to create a file and say main. CPP this is going to be your C++ file if you want you can copy the code that they suggest in the documentation here but I want to use C++ 20 for this project so let's try to go to CPP reference space share operator they will give us some code that we can use to test that our compiler at least support C++ 20 and the code that I am talking about is what we have here so let's copy this code and still and paste it back into our main.cpp file this is the code we will attempt to compile from inside of Visual Studio code and our compiler need to support C++ 20 because we are using a C++ 20 feature here you can read about this operator from CPP reference now we have the code how do we compile and run it well the documentation says that you need to let's go down and see if we can find that they say that you need to go to this button here in the top right corner and press the Run button let's try to do that we go back into Visual Studio code and it is not saying the same things it says but you can select run C and C++ file if you try to do that it will tell you to select a compiler it is going to pop up a dialogue with the compilers that it has detected on your system in this case it's detected an installation of GCC which is in user bean. GCC this is what we will select this is the compiler you just installed so let's use that uh with g++ because we are using g++ so I am going to select the second thing here if I do that it is going to fail but if you look in the Explorer here see that we have generated a vs code folder I want want you to see that okay let's make sure I can highlight this properly so we have a vs code folder here and if we go inside we have generated let's close out of this we have generated a tasks. Json file let's look at that file because this file is what is going to be used to tell Visual Studio code how to compile your code and you see we have a tasks property which contains ajon array and this Jon array is going to be containing configurations that your Visual Studio code instance is going to be using to compile the code uh you see that they say that the command that is going to be run is g++ which is inside of our user bin directory by the way if you want to see where your compiler was installed let's do contrl Z if you want to see where your compiler was installed you can say where is G g++ I think this is the command you run and it is going to tell you locations in your system where you have g++ installed if you go there you will find this binary here this was something I think was fun now what do we need to do to compile this code because it is failing to compile we need to configure our compiler to use C++ 20 and to do that we have to add another flag after the G option here which is going to be specifying the C ++ standard that we need so we need to add this thing I chose C++ 23 because GCC supports this so why not use it it is the best standard now that we have put this option in and by the way if you don't want to type this than or if for whatever reason you need the reference please find the code for these things in the GitHub repository that I am going to link in the description below so you have this here now we can try to to run again again we come here and hit the Run button now it is going to compile successfully and it is going to run the application if we go to terminal you see that it has output some stuff okay now we can run the application but there are a few things that we can configure to make the experience better let's go back to the documentation and pick some Clues now we have run hello world we can run it and see things running in our terminal and you see that in the documentation they talk about the tasks that Json file that we just generated but we have one that is specific to what we just did so we can skip over this thing in the documentation but if we go down there is something useful that we can take advantage of here so down here they say modifying the Json file that we generated we can modify it to make it build multiple C++ files this is something I find useful so I am going to grab this code snippet here and replace it in place of the file thing that we used in our tasks that Json file so I am going to take this thing out on line 11 so let's take this out and paste in what we just got from the documentation and make sure this is aligned properly put a comma after that so we will be building every C++ file in the workspace folder but we also want to change the name of the binary to Rooster because that's what I like to do you can name it whatever you want but it is going to be put into the same folder as the file as the active file selected in the file explorer here this is something you need to know that's why we need the file selected here so we're going to select the file and then try to run again now it's going to generate a binary named to Rooster okay you can see it here let's make sure everybody can see it we have a binary here and the binary has run and it has output this text here this is what we are doing we can run our application the next thing I want you to do is to debug your application to be able to step through things to really see what is happening in your application debugging is surprisingly easy all you have to do is set up a breakpoint so we can put a breakpoint For example on line 18 here you see the that the Red Dot here is a breakpoint and after you do that you come to the button on top here and choose debug C or C++ file once you do that the binary is going to be built again it is going to be plugged into the debugger that is associated with our compiler and we are going to hit the breakpoint here you see on line 18 we have hit the breakpoint we can hover over things to see the data inside for example you see that our Point object contains X and Y we can look at s the set here you can see that it doesn't have things inside we can go to any object and really hover over it but also we can go on the left and look at the local variables that we have set up in our project you can really inspect things you can set up watches you can look at the call stack you can do all kinds of crazy things but my favorite part in debugging is to step through code to really see what is happening as our code runs okay now we can debug our application and everything is really fine but this is not completely foolproof because for example if you need to pass arguments to the application that you are debugging then this doesn't work because this is completely taken over by Visual Studio code there is a way you can set up a configuration file for debugging and that file is going to be picked up by Visual Studio code with the information that you have inside and to set up that file it's really easy all you have to do is click on the G icon here so we're going to click on that and we will have to select a compiler here that compiler is going to be associated with our debugging configuration so we will choose g++ naturally once we do that notice what happens in your vs code folder on the left a launch. Json file is generated and that file is going to be containing information about how to debug your application for example we have a debugger that is going to be used uh it is GTB okay this is the debugger that is going to be used for debugging and they say the task in the tasks that Json file that is going to be associated with this debugging configuration here these things have to match what we have here on pre-launch task on line 26 has to match with the label that we have in the tasks that Json file online five here these two things have to match if if they don't match you will have problems here now that we have the launch. Json file we have the option to go in these args property and specify command line parameters that our application is going to use and if we debug these command line parameters are going to be picked up this is something you may need to do now that we have that file and we can go in main. CPP so for example we can go on line 18 and set up a breakpoint and we can go down here in the start bar and click on our debugging configuration to start debugging you see they say that we need to select the debugging configuration and we will start debugging our application here this is really cool we can step through things we can do all kinds of crazy things here so if you want to debug you can come down here and select your debugging configuration it is going to be picked up and used another thing you can do is come to the left here and click on run and debug and on top here you have the option to choose your debug configuration and if you click on the play button you will start debugging this is another way to do it let's stop our debugging another thing you can do is go to run and say start debugging okay this is going to start debugging but you also have the option to run your application without debugging start without debugging so you can do that or you can go through shortcuts and really do these things if you if you hit contrl F5 this is going to start without debugging if you hit F5 it is going to start debugging this is working really nice and this is really cool okay now we have this working let's bring in multiple files and show you that it can really work so I am going to add in a person header file and a person CPP file and I am going to put in the code that I steal from my GitHub repository because I don't want to to type all these things so we can go here and grab the header file we will put the code in our header file it is a simple person class we will also grab the CPP file do that and we will put the code in the CPP file here now that we have this we can hop over into our main. C++ file and include person. H we can do that include person. H and we can create a person object in our main file here we can say person uh person p and we can say P for example and this is going to do what we want to do I think we can put a new line character after this thing here so we can say back slash n here to put a new line character after we print all these things this is going to do after we do this we will have to rebuild the application so for example we can hit contrl F4 five to run without debugging and see that our I am eating text is outputed it is output here and this is really cool okay you can use multiple files with this setup that we have done here okay now we have a working configuration that we can use to do C++ development from within Visual Studio code we installed the GCC compiler we installed Visual Studio code and and the extension and we saw that we can configure stuff through these configuration files here but this is not completely foolproof the good thing about this is that it is simple you don't have to install a lot of things but it also has a lot of drawbacks for example it is hard to configure properties it is hard to switch ID these Json files are a visual studio code thing if you put them in another IDE it is going to completely not understand what is happening it is very hard to bring bring in third party dependencies with this way of doing things I am just trying to say here this is a good way to start developing applications with C++ from within Visual Studio code but there is a much better way and I will be sharing that in the next video using a tool called CME to really bring everything together and have an easier Life as a C++ developer before I let you go let's go back to the project and show you something that is bothering me and it is that we have these squiggles and to get rid of them you have to come to view and command pallette and select edit UI configurations if you search for that you will have this option here click on that and this is going to pop up a window which is going to allow you to set up a few things for example here you can select the compiler that is actively managing your intelligence another thing you can do is to specify your include paths for example if you are picking up include files from other locations on your system you can specify them here but the most important thing is the C++ standard that we want to use we will be using C++ 23 now Vis Studio code is going to be understanding that this is the standard we are using and it is going to understand our source code if we apply this settings and close that you see that the squiggle is gone now our program is using C++ 23 for intellisense and now this operator makes sense for visual studio code once you apply those settings another file is going to be generated in your vs code folder and it is going to be the CPP properties Json file here which contains our settings you see it is using C++ 23 or C++ if you want to change the standard for the C programming language you can set that up here you can really do all kinds of crazy things this is really all I had to share in this video I hope you found it interesting make sure to watch the next video in this series where I am going to show you how to use a cake to do things better don't worry about this CCH thing if you are a beginner it may scare you off but it is really a good investment for your time if you plan to do something serious with C++ make sure you watch that I am going to put links to these videos in the description below in an order that you can follow them nicely so make sure you check that out and if you like the video why not like comment share or subscribe I am going to stop here and I will see you next time
Info
Channel: LearnQtGuide
Views: 383
Rating: undefined out of 5
Keywords: Visual studio code, c++, build, debug, run, c++20
Id: RWHED9xhElk
Channel Id: undefined
Length: 24min 33sec (1473 seconds)
Published: Sat Jan 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.