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

ProgrammingKnowledge
26 Jan 202415:36

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

00:00

🛠️ 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`).

05:00

📦 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.

10:03

🐞 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.

15:04

🔧 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

A C++ compiler is a program that translates C++ code into machine code that can be executed by the computer. In the video, the speaker explains the need for a C++ compiler to compile C++ programs on a Mac. The example given in the script is the 'clang' compiler, which can be installed via Xcode tools.

💡Visual Studio Code

Visual Studio Code (VS Code) is a lightweight code editor that supports a wide variety of programming languages, including C++. In the video, the speaker demonstrates how to use VS Code for C++ development, showing how to install the necessary extensions and run a basic C++ program within the editor.

💡Xcode Development Tools

Xcode Development Tools are essential software packages for macOS that include a variety of tools and libraries for programming in languages like C++. The video discusses how installing these tools also installs the 'clang' compiler, which is necessary for compiling C++ code on a Mac.

💡Extensions

Extensions in Visual Studio Code are add-ons that provide additional functionalities. In the video, the speaker highlights the importance of installing the 'C++ Extension Pack' and 'Code Runner' to streamline C++ programming and execution. These extensions help with tasks such as syntax highlighting, code completion, and running code efficiently.

💡Command Line

The command line is a text-based interface used to interact with the computer. In the video, the speaker demonstrates how to use commands in the terminal to check whether 'clang' is installed and how to compile C++ programs directly from the terminal using 'clang++'.

💡Debugger

A debugger is a tool used to test and debug code by allowing developers to set breakpoints, step through code, and monitor variables. The video explains how to use the built-in debugging feature in VS Code to set breakpoints and step through a simple C++ program, illustrating how to fix issues in code.

💡Breakpoint

A breakpoint is a point in the code where the execution will stop, allowing the developer to inspect the current state of the program. In the video, the speaker sets breakpoints in the C++ code to demonstrate how to use the debugger in VS Code for tracking variable values and program flow.

💡Task.json

The `task.json` file is a configuration file in Visual Studio Code that defines how the code is compiled and run. In the video, the speaker shows how this file is automatically generated when you run C++ code, and explains its role in sharing code and configurations between developers in a version-controlled environment.

💡Launch.json

The `launch.json` file is a configuration file that specifies the settings for debugging a program. In the video, the speaker mentions how this file is created when setting up debugging in VS Code, enabling consistent debugging setups across teams or systems when the project is shared via version control.

💡Terminal

The terminal is a text-based interface in macOS where users can execute commands. The video demonstrates how to use the terminal to check for installed compilers, compile C++ programs, and run the compiled output, emphasizing its importance in programming outside of an Integrated Development Environment (IDE).

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

play00:00

hey guys in this video I'm going to show

play00:02

you how you can set up the C++

play00:04

development environment with Visual

play00:07

Studio code on your Mac operating system

play00:10

so let's get started and let's see how

play00:11

we can do it so first of all before

play00:14

starting with the development of our C

play00:17

program we need to have the C++ compiler

play00:23

in order to compile our C++ code so that

play00:26

you need to download first and then you

play00:30

can compile your C++ program with your

play00:33

Visual Studio code so for that the

play00:36

simple check whether the compiler is

play00:40

already there or not on your Mac

play00:43

operating system you just need to open

play00:44

the terminal to open the terminal click

play00:47

on Spotlight search and search for

play00:50

terminal here and once the terminal

play00:53

opens you just need to type this command

play00:56

which is C Lang space hyphen Hy py

play01:01

version and then press enter and if this

play01:04

gives you some kind of output which says

play01:07

Apple cang version something then the

play01:11

compiler for C++ is installed on your

play01:16

Mac operating system now if this gives

play01:19

you some kind of uh message which says

play01:23

cang is not recognized or this command

play01:26

fails then you need to install the

play01:29

compiler

play01:30

you can install the cang compiler in two

play01:34

different ways both will lead to the

play01:37

installation of cang and both these ways

play01:40

involves the installation of xcode

play01:42

development tools so if you want to

play01:44

install the full package of xcode which

play01:48

comes with separate IDE and several

play01:52

different uh sdks to develop various

play01:55

different languages then you can go to

play01:58

the Apple App Store and and install the

play02:00

xcode development tools from there I

play02:02

have already created a video about it

play02:05

how you can install that if you are only

play02:07

interested in development of C++ with

play02:10

Visual Studio code then I will recommend

play02:13

you to install the xcode tools using

play02:17

this command which is xcode hyphen

play02:21

select space hyphen hyphen install this

play02:24

is going to only install the essential

play02:27

xcode tools and that also involves the

play02:31

cang so if the cang uh command fails

play02:35

then just run this command when I run uh

play02:39

this command it's going to uh show me

play02:42

this uh error because I have already uh

play02:46

installed xcode separately on my Mac

play02:49

operating system so it says command line

play02:52

tool is already installed use software

play02:55

update in the system setting to install

play02:57

the updates right so in my case I have

play02:59

already have this installed in your case

play03:02

you might want to run this command once

play03:04

you run this command just check the

play03:07

version of cang once again with this

play03:09

command and if it gives you this kind of

play03:11

output then the C++ compiler will work

play03:15

once the compiler is there let's start

play03:18

with the C++ development with Visual

play03:21

Studio code so now open the visual

play03:23

studio code and here first of all we are

play03:26

going to open any folder in which you

play03:29

want to

play03:30

create your C++ program so I'm going to

play03:33

click on open folder and then I will go

play03:35

to the documents folder and here I have

play03:38

this hello world folder which is empty

play03:40

right now so I'm going to open this

play03:42

folder now here I can create a C++ file

play03:46

by clicking on this uh new file icon or

play03:49

I can click on the file and then click

play03:52

on new file icon so I'm going to click

play03:55

on new file icon and then I'm going to

play03:58

name my file as main do CPP here once I

play04:03

uh press enter Visual Studio code is

play04:06

going to recognize that I want to

play04:08

develop a C++ code and how it recognizes

play04:12

because I have named my file as main do

play04:15

CPP do CPP is an extension for C++ so so

play04:19

you can see it gives us the

play04:20

recommendation to install the C and C++

play04:23

extension pack which is from Microsoft

play04:26

itself so you can click on install from

play04:30

here if you don't want to do that then

play04:32

you can also go to the extensions

play04:36

section and here search for C++ which is

play04:39

also going to show you the same kind of

play04:42

result here you can see the first result

play04:44

which is for C and C++ and you can see

play04:48

it's and you can see it has been

play04:50

downloaded around 57 million times and

play04:54

it's from Microsoft so it's the official

play04:56

CN C++ extension pack

play05:00

for the C++ development so click on

play05:03

install here which is going to start the

play05:06

installation of this extension pack and

play05:09

once the C and C++ extension is

play05:11

installed you will see the disable and

play05:13

uninstall button so in future you can

play05:15

disable or uninstall it one more uh

play05:18

extension I will recommend here will be

play05:21

uh code Runner so just search for code

play05:24

Runner here and this is the extension

play05:27

which will help you to run your code in

play05:29

a easy way so I'm going to click on code

play05:32

Runner and then click on install which

play05:34

is going to install this extension also

play05:36

so we have installed CN C++ extension

play05:39

and we have installed the code Runner

play05:42

extension now I'm going to go to the

play05:44

Explorer and then here type a very

play05:46

simple C++ program so this is the C++

play05:50

program and what this C++ program does

play05:52

is it declares three variables num 1 num

play05:55

two and sum we assign num is equal to 5

play05:59

num 2 is equal to 10 and uh then we are

play06:02

going to add these two numbers and show

play06:05

the output here so nothing complicated

play06:07

about this uh C++ program you can find

play06:11

these kind of programs anywhere on

play06:12

internet right so now how can we run

play06:17

this program so to run this program you

play06:20

have this icon here you can see uh this

play06:24

debug C and C++ file you also have this

play06:28

Arrow here

play06:30

from here you can either debug your C

play06:32

your C++ file you have the option to run

play06:34

your C++ code or you have the option to

play06:37

run a specific C++ file using this

play06:41

option I also have the option to run my

play06:44

C++ code using this run button and then

play06:47

I can start the debugging or I can run

play06:49

the program without

play06:51

debugging so let's start with this uh

play06:54

Arrow key and here I'm going to click on

play06:57

this option which says run run code

play07:00

which is going to open the terminal and

play07:02

it's going to show me the output as soon

play07:05

as I click on the Run button you can see

play07:08

it creates this binary file so first of

play07:11

all in the background this run code is

play07:14

going to compile my C++ program and

play07:18

create this uh executable binary file

play07:21

you can see this main file is created

play07:25

and this is the command it has ran and

play07:28

then after that it's going to run this

play07:31

command and you will see the output in

play07:33

the output section right you can also go

play07:37

to the terminal and if you are a fan of

play07:41

command line you can also compile your

play07:43

code from here so let me just remove uh

play07:46

this binary and compile the code once

play07:49

again so I'm going to move this binary

play07:51

file which is the executable file we

play07:53

have created once the C++ code builds so

play07:57

I'm going to move it to the thrash and

play07:59

let's build it once again and I can uh

play08:02

just write C Lang command here so just

play08:05

write C Lang when you type uh first

play08:07

three or four letters of C length you

play08:10

can uh see what are the C length uh uh

play08:14

command which are available for us in

play08:16

our case we are going to use the

play08:18

C++ so I'm going to just write C Lang

play08:23

plus plus and then the name of the file

play08:25

which I want to compile which is main do

play08:28

CPP in my my case then hyphen o which is

play08:32

the flag we can use to name our

play08:34

executable file I'm going to name my

play08:37

executable file as main you can name it

play08:39

anything so you can uh name it out for

play08:42

example let's name it out and this is

play08:46

the command to compile your C++ code

play08:49

using the terminal right so you can run

play08:51

this command on any terminal it doesn't

play08:53

need to be in the visual studio code I'm

play08:55

going to press enter which is going to

play08:58

uh compile my C++ code you can see it

play09:00

has generated this out binary which we

play09:03

can run by

play09:05

using slout and then press enter and

play09:08

it's going to give us the same kind of

play09:10

result so you can also open the terminal

play09:13

and then run this command if the

play09:15

terminal is closed from here you can go

play09:18

to the terminal and then click on the

play09:20

new terminal and it's going to open the

play09:22

terminal for you right now we have uh

play09:27

compiled our code and ran our code now

play09:30

let's say you want to debug your code so

play09:32

you also have the option to debug your

play09:34

code using visual studio code so here

play09:38

you will be able to see uh the option to

play09:41

run your code in the debug mode so you

play09:43

can see debug C or C++ file and as I

play09:46

mentioned you also have the option here

play09:48

start debugging but before starting the

play09:51

debugging you need to set the breakpoint

play09:53

so here you can go to the run and debug

play09:57

section and right now it's not doing

play10:00

anything but when I set the break point

play10:03

so I can just uh click here and then set

play10:06

the break point on line number five line

play10:09

number six line number N9 and let's say

play10:12

line number 12 so you might have

play10:15

observed when I have uh created the

play10:18

breakpoint on these lines you will be

play10:20

able to see uh these kind of list of

play10:23

break points here in the debug section

play10:27

you will also see the line num so 5 6 9

play10:30

and 12 you can also toggle these break

play10:33

points by clicking on this button which

play10:35

is going to disable all the breakpoints

play10:37

you can enable it once again or you can

play10:41

close all the break points from here so

play10:43

you can remove all the breako by

play10:45

clicking on this remove all break points

play10:49

uh button and you will also see some

play10:52

more um options once you run the

play10:56

debugger so let's run the code in the

play10:59

the debug mode by clicking on run and

play11:00

then start debugging and then it's going

play11:03

to ask us which kind of debugger we want

play11:06

to use you can see you have CN C++ cang

play11:09

build and debug active file you have uh

play11:13

CN C++ cang build and debug active file

play11:16

so we are going to use the second option

play11:18

because we are compiling or debugging

play11:21

the C++ file if you have the g++

play11:25

compiler you will also see the option

play11:27

for uh GDB or g++ uh compiler I'm going

play11:33

to choose the second option we start

play11:35

with G cang Plus+ click on second option

play11:39

here and it's going to start the

play11:42

debugger and first of all it's going to

play11:45

give you this kind of warning which says

play11:47

developer tool access need to take

play11:49

control of another process so I'm going

play11:52

to just give my macbook's password here

play11:55

which I used to log to my MacBook and

play11:58

click on continue

play12:00

and then click on okay and then my

play12:03

debugging starts once the debugging

play12:06

start you will see these button on top

play12:08

the first button is for continue so when

play12:10

you press on this your program execution

play12:13

stops at the next break point you can

play12:16

also use this step over step into step

play12:19

out and you have the option to restart

play12:22

the debugging and stop the debugging

play12:24

using this button let's step over to the

play12:27

next line so I'm going to to click on

play12:29

step over button and as soon as I do

play12:32

that you can see num one variable is

play12:36

printed here and here the num one the

play12:39

value which is assigned to num one here

play12:41

is five you will also see num two and

play12:44

num three variables but because we

play12:47

haven't executed the code on the line

play12:49

number six or line number 12 we haven't

play12:52

assigned the value to num two and the

play12:54

sum variables so here you will be able

play12:57

to see some random value use assigned to

play12:59

num two and some variables 1 and let's

play13:03

say 1 25 72 these are some of the random

play13:06

values because our program execution is

play13:09

stopped at this point we haven't

play13:11

completed this line of code so when we

play13:14

step over to the next line you will

play13:16

observe that now num two has the value

play13:20

of 10 once again we have stopped at this

play13:24

point so some has random values so let's

play13:26

step over once again and now St some

play13:30

have 15 values so this is how you can

play13:33

debug your code once again when you

play13:35

click on the continue button your

play13:37

program will run successfully and it's

play13:39

going to show you the output here so

play13:43

this is how you can debug your C++ code

play13:47

if you want to create a launch. Json

play13:50

file in the debug section so when you

play13:52

click on debug and you have the option

play13:54

to create the launch. Json file so let

play13:57

me click on this file and it's going to

play13:59

create this launch. Json file for you so

play14:03

once it creates a launch. Json file you

play14:06

you will see that this vs code folder

play14:09

will will be created in your project

play14:11

directory and you will see these two

play14:14

files which are created which is launch.

play14:17

Json which have the configuration

play14:19

related to your uh debugger also you

play14:23

have the task. Json file have the

play14:26

configuration related to your your

play14:29

compiler right so you can see this task

play14:33

we have added this task here which is

play14:35

generated uh automatically for you when

play14:38

you ran this code using this button

play14:41

right so this kind of uh vs code and

play14:46

launch. Json and task. Json uh are

play14:49

important when you want to share your

play14:51

code with other colleagues so when you

play14:53

are using Version Control let's say when

play14:56

you click on Source control and when you

play14:58

add this this uh code into some kind of

play15:01

Git Version Control then you will be

play15:03

able to see your changes here and when

play15:05

you push your code to GitHub some other

play15:08

developer is going to pull that code and

play15:10

he will be able to use the same

play15:12

configuration that's why it's useful to

play15:15

create this kind of uh launch or task.

play15:18

Json

play15:20

files so that's it for this video this

play15:23

is how you can set up the c or C++

play15:25

development environment on your Visual

play15:27

Studio code editor

play15:29

on your Mac operating system I hope you

play15:32

enjoyed this video and I will see you in

play15:34

the next video

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
C++ setupmacOS developmentVisual Studio Codecompiler installationcode debuggingcode executioncode runnerclang compilerxcode toolsdeveloper guide
¿Necesitas un resumen en inglés?