VS Code WSL (C++) - Create Project, Compile, Run

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 vs code under windows using the windows subsystem for linux it'll be a brief tour linking to a few guides and showing you how to get started so to begin with i'm using visual visual studios code under windows so i'm going to download that and i'll probably download the user install 64-bit version i've already done that and installed it and then the next thing you'll want to do is install windows subsystem for linux microsoft has a nice guide for that i'll put the link in the description down below and you can follow that they've actually simplified it recently and now it's just one single command in powershell windows subsystem for linux wsl space minus minus install you do this from a power shell and type it in i've done it already and installed it it'll install windows substance for linux says 2 as a default you can also configure it to one if you wanted to anyway you get that installed and then once you've done that it'll install ubuntu for you as a default package a default kind of yeah bundle and that'll let you get opera up and on running once you've got that installed you get a nice command prompt there then the final thing is you can follow the guide that walks you through how to use windows subsystem for linux with vs code and that's going to be what i'm going to show you how to do right now so to begin with i'm going to create a folder that i want to work inside of i'm going to do this under windows i'm going to create a new folder here and i'm going to name this one i don't know compute 1 30. if you're taking a course like 130 and this would be a folder you want to work inside of so this is under my documents i've got a web just a single file a standard file manager here and i'm going to put it somewhere i wanted to now let's go and open this in vs code and put some code there so i'm going to run vs code uh this is the start menu it popped up off screen as i'll bring it on the screen here okay so now this is vs code it's in windows and it's sort of just a normal windows thing it's not yet working with windows subsystem for linux the first thing i'll need to do is i'll need to install an extension pack for that so i'm going to go on the left hand side and i'm going to click extensions setting here and i'm going to type in windows subsystem for linux i think i can just type in wsl i can say this remote wsl i've already got it installed which means it's probably the right one and this will allow me to connect to the windows subsystem for linux kind of running inside of windows so that's exactly what we need so now that i've got that let's open the folder that i want now to do this i'm going to tell it to kind of go into windows subsystem for linux mode i need to connect to this kind of remote connection down in the bottom hand left hand corner this green button it says open a remote window that's what i want so i'm going to click open a remote window and then open a folder in windows subsystem for linux so let's do that i'll take a moment and it'll pop up here and you'll note here that it's kind of already browsed to this wsl this is what's being shared or mapped off of my drive because i've got wsl installed and i could open anything up in here that'll be under the right folder for wsl but then getting the content off of there is a bit trickier i've got this folder i just created under my my documents so let's go to that and where to go under c here we go it's empty 130. i'm going to say open folder and now you'll note that my window has changed it's got a blue bottom and it says here wsl ubuntu so running in ubuntu wsl i'm in wsl1 that's fine so that's the big step that i need to do if it doesn't say that at the bottom always check if you're trying to do wsl check at the bottom see if it mentions that now the next thing i want to do is i'm going to create a folder and organize my tasks so i'm going to right click in here new folder and let's call this lab one imagine we got a lab we're gonna do i opened up there's no file here so i'm gonna right click the folder say new file and let's say main.cbp so i got my main program let's write in our usual hash include nclude iostream using name namespacestay and then int main and see out hello world of course and again inline on that i need it and return zero now the return zero reminder just sends a message back to the operating system saying my program finished okay so now i can go through from here and i can work on install and building things um what you might need to do and i'm going to run i'm going to run ubuntu so i'm just running the ubuntu command which brings me up an ubuntu prompt you might need to install the compiler you can check if you've got a compiler installed i'm running gc i want to run g plus so you can run g plus plus and it tells me here okay it tried to run it didn't find anything i'm gonna make this bigger there we go if instead if it didn't find the program it gave me an error like command not found i want to type in i want to install it so i'm going to type in sudo apt get install g plus plus i've already done this type in my password and it's done it for me already you might need to do this in order to make this work there may be a few other commands you have to install as well but you'll find that out as you go so the first thing i want to do is i want to build this so i can go up to terminal and i can say configure default build task and i can pick one of these i happen to have a number of different c plus plus compilers installed and i want to pick gcc here so i want to build active file under gcc there'll be a number of different gccs it won't probably matter which one and it goes through it configures it all one thing i like to do is i like to switch up my errors and i like to tell it to kind of treat some things as errors in my code so i'm going to uh inside of this list of args i'm going to add in i'm doing it here at the top why not i'm going to ask and a minus w-a-l-l with a capital w or s lower case this will treat this will show me all warnings and i'm going to set them to be errors as well w-e-r-e-e-r-r so this will set all errors to be displayed uh or permitting all warnings to be displayed and treat those warnings as errors so basically stop building so i'll do that i'm going to save this file i'm going to go back to main i'm going to go up to terminal and i'm going to say run build task so this i'll try and build my program we can see here that it's executed the g plus plus compiler and it worked now i want to prove it really works so i'm going to go and add a syntax error in my program i'm going to get rid of a semicolon i'm going to redo that build so run build task and we can see that it now fails i can control down control and i can click on this line and it will show me it'll tell oh hey do you want to take this location i'm going to hit enter no matches found i'm not sure why it's not working on that i'll have to look into that later oh um nope i'm not sure why actually i say maybe if there's a space in my path but there are no spaces in the path that i've got here well that's fine um if i click on the other one does that work hmm i'm not sure why it's not picking it up for me but that's fine so it normally would take me up here to the right place i'll add my semicolon back in ctrl shift b is the hotkey and so i'll rebuild it and it's in here okay so that's how i can build it and the last thing i want to show is how to run it so if i go up to terminal and start debugging well in fact there's two ways to run so now that i've built it here on the left main i can click on terminal new terminal and we can see that i'm in the folder the root folder i wanted to get into my lab so i'm going to ls to show you this is lab 1 by folder i'm going to cd space lab i could type the whole thing or type a little bit hit tab and it auto completes i'm in there now we can see these are the files i can type dot slash main is the program i want to run and there it is i can run it in the console or the terminal this is a good way um but i want to do it through the debugger as well so i'm going to go back into my main here i'm going to go to run start debugging i can pick what environment i want to run through i'm going to select the gdb ldbdb lldb and then build the active file that's good similar as before and this is going to come up with my configuration now initially it doesn't actually run my program it just builds me all this configuration about this launch file i'll show you where it is if i go on the left you go to explorer view in this vs code i've now got tasks which is where i configured my build and launch which is what it's doing now so i'm going to close both of those we've now got it configured i'm going to do that again i'm going to say run so back to my code f5 and run it goes through it builds it and then it runs it here it is well let's put a breakpoint in and prove what's going on here i'm going to set a breakpoint by clicking on the left here and i'm going to go to run start debugging and now i hit the break point we can see on the left here that i'm in the debug mode and i can start to run through the debugger i can see what's happening i can step over this line of code i see the output and carry on a note that it's way much better to use visual studios code under windows inside of wsl rather than using something like mingw because of this integrated debugger works much more seamlessly okay i think that's about all i wanted to show here thank you very much for watching happy coding
Info
Channel: Brian Fraser
Views: 32,581
Rating: undefined out of 5
Keywords:
Id: NY5izJWXi0U
Channel Id: undefined
Length: 10min 2sec (602 seconds)
Published: Mon Jan 17 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.