Run C++ and C in Visual Studio Code | Mac and Windows!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody my name is Caleb and in this video we're going to talk about how to code in C or CPUs Plus in Visual Studio code now the important thing to realize is that vs code is just a text editor it doesn't care what language you're writing you're going to need surrounding tools specifically a compiler which will take C or C plus plus compile that into an executable program and that's really what we're going to be talking about in this video but we're going to do that within the context of vs code so that you can run your C and C plus code very easily I want it so that anybody following my C or C plus plus content can follow along so we need an editor where you can run code that's the basics and then pretty much it's all going to be the same step one of this is to get Visual Studio code go ahead and download that I'm currently on a Mac but you should be able to get this on Windows or Linux as well once it's downloaded go ahead and go through the installation once you have Visual Studio code it's going to look something like this and to start writing C plus plus code I'm going to suggest that you go to the extensions Tab and this will allow us to download an extension to easily work with C and C plus plus you can see that there so you can go ahead and search C C plus plus and you'll see this one here you will want to go ahead and install this another extension I'm going to suggest is called code Runner which I have installed here this isn't required but this is going to make running that code a little bit easier now to get started we're going to create a file so we'll just go ahead and say file new text file and save this so command s or control s on Windows we're going to give this some name we'll just call it example and if this is a c program it'll typically be dot C if it's AC plus plus it'll be dot CPP so that's what I'm going to go with but it's going to be pretty similar if you're on C instead of C plus plus no big deal now you just need to write a valid program so let's go ahead and say include i o stream int Main return zero and the line that we will have this do is just to see out which says hello world and then we'll have an end line so looks like that Okay cool so we have our C plus plus code the goal is now to be able to compile this so what we can do is open a terminal with terminal new terminal and you will say G plus plus if it pauses for a second and says something like this error no input files you're most likely good to go you have G plus plus which is the C plus plus compiler similar for C which would be GCC and a size the same thing so you can see I have both of those on my system now I'll talk about Windows in a moment but let me talk about Mac first if you type in G plus plus and you don't have it installed it's most likely going to prompt you to install the xcode developer tools and one of those tools is this compiler if it doesn't prompt you to do that I believe you can get it from the App Store possibly using this xcode download here or alternatively there's a command in the terminal you can say xcode select dash dash install and that will cause that download to start and just so you know this can take a while so please be patient with it now if you're on Windows I want to reproduce that environment as closely as possible and we're going to use a tool called Min GW which will include those compilers and a nice terminal so go ahead and open a browser and head to msys2.org and download the installer and I'm going to go ahead and install this if it will let me so we will keep that software oh yes now I don't use Windows a whole lot so there might be better ways of setting this up but this is what worked for me it'll go through that installation process let it go through that installation and then once this is done we'll just say run and finish and once that's up we will just type in a brief command to install all the needed tools that command will look like this all right so here's that command that we're going to need this is going to install all the needed tools and just leave the default to install them yes a lot of these steps have been inspired by the visual studio guide so if you need to copy those you can find this guide alright this is done so you may need to add the compiler to your path so to do that you can go down to the search type in environment variables and then environment variables select the path and then edit and you want to make sure you have the Ming w64 slash bin path in here what this will do is it'll add the commands to the path so we should be able to execute them right in a new terminal by saying G plus plus and you can see we get that error we saw earlier when we tried this on my Mac system and then also GCC and also G DB which is a debugger but we're not going to be needing that but just letting you know that that is there how do you get out of this there we go now if you're on Windows you should be able to open Visual Studio code and the rest of the tutorial should be the same now within Visual Studio code we can say run C C plus plus file and you'll get this drop down here to choose which tool you want to use we're going to use the G plus plus compiler or GCC for a c program so we'll select that what this will do is build or compile the code now from a new terminal you should be able to run the compiled version with DOT slash example and you can see the output hello world and we can try changing this by you know changing the text hitting G plus plus build and debug you may also see the output here in the debug console there but I generally just prefer to use the terminal so you could run it here and there is our change now going through this you know hitting this and selecting this it can get a little old you can make it a little bit easier if you open a folder so for example if we say open folder I'll just create a folder as an example here and within here we can create our c plus file let me just write a quick C plus file a stream in Main return zero this is a new file so now that we are in a file we can say run C C plus plus file choose G plus plus some stuff pops up we can just ignore most of that and you can see it creates this vs Code full file uh hidden folder here with this task.json that is where the actual compile command is defined but now it should be able to read that and allow us to just hit that play button without having to select what compiler we want to use every single time so you could create a new terminal split that off into the side of the build output like this when you make a change you will just hit play and then whatever the name of the file is with a DOT slash before it there you go anytime you make a change just do that again real simple and you see your output now the code Runner extension I talked about earlier I'm going to show you how to use that real quick and what that's going to do is it's going to allow us to immediately do both of those things and get that output here which can be a little bit faster if that's what you prefer you may need to do some configuring so you can go to your extensions and find code Runner with a quick search code Runner and then go into extension settings and anything that you're interested in is going to be in here so for example the option to run it in a terminal run in terminal I have that checked so it shows up in the terminal instead of in the output just so I can get user input a little bit easier but if you uncheck that what that's going to do is it's going to run in the output which actually gives quite a nice looking output so that's up to you if you want that now say you're working with something that might be a C plus plus 11 feature let's say we said include vector and we wanted to use some initialization features we might say Vector of type int items and we will assign this one two and three this is going to show up as a problem probably because I need to prefix this with standard all right so far so good if we want to run this the first way run C C plus plus we get a problem now cannot be initialized with an initializer list I know this is valid because you can compile it manually saying G plus plus and then we'll say dash dash standard is C plus plus 11. example.cpp it doesn't give us any errors and then we can run it using dot slash example so to fix it with the automatic running button we need to go into our task.json file and just change the compiling command so here's the command and then the arguments so we could just add another argument in here where we can say Dash Dash standard is C plus plus 11. now head back over to the C plus file run it it no longer sees that as a problem and we are good to go this similar thing is going to happen with code Runner so if we say run code ah that seems to be working but that's because I already had it configured you can find a setting within settings for code Runner executor map edit in settings.json and you can configure the C plus plus command to use whatever standard you want in this case it's dash dash standard it's C plus plus 20. so by default you may not have that so you can go ahead and add that in and it'll compile correctly it's quite the process to get some code running but hopefully at this point we're all on the same page and we can execute our c plus code inside of Visual Studio code hopefully that's helpful stay tuned for the upcoming C and C plus plus videos if you want to be notified of my upcoming c c plus Master course follow the link down below to sign up for my newsletter thank you I'll see you in the next one peace out
Info
Channel: Caleb Curry
Views: 82,841
Rating: undefined out of 5
Keywords: python, tutorial series, tutorial, programming, code, coding, caleb, curry, intermediate python, intermediate, how to
Id: 3-9sObAg6R0
Channel Id: undefined
Length: 10min 56sec (656 seconds)
Published: Thu Mar 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.