Install and Run C++ in Visual Studio Code (Linux)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello i'm dr brian fraser and in this video i'll be showing how to use visual studio's code with the gcc compiler in fact the g plus plus compiler uh for writing c plus code under linux so i've got a linux in my case a virtual machine up and running it's running ubuntu and we're going to walk through the steps on getting gcc in as well as visual studio's code so the first thing i want to do is i want to bring up a terminal where all good linux happens and from here i want to run the apt-get so i'm going to do sudo to run and something as the super user so super user do i'm going to run apt get update and then type in my password so for this to work i need to be this a super user which is to say have kind of root permissions um don't do this on some managed computers like in the lab for example where you won't have super user access but if you installed the ubuntu yourself then you will have access to it so it's going to go through right now and it's contacting the web basically and i've already updated my system so that's fine that just basically kind of updates all the packages that it knows about next i want to install all of the things to deal with um c plus plus so i'm gonna do sudo app to get install and i'm gonna list the things i wanna install so i'm gonna install build essentials and uh gdb and i'm also going to put in uh gcc i think i want to put in our g plus plus it's going to list what it's going to install and so forth i'm going to say yes i want to take that and now it's going to pull down from the web all of these packages that it said it's going to install of which it's going to take about 50 extra megabytes while that's running i'm going to kind of in parallel do something else so i'm going to launch my file manager and it automatically defaults to the home folder so i'm going to create some folders here that i can then work inside of so i'm going to right click say new folder and let's call this one cmpt130 for maybe the course i'm taking and i'm going to go inside of that and i'm going to create a new folder let's call this one i don't know hello flow test so what i might do is inside of this cmpt 130 folder have a bunch of separate folders for different labs labs one lab two lab three assignment one assignment two anything i want i can create a whole bunch of separate folders in here to help manage my projects okay so we'll come back and use that in just a moment i've now got gcc installed i can prove it's installed into a g plus plus minus minus version and it will tell me the version of the software that was installed i should also have gdb gdb minus minus version and gdb is the gnu debugger so i have my c plus plus compiler that's g plus plus and my debugger install that's great now the next thing i want to do is i want to bring in visual studios code now to get that i need another tool called snap so sudo apt get install snap snap is a package management system kind of like apt-get except um kind of has a bit more i'll say flexibility for after the fact installs it's not quite so it's such a managed approach so now that i have snap i can do sudo snap another way of installing things so install i'm going to use classic here which allows it to do kind of many of the things that it wants to do it gives it more permissions when installing software and the package name is code for visual studios code so it'll download it and install visual studio's code for me okay so we've got code installed let's make sure that actually is going to work for us so i'm going to open up visual studio's code and i can do this in a number of ways while it's still installing i guess but i can do it a number of ways i could launch visual studio's code and then tell it to open the folder this compute 130 folder but in fact what i will do is i'll do it all from the command line because well a lot of linux happens from the command line so i'll demonstrate a little bit of what you can do from the command line there we go so if i just type cd enter it takes me to my home directory pwdl list kind of the folder i'm in i'm in slash home slash brian i'm going to go into that compute 130 and i'm going to hit tab here to do tab complete so i don't have to type the whole thing in and so here's that 130 folder i just created a moment ago um so from here i can launch visual studios code i say code and then period that says load the workspace from the current folder which is my the folder i'm interested in having everything sit inside of incidentally you could make this folder in git lab or somehow some managed location so that your code is shared with other systems as appropriate okay so now i launched in it comes with the welcome screen i'll skip that over for the moment and here we are we have hello world with nothing in it so let's create a new file let's say a new file here and let's call this on hello.cpp and i'm going to type in my standard hello world so hash includes using namespace std uh int main loop and then c out hello world of course and line and return 0. hello world is my standard program to start with now it's going to ask me down here it says do you want to install this c plus extension i do in fact i could click install from here but i'm going to show you how to do it through the ide just to give you a bit more of a feel for it on the left hand side allows me to pick what gets shown here on the bar on the left we're already at this explorer pane if i click it again it goes away this is where i'm going to want to normally be but i'm going to go down here now to extensions so under extensions it gives me a list of some popular ones about python and well here it is c plus plus c plus plus the microsoft intellisense and debugger support so i'm going to click install it will install and nicely not even require a reboot it's currently going to go through and update the extensions for me allows me to do some insider i don't need a new version that's fine get rid of that i'm good i'm going to go back up here to explore now let's see what i also want to do i want to enable something here under file i'm going to set i'm going to turn on this auto save which will automatically save the changes at important points which is nice because in linux i'm going to often be leaving this editor to go do something else now we're going to try to do as much as we can through this integrated development environment somewhat but sometimes you move to another file place in the file system we don't want it to not save our changes so let's run this program now i can go up here to run and i can say start debugging it doesn't yet know exactly what to do though i have to tell it what compiler to use and what sort of tool chain in order to fix get this working so i'm going to check here gdb is the first one and then it asks me oh well which of these different sort of variants do you want to support and i want to take the first one that says g plus plus just standard g plus plus there's a couple of them listed here not entirely sure why i'll just pick the first g plus plus without a version number and it builds me this launch dot json file json is a file format that just sort of structured data can be stored in and if you want you can find all the details about this online i don't really care about the details here other than it's simply going to work i'm now going to go back to my hello.cpp i'm going to go back to run or and start debugging or i'll just come over here and hit f5 it launches the compiler we see here the compiler is going through here and then it launches the debugger and runs my program and if i look here on the terminal there it is hello world that's the output of my program that i just ran let's prove it is c out yep it's the output this end line gives me a line feed i'm going to hit f5 again to run it rebuilds it we see here and there it is my new output okay so we've got the program up and running now what's kind of cool about visual studio's code is that inside of this whole workspace if i'm looking at a dot cpp file i can hit f5 and it'll build that file for me assuming it's a fairly simple project so i'm going to right click on this i'm going to copy and let's create a new folder i'm going to come back up here and say i want a new folder and this folder i'm going to call something like let's say lab 1 and i'm going to move it up into the root of this i don't even do that oh i could do this pull it down make sure you want to move it yes so i moved it into the root of my project then i've got lab one nothing's there so i'm going to paste in hello and let's rename it so i'm going to hit f2 and let's close one goodbye good bye over and out and now i'm looking at a different cpp file i hit f5 i don't need to tell it to reconfigure the project it simply loads that information from the launch.json file and there we have my program ran over and out all right um so this is going to be a great structure throughout the course because you can have all of your assignments and so forth in one spot down the left-hand side here nicely organized by like labs and assignments and so forth recall though that under linux and in fact c plus plus in general it is case sensitive by identifiers as well as file names and so make sure that you're typing in consistent file names don't capitalize some and lowercase others generally they're going to be lowercase file names uh the last thing i want to do is i want to show you some possible things that could go wrong of which there are many uh first one is if you happen to select the launch configuration incorrectly and this doesn't work you can simply go over here to the left hand side there should be this vs code folder if you open it up and there's this launch.json you can just delete it right click and then delete i'm not going to do it here because mine worked but if you've got the wrong one do that try it again hit f5 and you'll get a chance to select a different option if it's not working at all go back to the command line and make sure you type g plus plus you'll get something type gdb minus minus version you'll get something else make sure those programs are installed the next thing is if you get a compile time error so let me go back in here and i'm going to do the standard one about forgetting to put in a semicolon here at the end so now when i hit f5 it's going to try and build and then run my program well the build fails and so that's what it's saying here i it failed to do this build active file task so what can i do i can debug anyway saying i know it didn't build but debug it anyway that's foolish because i want to debug the real thing here i could say i want to say abort or show errors in fact it turns out to be quite nice so if i say show errors it lists here the errors that it detected and it thinks well here's one expected semicolon before line there oh yes right semicolon back in the file will save when i run if i hit f5 it'll run my program now we're back up and running the last thing i want to mention is by running it through the debugger it's actually pretty powerful if something goes wrong now one of the common things to go wrong is called a seg fault so we just quickly write one i'm going to create an in pointer called p height equals null putter and then i can do something like dereference it star [Music] height now don't worry if you don't know what this is yet you'll certainly learn about no pointers before the end of your programming career but suffice to say when you do something wrong with memory access it fails horrifically and we wanted to do it smartly so this line of code is not going to work we're basically saying hey here's an address it happens to be address 0 which we don't have access to ever and i'm going to look at address 0. basically i'm trying to look somewhere i'm not allowed to do that the operating system is going to stop me with what's called a seg fault a segmentation fault and here we see in fact that visual studio's code along with gdp were able to tell me precisely where that fault was and so it was on here line 9 it hit a seg fault i can still see some of my output terminal out whatever has happened up to this point but now it's telling me precisely where in my code it died which is a very powerful way of quickly debugging your code okay thank you very much for watching you
Info
Channel: Brian Fraser
Views: 88,931
Rating: 4.9103918 out of 5
Keywords:
Id: BEJUdkPemYY
Channel Id: undefined
Length: 13min 42sec (822 seconds)
Published: Fri Sep 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.