How to get started with C++ on Mac / Windows

Welcome back! C++ is a very popular programming language used in almost every company across the world, so let’s talk about the basics of this language, specifically installing it on your machine! It is recommended for MacOS users to make sure you have Xcode installed, here is a link on how to install that:

Installing IDE

Starting off we want to install an IDE, this is an integrated development environment, there are many different ones available but we’ll be using Visual Studio Code in this example, so let’s go ahead and install it. Let’s make our way over to the Visual Studio Code website:

From there, we want to download the specific software for our machine, luckily for us the website will automatically determine which machine we’re on, all we have to do is click the download button here:

Once downloaded you want to install this piece of software just like any other software. Once that is done we will then have to install the C++ extension for this environment, to do so we want to open up the VS Code (Visual Studio Code) app, we want to select the extensions view icon, search up C++ and install this extension:

Awesome! If you want to view the full documentation on Visual Studio Code C++ extension check out their page below:

Great! Now a C++ file should always use the extension .cpp, this indicates that the file is a C++ file. Now, let’s say we have a C++ file coded out and we want to run it, well we would need to run it through a compiler.

Installing a Compiler

To install a compiler we’ll need to make sure we don’t already have one installed, Linux and MacOS machines already have a C++ compiler built in. We can make our way over to the Visual Studio Terminal and type in:

g++ --version

This should output the version of your g++ version of your system, if you don’t have g++ installed on your MacOS machine you’ll have to install command line tools, look at the following link to do so:

On Windows we would need to install the MinGW-x64, to install this software we want to use this link down below:

At that point you want to verify that the installation went through successfully by typing in the following code:

g++ --version
gdb --version

Awesome, you are now ready to start writing a C++ project, let’s build a very basic C++ script.

Let’s Code

Let’s go ahead and do a sample Hello World, this is essentially the code to do so:

#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
}

We’ll go into more detail about this code in other tutorials, now we want to save this as a .cpp file, so make sure to save it as that file type. Next up, we want to run it in our terminal, let’s start off by compiling the code, to do so we use the following template:

g++ PATH/TO/THE/CODE.cpp -o <any name>

For me this code will look like this:

g++ Users/Desktop/hello.cpp -o hello

Now we want to execute this code, this is the line to do so:

./hello

This is what the final code will look like:

Awesome! At this point you should have your C++ environment set up and the ability to run your code as well. I will be making tons of tutorial for C++ which will link to this tutorial, so if you have any improvements to this tutorial please let me know!

As Always

if you have any suggestions, thoughts or just want to connect, feel free to contact / follow me on Twitter! Also, below is a link to some of my favorite resources for learning programming, Python, R, Data Science, etc.

Thanks so much for reading!

Data Scientist / Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store