Set Up C++ Development With Visual Studio Code on Mac | VSCode C++ Development Basics MacOS (2024)
Summary
TLDRThis video tutorial demonstrates how to set up a C++ development environment on a Mac using Visual Studio Code. It walks through checking for an existing C++ compiler (Clang), installing it via Xcode command line tools if necessary, and configuring Visual Studio Code by installing the C++ extension pack and Code Runner. The video covers writing a simple C++ program, running it, and debugging with breakpoints. It also explains how to use tasks and launch configurations for easier collaboration using Git and GitHub. By the end, users will be ready to develop and debug C++ programs on their Mac.
Takeaways
- 😀 Check if the C++ compiler (Clang) is installed on your Mac by using the 'clang --version' command in the terminal.
- 💻 If Clang is not installed, you can install it by either downloading the full Xcode package from the App Store or using the 'xcode-select --install' command to install only the essential development tools.
- 🔧 Open Visual Studio Code and create a new folder for your C++ project, then create a new file with the '.cpp' extension to signal that it's a C++ file.
- 🔍 Install the C++ extension pack from Microsoft in Visual Studio Code by going to the Extensions section and searching for 'C++' to enhance the development experience.
- 🏃♂️ Install the Code Runner extension in Visual Studio Code to easily run your C++ code from within the editor.
- 📜 Write a simple C++ program to declare and add two numbers, and use the 'Run' button in Visual Studio Code to compile and run the program.
- 🛠 You can also compile C++ code directly from the terminal using the Clang compiler and the 'clang++' command.
- 🐞 Set breakpoints in Visual Studio Code to debug C++ programs by stopping execution at specific lines to inspect variable values.
- ⚙️ Visual Studio Code allows you to step through code line by line using the 'Step Over' and 'Continue' buttons in the debugger.
- 📝 You can create 'launch.json' and 'tasks.json' configuration files for debugging and compiling to share your setup with other developers via version control.
Q & A
What is the first step before starting C++ development on Visual Studio Code for Mac?
-The first step is to ensure that you have a C++ compiler installed on your Mac. You can check for this by opening the terminal and typing the command `clang --version`. If the compiler is not installed, you will need to install it.
What should you do if the command `clang --version` does not recognize the C++ compiler?
-If the `clang --version` command fails or the compiler is not recognized, you can install it by using the command `xcode-select --install`. This will install the necessary Xcode tools, including the C++ compiler.
How can you install the full package of Xcode on a Mac?
-You can install the full package of Xcode from the Apple App Store. The full package includes an IDE and SDKs for multiple languages, including C++.
What are the benefits of using the minimal Xcode tools installation instead of the full package?
-The minimal Xcode tools installation only includes the essential tools needed for C++ development, making it lighter and faster to install, especially if you're only interested in using Visual Studio Code for C++ development.
How do you create a new C++ file in Visual Studio Code?
-To create a new C++ file in Visual Studio Code, open a folder where you want to save your file, then click the new file icon and name the file with a `.cpp` extension, such as `main.cpp`.
Why does Visual Studio Code recommend installing the C++ extension when you create a .cpp file?
-When you create a `.cpp` file, Visual Studio Code recognizes it as a C++ file and recommends installing the C/C++ extension pack from Microsoft to provide language support, such as syntax highlighting, IntelliSense, and debugging tools.
What is the purpose of the Code Runner extension in Visual Studio Code?
-The Code Runner extension helps you run code easily within Visual Studio Code. It allows you to execute your C++ program directly from the editor without needing to manually compile it from the terminal.
How can you compile and run a C++ program using the terminal in Visual Studio Code?
-To compile and run a C++ program using the terminal, you can use the `clang++` command followed by the filename (e.g., `clang++ main.cpp -o main`). This command compiles the C++ code and generates an executable file, which you can then run by typing `./main`.
How can you debug a C++ program in Visual Studio Code?
-To debug a C++ program, you can set breakpoints by clicking next to the line numbers, then click on the 'Run and Debug' option. Visual Studio Code will prompt you to choose a debugger, and after selecting the appropriate one (e.g., `clang++`), you can start the debugging session.
What is the purpose of creating the 'launch.json' and 'tasks.json' files in Visual Studio Code?
-The `launch.json` file configures the debugger, while the `tasks.json` file configures the compiler settings. These files are useful for maintaining a consistent environment when sharing the project with other developers or using version control systems like Git.
Outlines
🛠️ Setting Up C++ Development Environment on Mac
This paragraph provides an introduction to setting up a C++ development environment using Visual Studio Code on a Mac. It explains the need for a C++ compiler (like clang) to compile the code and how to check if the compiler is already installed via the terminal. If not installed, users are guided to install Xcode tools by either downloading the full Xcode IDE or only the essential Xcode tools through a specific command (`xcode-select --install`).
📦 Installing Necessary Extensions for Visual Studio Code
The focus here is on setting up Visual Studio Code for C++ development by installing the necessary extensions. It explains how Visual Studio Code automatically suggests the C++ extension pack when creating a `.cpp` file, which is critical for C++ development. It further recommends installing an additional extension, Code Runner, for simplifying the code execution process. The paragraph concludes by showing how to write a simple C++ program and execute it using the installed tools.
🐞 Debugging C++ Code in Visual Studio Code
This section highlights the process of running and debugging a C++ program within Visual Studio Code. It explains the different methods to execute the code, including using a run button and debugging options. Users are shown how to set breakpoints to pause the program execution at specific lines, step through the code, and observe the values of variables as they change during the debugging process. It also covers how to compile the program using the terminal and run it with the generated executable file.
🔧 Configuring Debugging and Version Control with VS Code
This final part introduces how to create and configure `launch.json` and `tasks.json` files for debugging and compiling code automatically. It shows how Visual Studio Code generates these files when running the C++ program and how these files can be used when sharing the project through version control systems like Git. The paragraph concludes by emphasizing the usefulness of having such configurations for easier collaboration with other developers.
Mindmap
Keywords
💡C++ Compiler
💡Visual Studio Code
💡Xcode Development Tools
💡Extensions
💡Command Line
💡Debugger
💡Breakpoint
💡Task.json
💡Launch.json
💡Terminal
Highlights
Opening the terminal on Mac and checking for the C++ compiler by typing the command `clang --version`.
If the compiler is not found, you can install it using the command `xcode-select --install` to download essential Xcode tools, including the C++ compiler.
Downloading the full Xcode package from the Apple App Store is another method to get the C++ compiler, though unnecessary if only C++ development is required.
Opening Visual Studio Code, selecting a folder, and creating a new C++ file (e.g., `main.cpp`).
Visual Studio Code recognizes the `.cpp` extension and suggests installing the C/C++ extension pack from Microsoft.
Installing the C/C++ extension pack from the Extensions section in Visual Studio Code for enhanced C++ development support.
Installing the Code Runner extension to easily run code within Visual Studio Code.
Writing a simple C++ program to declare variables and calculate their sum, demonstrating basic functionality.
Running the C++ program using Visual Studio Code's Run button, which compiles and runs the code while displaying the output in the terminal.
Manually compiling the C++ code in the terminal using the `clang++` command, specifying the file to compile and naming the output binary.
Running the compiled binary from the terminal and observing the output, showcasing terminal-based compilation and execution.
Debugging the C++ code in Visual Studio Code by setting breakpoints and stepping through the code to inspect variables and program flow.
Using the debug features like 'step over' and 'continue' to analyze variable values and detect errors during code execution.
Creating a `launch.json` and `tasks.json` file automatically in Visual Studio Code for sharing configurations and enabling consistent debugging across teams.
Using Source Control in Visual Studio Code to track changes in the project and easily share the code and configurations with other developers via Git.
Transcripts
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
تصفح المزيد من مقاطع الفيديو ذات الصلة
How to set up Visual Studio Code for Executing and Debugging C Programs | Tutorial
How To Install C Programming Software In Laptop | C Installation Tutorial For Beginners |Simplilearn
How to set up Python on Visual Studio Code
Getting Started with Debugging in VS Code (Official Beginner Guide)
Generate C# Model from existing Microsoft SQL Server Database
Cara Install React Native + Full Setup [2024]
5.0 / 5 (0 votes)