C++,  Programming

Visual Studio Code and C++ Setup for Mac – A Complete Guide

A C++ compiler is required to compile and execute C++ programs on you machine. To get the most benefit out of it, you should have a Code Editor installed along with it, as it makes your life a lot easier! In this article, I will walk you through the entire C++, VSCode setup for Mac.

A code editor plays a vital role in improving the productivity of a software engineer in day-to-day life. Learning and understanding to use a code editor properly is very important. With the help of colored syntax highlighting, auto indentation/formatting, auto function completion/suggestion and proper error detection, a code editor makes our program look visually appealing by making it look clean, beautiful and error-free.

A code editor is more suitable than an IDE, especially for beginners. But why? Because it is lightweight, easy to install, and contains all the necessary functionalities to run and debug our code. An IDE (Integrated Development Environment) is a software development environment that combines different tools like code editor, compiler, debugger, test tools, tools for creating user interface designs, and so on (Android Studio as an example), which is unnecessary at this point in time.

In this lecture, we will setup up one of the most popular code editor to start learning C++ programming, VSCode (Visual Studio Code)! Please note that I have explained in detail the steps required to setup C++ and VSCode on Mac. For Windows, you need to install GCC (MinGW) compiler for C++. There are plenty of tutorials available for it, so do not worry. Also note that the VSCode settings I show below will be same irrespective of whether you are using Mac or Windows.

So let us begin!

VSCode Setup on Windows

  • Go to https://code.visualstudio.com/
  • Click on the Download- Stable Build button
  • The installer will automatically start downloading
  • Once, the download is complete, double-click on the installer
  • Select I accept the agreement
  • Click Next
  • Set path location, prefer C:/Visual_Studio_Code
  • Click Next
  • Leave the Select Start Menu Folder as it as and click Next
  • Check all checkboxes as shown below
vscode setup check all boxes
  • Click Next
  • Click on Install
  • Check Launch Visual Studio Code
  • Click on Finish

Once VSCode opens up,

  • Close the Description and Release Notes files
  • Now, you should see a screen as shown below.
  • Click the Explorer (highlighted) to view your files.
vscode setup welcome screen

C++, VSCode Setup for Mac (Complete Installation)

Firstly, let us install our C++ compiler. Clang is the default C,C++ compiler for Mac. To check whether we have it on our machine

Setting up Clang

  • Go to Launchpad
  • In the search bar above, type Terminal
  • Once the terminal opens, type the command clang –version (double hyphen)
  • If there is a message saying “No developer tools found, requesting install”, it means clang is not installed on you mac
  • It will automatically show a popup for install as shown
install clang
  • Click Install
  • Click Agree
downloading clang
clang download complete
  • Click Done
  • If by chance, the dialog to download clang does not show up, you need to
  • Enter the command xcode-select –install (double hyphen before install)
  • To check whether we have it installed on our machine now?
  • Type clang –version in the terminal again (double hyphen before version)
  • You should be able to see the version details
  • To quit the terminal properly, type exit
  • Now, on the top left, click Terminal
  • Select Quit Terminal

Alright, our C++ compiler is ready, it is time to install our code editor now!

Setting up VSCode

  • Open any browser, and type Visual Studio Code Download OR
  • Visit: https://code.visualstudio.com/download
  • Click the Download button under Mac
  • Open Finder
  • Go to Downloads
  • Right-click the setup file
  • Click Open
  • Now, you will be able to search Visual Studio Code in Launchpad
  • Open VSCode
  • Choose Dark Modern Theme
  • Mark Done
  • You will be greeted by a window as shown below
vscode welcome
  • Now, at the bottom left of the window, click the Manage icon
  • Choose Settings
  • Under Applications, select Telemetry
  • Select Off

Alright, so now let us create a folder where we will store all our C++ programs which we learn

  • On your desktop, create a new folder “c++_workspace” (don’t use space)
  • Inside this folder, create another folder “c++

Now, imagine that as a software engineer, you are working on a project specifically using C++. There might be multiple projects on your machine which might use different programming languages but you use the same code editor for them too, i.e, VSCode!

It might be the case, that in the future, you want to change some specific settings for this particular project but want to ignore those settings when you open VSCode for some other project. To solve this problem, we need to create a Workspace specific to the project which in our case is our current C++ project.

Setting up Workspace

  • Click on File on the menu bar
  • Choose Add folder to workspace
  • Select the “c++_workspace” folder you created
  • Next, allow VSCode file access
  • Also select “Yes, I trust the authors” (if the prompts are asked)
  • Now, we need to save our workspace, for that
  • Go to File again
  • Select “Save workspace as
  • Again select the “c++_workspace” folder
  • Click Save
  • Click on the Explorer icon on the top left corner
  • You will have a c++_workspace.code-workspace file along with the c++ folder in your “c++_workspace” folder
  • Your folder structure should look something like this (shown below)
  • In the future, whenever you need to open this workspace, double click on the file c++_workspace.code-workspace
c++ vscode setup folder structure

Setting up Extensions

  • Select the Extensions icon (last icon in image above)
  • Search C/C++ Microsoft (IntelliSense)
  • Click Install
  • You can ignore this popup
c++ extension popup
  • Click on Manage icon
  • Open Settings
  • Type Autosave
  • Change Off to onFocusChange (i will explain what it does later)
  • Type Format
  • Check Editor: Format On Paste and Editor: Format On Save
  • Type C_Cpp.clang_format_fallbackstyle
  • Keep it as Visual Studio (you can choose Microsoft, Google if you wish)
  • Click File
  • Click AutoSave (it should be ticked)
  • Now, click the Open Settings icon as shown below

Your settings.json file should look like this

settings.json

Setting up Appearance

  • Go to View
  • Select Editor Layout
  • Choose Three Columns
  • Click View again
  • Select Editor Layout again
  • Choose Two Rows Right
  • Click on Explorer
  • Select the “c++_workspace” folder
  • Click on the New File icon
  • Create input.txt
  • Create output.txt
  • Now, drag input to the top right window we created and output to the down right
  • Click the cross icon to resize these 2 windows
  • Use the scroll line to resize the main window
  • Now, in your “c++” folder, create a checker.cpp file
  • Add the following code to this file as shown in the image
c++ code
  • In the input.txt file, type “abc def
  • Click on the Run/Debug C/C++ file as shown above (choose run c/c++)
  • Select the C/C++: clang++ build active file option to run your program
  • Approve the necessary permission to access the desktop

Now, you should see the entire setup working as shown below

Important Note: You need not understand, what does the code do, right now, I will cover it later! All you need to observe is the file structure and the output in the output.txt file.

  • Click on the Terminal
  • Move your cursor on the window to activate it
  • Press return

Hurray! You have completed the entire C++ and VSCode setup for your Mac🤗.

Conclusion – C++, VSCode Setup

The aim of this article was to help you understand how do software engineers set up a proper environment to executive day-to-day programming tasks.

You can even code in a notepad once you have a compiler installed on your machine. But since software codes are huge and complex, software engineers use code editors and IDE’S because they make your life a lot easier. How?

By making your code readable, reusable, beautiful, versatile, maintainable as well as manageable. Also, code editors often show you errors when you make a mistake (for example: if you miss a semi-colon after a line etc.). IntelliSense in VSCode helps you in completing the code by automatically suggesting functions.

VSCode has auto code format option (as used above) as well as the support of bracket pair colorizer (manages bracket colors for readability and scope automatically) by default. Formatting code is critical as it automatically indents your code, removes unnecessary spaces from it, maintains brackets, manages line spacing which is very helpful in maintaining symmetry in your code, thus making your code visually appealing!

Some Bonus Tips

Bonus 1

With the help of onFocusChange option in AutoSave, when you click outside of the active window, the code in that active window saves automatically. Once it saves, it will automatically format itself (according to our settings). For example: If you click the input.txt window/file to enter input after you have written your code in the checker.cpp file, the file saves and formats itself automatically.

Bonus 2

As a beginner, try to minimise the use of IntelliSense by typing your code in notepad while practicing. You can paste it later in the editor (it will auto format your code). Remember, programming is all about practicing and remembering. The reason we set up everything is to help you practically understand how code editors can help you greatly during real-world software development.

Bonus 3

A slight difference you might encounter when running c++ programs on windows is the use of a GCC compiler header file #include <bits/stdc++.h>, which is unavailable in the clang compiler for mac.

Basically, when we write c++ programs, we use libraries which are included in our code by using header files i.e., #include <a_random_library.h>. With the help of the command discussed above (bits/stdc++.h), you can include all the headers in your code in one shot! The header is very lightweight, so even though you do not use all the libraries in your code, it does not create any difference!

Although, for beginners, I recommend adding all the header files which you use in your code separately, so that you are aware about the various headers and the functions available inside them.

If you still want to use it, you have to create bits directory inside /usr/local/include and then make a header file stdc++.h inside bits and paste the contents of this code inside it. Then, it should compile as expected. I would not recommend it as discussed above.

Final Bonus Tip

Suppose, you want to code in a machine which does not have c++ set up as we did or you want to write and test a small code and all you have is a smartphone (no laptop, pc)! How do we solve this problem?

Go to you web browser, search “C++ Online GDB” or visit: https://www.onlinegdb.com

Select C++ 14/17 as your language, and you are good to go😎!

I know it was quite a tedious article filled with a lot of information to process, but the knowledge you gained from this article will always help you work efficiently throughout your journey as a programmer!

Beautiful, so we are all set up, let us dive straight into learning C++!

And I will see you in the next one😀.