How To Use VS CODE for C++ | With CMake & Any Compiler

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys i'm here to bring you a top secret video of things you shouldn't even know but you're gonna know anyway hooked okay i'm trying to give views all right we're gonna make a c plus program with visual studio code this is a question i get a lot and it's how do you manage a c plus plus project install the build run and get all your buttons and stuff with visual studio code and uh yeah okay i guess my camera's reflected i don't know where i'm pointing all right let's launch visual studio code we're gonna do it so pre-requisites you're gonna need some kind of c plus plus compiler it doesn't really matter what kind um i'm gonna use the visual studio one which is ms build and i have a previous video that explains that some in section three i'll try to remember the link up there so if you want to know more about that you can but uh we're also going to be using cmake cmake visual studio code and that's basically about it and we're just going to say all right let's make a new file select the language okay c plus plus so we're starting from scratch oh my gosh a file let's just make like a main uh of course if you're writing software you wanted to do something so that's up to you to decide what you want to do with the software we're just going to make a hello world just to get a base case up and do it and when we go to save it i just press ctrl s i need to give it a spot and this is where we're going to start our project so i'm going to go into one of my storage drive source make a new folder and call it youtube vs code c okay that's like a really long title all right go into there name this file main rather than untitled one and there we go we now have a main so what's next let's go to open a folder and we're going to open this one we just created that way it's going to give us the directory over here you could do this in reverse order you could create the folder and then go to open folder and then start doing your stuff however you want to do it's fine ah sip of coffee fellas if you'd like to support me and help buy me coffee you can do that and i would appreciate it all right here we go let's make a cmake list on text and with this one of our extensions should kick in so we're going to talk for a second about extensions the only one i am using is the microsoft cmake one here it is cmake tools by microsoft that's like the only extension i'm using you can use more if you want but i'm not going to make this real complicated and use a bunch of random ones just the microsoft c make tools and i have intellisense from the microsoft one as well so we need to design a basic cmake and we can just type it in to make no suggestions so we want this to be detected as a cmake project we're gonna hit the command ctrl shift p and that brings up this whole command thing for visual studio code we're gonna start typing in cmake and there is a cmake quickstart so we're just gonna click that it's just going to fill some code and advance for us and we can choose what kit we want we're just going to use release 64. and it says we already have a cmake file okay apparently you can't do that when you already have a cma file i just want the starter code and if you can't get this quick start to work or for whatever reason you can just manually type in all this stuff you're about to see making an executable okay so here's all you really need you need the minimum required you need a project name uh version's optional cool gives us a version uh testing if you want we're not going to do testing right now all right and we have a cpac installer but really all we want right now is this executable the rest of this we're not going to worry about i have tutorials on all that stuff in my cmake series if you want to check that out but we just want to get the base case going all right so we have this we have a main this should already work but all cmake does is it configures your build so it looks like it did that automatically if it doesn't do it automatically you need to go to your terminal and of course do the cmake commands that you need but by default this extension that i'm using the microsoft cmake tools will do some of this stuff if you enable it so we do know that once it's ready to be built we can build it and the way we build it is we use a compiler or we use the make file so if we go to this build we'll see that it has a bunch of visual studio stuff in it zero check run test package nightly experimental i've turned off all this so dart c test yeah we don't need to worry about all that the only thing we actually want is this one right here so this actually might be a bit overkill using this whole start thing but in any real project you're probably going to we want to use all those other features anyway so it's fine so we have the solution and we have this project we basically need to compile those there's a couple different ways you can do it uh the first way is probably the default way you want to open this directory so right click on the file reveal and explore and you'll see it here and you can open this up with visual studio and compile it it's going to have all the settings from cmake that you specified see it over here and right click build put it built see down here that it succeeded okay now we can close this close this so we have our out file it should be in debug there it is there's the executable so how do you launch this well you can launch it from your terminal just by doing you know the dot slash there we go yeah you do need to die all right so hello world we ran it but what if you just want a button well you can do that we got a little run and debug over here ctrl shift d and it creates a json file when you try to run it so let's go over this well let's create our json and it asks you what environment we're going to say windows because we're on windows and it gives us this default windows launch what we want to do here is basically make this go to our executable now we can delete all this first text but we have a workspace folder that's the folder of our project and we know we went to build and then in this case debug the name of our executable is youtube youtube vs code cp so now it should launch the right ones go back to the run and debug hit the run button and it launches in a terminal cool and you can set up any amount of configs you want if you have if you want special debugging once for gdb or if you want to attach it to a process you can do that it's a little more advanced we're not going to talk about that here so what next what if you update your program and you want to recompile or maybe you want to see more options you can do with these configs for example in this console you just delete this and hit control space to bring up this uh fill out thing you'll see some options integrated terminal it's going to launch it in the terminal down there so we hit play and we'll see hello world right here in visual studio so let's talk about one other thing and that is just rebuilding and making changes all right say let's just add like another let's just make a function here just put the hello world in it so essentially the same program just made some changes so that we have to recompile we'll save from function so it's obvious that's using the new one that we built and yeah we should be able to just compile this and re-run it but we can't just compile it from visual studio right we have to regenerate the cmake well if we added a room files would have to regenerate cmake but mainly we got to open up this project again and rebuild it with visual studio right and that can be a pain so you reveal open it yada yada or you can just use your terminal here and go msbuild.exe which is essentially the same thing the visual studio community edition does and then path to this project which is let's use forward slashes and no leading one okay that worked fine so what that did was just build our software so you can just do it like that you can just run ms build and it's going to build it with the config that you have specified in your cmake file basically whatever you generated that project with and if you're using a different compiler and generating for like ming gw or clang or something well you'll know those commands but essentially you can do the same thing can you turn this into a button that builds i am not entirely sure i couldn't find a way there's probably add-ons that do it but i don't want to get into trying out a bunch of add-ons all right so this is for a console program hopefully this answers all your questions about how to get a c plus program up and going with visual studio code get the run button so that you can just launch it and how to build it how to set it up with some basic stuff let me know if you have any questions and there's another version of this too where if you're launching software that has no terminal like windowed only and like it's uh i don't know i'll do it i'll do another version if this gets like 50 likes so smash it and smash my channel up into the sky yeah subscribe for more cheesy puns and more code see you guys next time like and subscribe so i know to keep making these kind of videos you
Info
Channel: Code, Tech, and Tutorials
Views: 56,449
Rating: undefined out of 5
Keywords: c++ visual studio code, coding, learning, howto, tutorial, guide, self improvement, what is programming, c++ programming, English, software development, vscode, what is coding, programming, coding for beginners, visual studio code, c++, vs code
Id: gGxi500Q5uE
Channel Id: undefined
Length: 9min 13sec (553 seconds)
Published: Fri Nov 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.