Visual Studio Code for C/C++ on Linux (2021)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i'm going to take a look at how to install visual studio code on a linux distribution how to set it up so that you can build launch and of course debug c or c plus plus programs within it it's actually incredibly simpler than doing the same thing on windows so to start off uh usually you want to install the software so here i am on my ubuntu software installation thingy and here i search for code and i can find it it's pretty easy to install just hit install uh if you're on ubuntu if you're on other distributions i'm not gonna go over every single distribution but it's it's incredibly simple uh just google it and it should be straightforward and one more thing uh you have to make sure before doing anything with visual studio code and cc plus plus is that you actually have the gcc compiler installed if you open here a terminal on ubuntu it's by default installed but if you for some reason you don't have it you just say sudo apt install uh build essential and build essential is a sort of meta repository that has all the uh gcc and its debuggers and all the tools you need to actually compile and run your programs so if i do add it here you see i already have it installed now once you have it installed here this is basically what you'll get just a simple uh screen with nothing on it now it might look a bit more different on the sidebar here on my end is on the right yours is probably on the left doesn't really matter uh the most important thing that many people actually get wrong on this editor is to actually create a folder a workspace okay so before anything you go here and you hit open folder okay you hit open folder and i have here for example development folder i'm going to create let's say tutorial free okay just like that so i'm in here in tutorial free i'm going to hit ok and now i have a proper workspace so before that i don't have a proper workspace and certain configuration options weren't available for me like this they are now and you should be able to uh continue now now that we have the actual uh folder and workspace setup let's create a simple hello world that's probably straightforward you've seen this before we're going to create here a new file and i'll say here i'll first save it as main.c main.c so that we know that it's ac file and down here on the right you're going to notice that visual studio code already notices that okay you can uh install an extension that would help you develop this uh these types of applications and actually we do want to install this so you can hit install and it's going to automatically install this extension if you don't get this recommendation what you can do is to go here on the extension and simply search for cc plus plus and it is this one that you have to install just here and there should be an install button and you're done with that you have the extension and that extension is gonna help you a lot when it comes to auto completion and of course setting up the tasks for building and debugging well we'll get there now let's finally create the hello world program that we wanted all right there we go now how do we actually uh build and how do we actually launch this program well first things first uh there is a very simple shortcut key in uh visual studio code and that is ctrl shift b and if you hit that it's gonna try to compile now right now it doesn't have anything set up it doesn't know where it's at what languages are you using and what environment you're at so it's just going to sort of guess and here it gives us some options it's nice but what i want you to do when you get to this screen is to hit on this cog and if you click on this it's going to create you might notice there is now a dot vs code folder here and it's going to create a tasks.json up top and this guy is what tells well what else vs codes to the compiler how to actually compile the code that we have here and certain things are important uh namely we have here the command that's using so it's looking into us usr bin and gcc this is that is where our gcc compiler is located then we have a lot of important arguments minus g allows us to debug the program and we're going to see how to do that in a bit um then this is the actual file this is the current file that we you have opened right so main.c is going to be the one that we're gonna actually compile and then there's the output and output here is the deal name slash the file base name no extension is basically this but without the extension so instead of main.c it's just gonna be main and what i like to do here actually is to change this to uh slash b slash so that it actually has a folder we don't have all the executables in the same folder as the source files all right and uh it's important that you hit right click and create that folder right on the right side here on the left side probably you are here all right now if we have that setup one more thing we need to change to this is the group here so it says group build but what we want is to set it up as default why because right now if we go back to the main.c and if we hit ctrl shift b you'll notice it still asks asks us what do we want to do and i wanted to automatically know that okay well i want to debug i want to uh build a c file so what i can do is just remove this part and change it so that we have an object here adjacent object thing is default set data true and kind is a build is a build task now if we have this setup we can simply hit ctrl shift b and it's going to automatically build it for us and i think that is amazing we now have a build executable and here we can take a look that we have a main executable we can even launch now one way to launch it would be to just go to the terminal so just hit uh it's ctrl grave to actually open the terminal and i can go to bin and here we can see the main executable i can launch it and i'm gonna get hello world that's one way to do it but i want you to be able to launch it through the uh editor itself so you don't have to execute any um any commands in the terminal really to actually launch this so how do we do that well vs code can actually do this um besides the tasks we can hit here f5 to actually launch the project now again vsco doesn't know anything about what we want to do it doesn't even know that it's actually c it just kind of assumes that oh maybe it's actually c and we can actually we may want to launch it here but it's uh it doesn't know so here we can select c plus plus gdb it's important just like gdb because uh that is the debugger that is available for linux we don't have the windows debugger here so of course we want to select this one and here we are to actually select the default configuration and if you click that it's going to automatically again generate a file called launch.json in this vs code or dot vs code folder and this guy is going to be very important now the most important thing here to change is really this program argument and this program is the actual executable that you should launch now this should be in relationship to which file you have opened we'll get to that in a bit so here i'm going to change it to whatever we have here in the tasks so i'm going to simply copy this output fold folder that we get it's really representing this main executable and just paste it there and that's really it now if we hit f5 we can see that the program launches and in the terminal we do get hello world amazing that's that's really nice and it's working there's only one problem if we for example don't have the executable already built right so for example i just removed it let's say we didn't build anything if i hit f5 and want to launch it we're gonna get an error saying that okay main doesn't exist what what do you want to do here right and what we have to tell visual studio code is to actually compile uh the program before trying to launch so we have to go in our launch.json file here and change it here at the pre-launch task property and this property should be the exact same name as you have in the task so this is a a task that's going to launch before well launching the actual uh configuration so here we can go to the tasks of json and copy the label this has to be the same as the one that we passed we paste in there so just copy and paste this here and now even though we don't have a main dot exe or main executable really in here uh if we launch this it's going to first compile and then try to launch it and now it actually does launch and that's really all there is to it now if you try to for example debug the program if i have here let's say an a variable that's five i can just add a breakpoint launch this and we should break in that breakpoint and we can see here on the right you're probably going to see it on the left that yes a is 5 rc is 1 and rgb has my uh back here my path to the executable and if i hit continue it's gonna finish execution so that works beautifully and if you have multiple main files if you really want to have uh a main file for for example for every single exercise you're doing well you can do that you can actually create here let's say main main two of c and let's copy everything we have here in main and i'm gonna say here hello world too just to distinguish between the two if i try to launch this with f5 you're gonna see that it compiles and it does launch our main2.c and inside our folder structure we do get here a main two executable so that's perfectly fine it works amazing now if you want to actually have multiple files compiled at the same time you can but it's a bit more tricky with just this setup you're probably going to have to use make files or whatnot but if you want to have just two files you can still do it it's just that you have to set tasks.json you're gonna have to add another file in here so here this is gonna be replaced with main.c and if you have another uh source file let's say example dot c for whatever reason and let's say we create this example.c let's see and i don't know let's let's define here a function function and if we for example add a printf here hello from example let's say and we try to call it in main.c it's a bit more trickier you can't call it right but you're gonna have to first also tell it that it exists and i have to say here void function uh just the declaration itself and now if i try to launch this i should get that hello from example called here and that's really all there is to launching uh c or c plus plus files in linux in visual studio code uh in the next video i'm going to take a look at how i work with visual studio code and c because it's a bit more different than this but if you're on linux know that it's actually pretty easy and this workflow actually works very very well if you do have any questions uh leave them down comments below or on our discord server again all the files that i have talked about here are going to be available on our website link in the description below alright take care bye
Info
Channel: CodeVault
Views: 24,115
Rating: 4.940136 out of 5
Keywords: codevault, c (programming language), visual studio code, linux, ubuntu, run, compile, c++, code, visual studio, vscode
Id: 9pjBseGfEPU
Channel Id: undefined
Length: 13min 4sec (784 seconds)
Published: Tue Jan 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.