Set Up C++ Development With Visual Studio Code on Mac | VSCode C++ Development Basics MacOS (2024)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how you can set up the C++ development environment with Visual Studio code on your Mac operating system so let's get started and let's see how we can do it so first of all before starting with the development of our C program we need to have the C++ compiler in order to compile our C++ code so that you need to download first and then you can compile your C++ program with your Visual Studio code so for that the simple check whether the compiler is already there or not on your Mac operating system you just need to open the terminal to open the terminal click on Spotlight search and search for terminal here and once the terminal opens you just need to type this command which is C Lang space hyphen Hy py version and then press enter and if this gives you some kind of output which says Apple cang version something then the compiler for C++ is installed on your Mac operating system now if this gives you some kind of uh message which says cang is not recognized or this command fails then you need to install the compiler you can install the cang compiler in two different ways both will lead to the installation of cang and both these ways involves the installation of xcode development tools so if you want to install the full package of xcode which comes with separate IDE and several different uh sdks to develop various different languages then you can go to the Apple App Store and and install the xcode development tools from there I have already created a video about it how you can install that if you are only interested in development of C++ with Visual Studio code then I will recommend you to install the xcode tools using this command which is xcode hyphen select space hyphen hyphen install this is going to only install the essential xcode tools and that also involves the cang so if the cang uh command fails then just run this command when I run uh this command it's going to uh show me this uh error because I have already uh installed xcode separately on my Mac operating system so it says command line tool is already installed use software update in the system setting to install the updates right so in my case I have already have this installed in your case you might want to run this command once you run this command just check the version of cang once again with this command and if it gives you this kind of output then the C++ compiler will work once the compiler is there let's start with the C++ development with Visual Studio code so now open the visual studio code and here first of all we are going to open any folder in which you want to create your C++ program so I'm going to click on open folder and then I will go to the documents folder and here I have this hello world folder which is empty right now so I'm going to open this folder now here I can create a C++ file by clicking on this uh new file icon or I can click on the file and then click on new file icon so I'm going to click on new file icon and then I'm going to name my file as main do CPP here once I uh press enter Visual Studio code is going to recognize that I want to develop a C++ code and how it recognizes because I have named my file as main do CPP do CPP is an extension for C++ so so you can see it gives us the recommendation to install the C and C++ extension pack which is from Microsoft itself so you can click on install from here if you don't want to do that then you can also go to the extensions section and here search for C++ which is also going to show you the same kind of result here you can see the first result which is for C and C++ and you can see it's and you can see it has been downloaded around 57 million times and it's from Microsoft so it's the official CN C++ extension pack for the C++ development so click on install here which is going to start the installation of this extension pack and once the C and C++ extension is installed you will see the disable and uninstall button so in future you can disable or uninstall it one more uh extension I will recommend here will be uh code Runner so just search for code Runner here and this is the extension which will help you to run your code in a easy way so I'm going to click on code Runner and then click on install which is going to install this extension also so we have installed CN C++ extension and we have installed the code Runner extension now I'm going to go to the Explorer and then here type a very simple C++ program so this is the C++ program and what this C++ program does is it declares three variables num 1 num two and sum we assign num is equal to 5 num 2 is equal to 10 and uh then we are going to add these two numbers and show the output here so nothing complicated about this uh C++ program you can find these kind of programs anywhere on internet right so now how can we run this program so to run this program you have this icon here you can see uh this debug C and C++ file you also have this Arrow here from here you can either debug your C your C++ file you have the option to run your C++ code or you have the option to run a specific C++ file using this option I also have the option to run my C++ code using this run button and then I can start the debugging or I can run the program without debugging so let's start with this uh Arrow key and here I'm going to click on this option which says run run code which is going to open the terminal and it's going to show me the output as soon as I click on the Run button you can see it creates this binary file so first of all in the background this run code is going to compile my C++ program and create this uh executable binary file you can see this main file is created and this is the command it has ran and then after that it's going to run this command and you will see the output in the output section right you can also go to the terminal and if you are a fan of command line you can also compile your code from here so let me just remove uh this binary and compile the code once again so I'm going to move this binary file which is the executable file we have created once the C++ code builds so I'm going to move it to the thrash and let's build it once again and I can uh just write C Lang command here so just write C Lang when you type uh first three or four letters of C length you can uh see what are the C length uh uh command which are available for us in our case we are going to use the C++ so I'm going to just write C Lang plus plus and then the name of the file which I want to compile which is main do CPP in my my case then hyphen o which is the flag we can use to name our executable file I'm going to name my executable file as main you can name it anything so you can uh name it out for example let's name it out and this is the command to compile your C++ code using the terminal right so you can run this command on any terminal it doesn't need to be in the visual studio code I'm going to press enter which is going to uh compile my C++ code you can see it has generated this out binary which we can run by using slout and then press enter and it's going to give us the same kind of result so you can also open the terminal and then run this command if the terminal is closed from here you can go to the terminal and then click on the new terminal and it's going to open the terminal for you right now we have uh compiled our code and ran our code now let's say you want to debug your code so you also have the option to debug your code using visual studio code so here you will be able to see uh the option to run your code in the debug mode so you can see debug C or C++ file and as I mentioned you also have the option here start debugging but before starting the debugging you need to set the breakpoint so here you can go to the run and debug section and right now it's not doing anything but when I set the break point so I can just uh click here and then set the break point on line number five line number six line number N9 and let's say line number 12 so you might have observed when I have uh created the breakpoint on these lines you will be able to see uh these kind of list of break points here in the debug section you will also see the line num so 5 6 9 and 12 you can also toggle these break points by clicking on this button which is going to disable all the breakpoints you can enable it once again or you can close all the break points from here so you can remove all the breako by clicking on this remove all break points uh button and you will also see some more um options once you run the debugger so let's run the code in the the debug mode by clicking on run and then start debugging and then it's going to ask us which kind of debugger we want to use you can see you have CN C++ cang build and debug active file you have uh CN C++ cang build and debug active file so we are going to use the second option because we are compiling or debugging the C++ file if you have the g++ compiler you will also see the option for uh GDB or g++ uh compiler I'm going to choose the second option we start with G cang Plus+ click on second option here and it's going to start the debugger and first of all it's going to give you this kind of warning which says developer tool access need to take control of another process so I'm going to just give my macbook's password here which I used to log to my MacBook and click on continue and then click on okay and then my debugging starts once the debugging start you will see these button on top the first button is for continue so when you press on this your program execution stops at the next break point you can also use this step over step into step out and you have the option to restart the debugging and stop the debugging using this button let's step over to the next line so I'm going to to click on step over button and as soon as I do that you can see num one variable is printed here and here the num one the value which is assigned to num one here is five you will also see num two and num three variables but because we haven't executed the code on the line number six or line number 12 we haven't assigned the value to num two and the sum variables so here you will be able to see some random value use assigned to num two and some variables 1 and let's say 1 25 72 these are some of the random values because our program execution is stopped at this point we haven't completed this line of code so when we step over to the next line you will observe that now num two has the value of 10 once again we have stopped at this point so some has random values so let's step over once again and now St some have 15 values so this is how you can debug your code once again when you click on the continue button your program will run successfully and it's going to show you the output here so this is how you can debug your C++ code if you want to create a launch. Json file in the debug section so when you click on debug and you have the option to create the launch. Json file so let me click on this file and it's going to create this launch. Json file for you so once it creates a launch. Json file you you will see that this vs code folder will will be created in your project directory and you will see these two files which are created which is launch. Json which have the configuration related to your uh debugger also you have the task. Json file have the configuration related to your your compiler right so you can see this task we have added this task here which is generated uh automatically for you when you ran this code using this button right so this kind of uh vs code and launch. Json and task. Json uh are important when you want to share your code with other colleagues so when you are using Version Control let's say when you click on Source control and when you add this this uh code into some kind of Git Version Control then you will be able to see your changes here and when you push your code to GitHub some other developer is going to pull that code and he will be able to use the same configuration that's why it's useful to create this kind of uh launch or task. Json files so that's it for this video this is how you can set up the c or C++ development environment on your Visual Studio code editor on your Mac operating system I hope you enjoyed this video and I will see you in the next video
Info
Channel: ProgrammingKnowledge
Views: 9,783
Rating: undefined out of 5
Keywords: Can I use Visual Studio Code for C++ on Mac?, How do I set up C++ on my Mac?, How do I set up Visual Studio Code to compile C++ code?, How to run C program in Visual Studio Code on Mac?, How do you set up VS Code on a Mac?, Can Visual Studio Code compile C++?, How to setup C in Visual Studio Code?, Which is better GCC or Clang?, How do I run cpp in VS Code terminal?, How do I run a cpp project in Visual Studio?, Mac, M1, M2, M3, MacBook Pro, MacBook Air
Id: v2_Pth8MrKA
Channel Id: undefined
Length: 15min 36sec (936 seconds)
Published: Fri Jan 26 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.