CommonLounge Archive

Install C++: Instructions for Windows, Mac and Linux

September 11, 2018

If you don’t want to install things right away and want to get started with the Learn C++ course, then you can skip installation for now. You will see a Try it now! button on code snippets in this course, which allows you to run the code directly from your browser / mobile app. You will also be able to edit the code and play around (in-fact, the coding exercises will require you to). In addition, there are also a number of other websites where you can submit and run your code — here are some options: Onlinegdb, Ideone, Jdoodle. That said, if working online is slowing you down or you prefer to have C++ installed on your computer, please use the instructions below. For beginners, we recommend installing C++ once you are about halfway through the course.

Setting up Environment for C++ development

C++ runs on lots of platform like Windows, Linux, Mac etc. Now, before writing your first code in C++, you need an environment to be set-up on your computer to run C++ programs successfully. Running a C++ program requires two steps (a) compiling the C++ program, and (b) running the program. In addition, we’ll also need an editor to write the program in the first place. In this tutorial, we’ll get all of these set-up.

Why are we installing a code editor or an IDE?

You might be wondering why we are installing this special code editor software, rather than using something like Word or Notepad.

The first reason is that code needs to be plain text, and the problem with programs like Word and Textedit is that they don’t actually produce plain text, they produce rich text (with fonts and formatting), using custom formats like RTF (Rich Text Format).

The second reason is that code editors are specialized for editing code, so they can provide helpful features like highlighting code with color according to its meaning, or automatically closing quotes for you.

What is an IDE (or integrated development environment)?

An integrated development environment (IDE) is a software suite that typically consists of a code editor, tools to compile and run the code, and other tools which are helpful during programming such as debugging tools, etc.

An IDE may be a standalone application (like Visual Studio, Code Blocks, Eclipse, etc), or it may include applications like Text Editor and Compiler for writing code and interpreting the code.

Windows Installation

There are lots of IDEs available on Internet for Windows operating system, like Code::Blocks, Visual Studio, Eclipse, Turbo C++ etc. We’ll be using Code::Blocks.

Installing Code::Blocks

  1. Download the codeblocks-XX.XXmingw-setup.exe binary for your computer from: www.codeblocks.org/downloads/binaries, whereXmeans any number.

The mingw-setup version includes the gcc/g++ compiler preconfigured and makes our installation easier.

  1. Run the setup file and press the Next > button after reading the dialog.

  1. Read the license and press the I Agree button.
  2. Choose the default components as shown and press the Next> button. For Windows, if you do not see minGW go back to step 1 and download the correct file.

  1. Choose an installation folder (C:\CodeBlocks) as shown below and press the Install button. Do NOT use the default location or you may have problems later in the installation of some libraries or the creation of a project. Instead, install at C:\CodeBlocks to make working at the command line and with libraries easier.

  1. Choose to run Code::Blocks now by pressing the Yes button. This step allows us to immediately configure the compiler. You can come back to this step if you decide to run Code::Blocks later.

  1. Select the GNU GCC Compiler as shown, press the Set as default button and then press the OK button.

If you have previously installed Code::Blocks, you may not see this dialog. If you are changing the compiler, you will need to manually set the compiler path (Settings -> Compiler -> Toolchain executables -> Compiler's installation directory).

  1. Make a choice for the file association and press the OK button. Choosing to associate Code::Blocks with C++ files means that Code::Blocks will start automatically whenever a C++ file is selected, such as by double-clicking an icon on Windows.

  1. Exit Code::Blocks (File -> Quit) and then press the Finish button to exit the installer.
  2. Test the installation by creating a project as shown in the next section.

Creating a Project in Code::Blocks

  1. Start Code::Blocks and click the “Create a new project” link.

  1. In the New from template dialog select the Empty project template.

  1. In the “Empty project” start screen press the Next button.

  1. Continuing through the Empty project wizard, enter a project title and folder path and then press the Next button.

  1. Continuing through the Empty project wizard, verify the GNU GCC compiler is selected and then press the Finish button.

  1. Add a new empty file to the project using the menu:File -> New -> Empty file.

  1. If you get the following dialog press the Yes button.

  1. Save the file using an appropriate file name (hello.cpp) and press the Save button.

  1. Confirm the multiple selection and press the OK button.

  1. In the file tab (hello.cpp), copy and paste the following program code. This is the “C++ Hello World” program. We’ll explain later what the program really does. For now, we’re just using it to check our installation is working.
#include <iostream>
using namespace std;
int main()
{
   cout << "Hello, World!\n";
   return 0;
}
  1. In the project workspace, build and run the project (Build -> Build and run).

  1. Verify you get a console window like the following after running the program.

You should see both Hello, World! and that the process returned 0. Any value other than 0 means there is a problem. If it happens, then you can discuss with me or comment on this discussion.

Good job! You’re all set-up to start writing C++ code!

Mac OS Installation

If you are a Mac user, one option of running C++ is to use Xcode (note that this requires a 5GB download. If you don’t want that, see the next section for how to install C++ on Mac OS using the command line).

  1. You can download it from the apple website (Download Link) or you can search it on the Mac App Store.
  2. After successfully installing Xcode, opening it and go to File -> New -> Project

  1. In the OS X section, select ‘Application’ and then select the ‘Command Line Tool’ option. Then press ‘Next’

  1. Enter “Hello World” under “Product Name”, Guest under “Organization Identifier”, and under the “Language” drop down arrow, click C++ and finally hit Next and then Create.

  1. Click main.cpp on the grey left margin of the screen. If you do not see ”main.cpp”, you should be able to see a drop down arrow that has Hello World with the blue Xcode logo next to it; click on that arrow to find a cpp file called main.cpp and click it.

  1. Write the code below into main.cpp and save it. This is the “C++ Hello World” program. We’ll explain later what the program really does. For now, we’re just using it to check our installation is working.
#include<iostream> 
using namespace std;
int main()
{
    cout << "Hello, World!";
    return 0;
}
  1. Click on Play button on the top left corner of your screen. It will compile your program and run it.
  2. Look at the bottom right of your screen to view the console output! ”Hello, World!” will be displayed.

Good job! You’re all set-up to start writing C++ code!

Linux installation and Mac OS installation via command-line

For setting up our environment, we need to install two important softwares - the code editor (to write C++ code), and the C++ compiler (to compile and run C++ code).

Step 0: Introduction to Command Line

You need to be familiar with the command line to perform this set-up. The command line is an important tool to be familiar with if you’re a programmer who uses Linux or Mac OS. You can read the Introduction to the Command-line Interface tutorial to become familiar with the command line.

Step 1: Installing the Code Editor

There are a lot of different editors and it largely boils down to personal preference. Our recommendations are included below (any one will do):

  1. Gedit: Gedit is an open-source, free editor, available for all operating systems. Download it here
  2. Sublime Text 3: Sublime Text is a very popular editor with a free evaluation period and it’s available for all operating systems. There is currently no enforced time limit for the evaluation (so you can evaluate it for a while) but you will be prompted every day to get the license. Download it here
  3. Atom: Atom is an extremely new code editor created by GitHub. It’s free, open-source and available for Windows, OS X and Linux. Download it here

Step 2: Installing the compiler

Open up the Terminal. Then, follow the steps below.

Installing compiler on Linux

Run the below two commands from your Terminal:

sudo apt-get update
sudo apt-get install build-essential

This command will install all the libraries which are required to compile and run a C++ program.

Installing compiler on Mac OS

Run the below command from your Terminal:

xcode-select --install

Step 3: Checking installation

To check that the GCC compiler is installed correctly, run the command below from your terminal:

g++ --version

If the above does not give any errors, and prints a version number, then your installation happened correctly.

Step 4: Run C++ Hello World program

Now, we will learn how to compile and run a C++ program on using the GCC compiler we just installed.

  1. Open your Code Editor and write the below program in a new file. Then, save the file as helloworld.cpp (C++ files end with .cpp extension).
#include<iostream>  
using namespace std;
int main()
{
    cout << "Hello, World!";     
    return 0;
}

This is the “C++ Hello World” program. We’ll explain later what the program really does. For now, we’re just using it to check our installation is working, and to learn how to run the program.

  1. Then, open the terminal and move to the directory where you have saved your file (your learnt how to do this in the Command-line tutorial).
  2. Run the following command to compile your code:
g++ helloworld.cpp -o hello

In the above command, helloworld.cpp is the file we are compiling, and hello is the name of the executable file which is created by the compiler after compilation.

After executing the command, a new file is created in your current directory. The name of this file is hello.

  1. Now run your program with the following command:
./hello

hello here is the name of the executable file that was just created.

  1. Your program will display Hello, World! in terminal window. Congratulations on running your first C++ program!

Congratulations

We’re now ready to start coding in C++!


© 2016-2022. All rights reserved.