Setting up a C++ Development Environment

Getting our computer set up so we can create and build C++ programs. Then, creating our very first application
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, Unlimited Access
3D Character Concept Art
Ryan McCombe
Ryan McCombe
Updated

In this introductory lesson, we'll create our first program in C++!

We will start very simply. We'll just be outputting some text to the screen.

Things will ramp up quickly, but first, our priority is to make sure we have everything we need to start writing and compiling C++ code.

If you do not have a development environment set up yet, let's start by going through some options we have.

Selecting a Code Editor

If you do not yet have a code editor, now is the time to get one! There are plenty of free options for every major operating system.

For C++, we'd recommend these:

For Windows, Visual Studio is the common choice. The Community Edition is free and has everything we'll need

For Mac, Xcode is likely the best option. It can be downloaded for free from the App Store.

For Linux, Qt Creator is a commonly recommended choice.

JetBrains also offers editors for C++ that work across all popular operating systems. Their products include CLion for general-purpose C++, as well as Rider which is tailored towards game development specifically.

Unlike the above three, JetBrains editors are not free. However, they have free education licenses if you're a student, and a free trial if you're not.

Integrated Development Environments (IDEs)

You will often hear the term "IDE" being used to describe the tools we use to build software. All the previous recommendations are IDEs.

In the past, developers needed to use a combination of tools to build software. In modern times, most developers now work in an IDE. These applications combine the most important tools into a single, unified user interface.

The screenshots we show in this course will be from Visual Studio 2022, but it's not that important what software you use. The interface and processes will be very similar, and the code will be entirely identical.

When installing your editor, ensure that you include C++ components during the setup if given the option. This can be done by reviewing the online documentation for your editor.

This course doesn't require anything specific or unusual - the basic ability to compile modern C++ will fulfill our needs.

In Visual Studio, this can be done by selecting one of the C++ Workloads during installation - for example, Desktop Development with C++ or Game Development with C++

A screenshot of the visual studio installer

If you already have Visual Studio installed without these components, you can configure your installation simply by running the installer again.

Hello World

Once we've got your editor of choice set up, create a new, blank C++ project with it.

In Visual Studio, this involves creating an Empty Project from the Create a new project window. This window should be accessible from your start screen, or File > New > Project from the top menu bar.

A screenshot of the visual studio window used to create a new project

Once we create the project, we can then add our first C++ file to it.

Other editors may have automatically included a starting file when they generated the project, in which case this step can be skipped.

In Visual Studio, we can add a file from the top menu bar under Project > Add New Item.

We can call our file anything we want - I've gone with main.cpp

A screenshot of the Visual Studio window for creating a new file

In Visual Studio, this will create a new, empty file in our project.

If you're using a different editor, and that editor included a starting file for you, they may also have included some code in that file. If so, delete their starting code.

We want to have our starting code look exactly like what is below:

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
}

To ensure everything works, paste this code into your main.cpp, save your file then build and run the program.

In most IDEs, this can be done by pressing a button somewhere in the user interface. In Visual Studio, for example, we can use the Start Without Debugging button on the top menu.

A photo of the Visual Studio interface

Our program should open a terminal where we see the output of the code - a simple "Hello World!" message.

Hello World!

Error: extended character " is not valid in an identifier

C++, and programming languages in general, are very strict about what symbols are used in our code.

One of the most common reasons the simple previous example won’t work is that we’ve used the wrong form of quotation marks when entering "Hello World".

There are several different options we have. We must be specifically using the " character, commonly called "straight double quotes". On most keyboards, this is either near the right-shift key or above the 2 on the top row.

Other options, such as ", ‘ and ` may look similar, but they are not equivalent, and won’t work.

Understanding our Program

By the end of this course, you'll understand what every line of code is doing, but for now, we need to skip over some things.

Until we introduce those more advanced topics, our code files should always follow the basic structure of the previous example.

Initially, we will limit our changes to just the area between the { and }

Currently, that line of code is cout << "Hello World!";.

cout is an abbreviation of "character output". When we want to write (or "stream") things to the character output, it should appear in a terminal or console window when we run our program.

We stream things to the character output using << operator, with cout on the left and the thing we want to send on the right.

Operators

An operator is a symbol that performs some action on one or more objects in our program. << is an example of an operator.

In the cout << "Hello World!" example, the objects it is acting upon are cout and "Hello World"

The objects an operator acts upon are sometimes called its operands. We’ll see many more examples of operators, and get very comfortable with them over the following lessons.

If the thing we want to send is some text, we need to put the text in double quotes, such as "Hello World!" or "Goodbye!".

Finally, we need to end our statement using a semi-colon;

Statements

In programming, a statement is an instruction - some action we are commanding the computer to carry out.

In C++, and many other programming languages, a semicolon ; is used to denote the end of each statement.

We can stream as many things to the output as we want simply by repeating the pattern on additional lines:

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  cout << "My Name is Ryan";
  cout << "Goodbye";
}
Hello World!My Name is RyanGoodbye

Line Breaks

Even though our previous code was spanned across multiple lines, we can see that our input wasn’t. As we’ll see later in this chapter, C++ is fairly flexible about the spacing around our code. There are many ways to lay out the code in any given file, and our layout choice generally won’t affect how our program behaves.

However, it would be nice if we could add some spacing that does affect the output of our program. To insert a line break into our output, we stream the special sequence "\n". Note the direction of the slash is important - we need \, not /

Any subsequent output will then be on a new line:

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  cout << "\n";
  cout << "My Name is Ryan";
  cout << "\n";
  cout << "Goodbye";
}
Hello World!
My Name is Ryan
Goodbye

We can also place the \n character within an existing string of text - even in the middle of a string of text. It does not have to be streamed individually, so we could have written our previous program like this:

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!\n";
  cout << "My Name is Ryan\nGoodbye";
}

Our output is:

Hello World!
My Name is Ryan
Goodbye

endl and std::endl

Another way of achieving line breaks, which we’ll see in other documentation and examples, involves streaming endl or std::endl to the output. The following code shows an example of both:

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  cout << endl;
  cout << "My Name is Ryan";
  cout << std::endl;
  cout << "Goodbye";
}
Hello World!
My Name is Ryan
Goodbye

We’ll tend to prefer the \n method, but we can consider either approach to be approximately equivalent.

Note however that the option of inserting the new line sequence in the middle of our text like "Hello\nWorld" is not available when using endl or std::endl. Those need to be streamed in isolation

Chaining << Operators

We can stream multiple things to the terminal in a single statement. This is done by using the << operator multiple times, sometimes referred to as chaining the operator:

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!" << "\n";
  cout << "My Name is Ryan" << "\n";
  cout << "Goodbye";
}
Hello World!
My Name is Ryan
Goodbye

We can further shorten this program to a single statement. The following statement spans multiple lines to make our code easier to read, but it is still a single statement. We can tell when our statement ends by looking at where the semicolon is:

#include <iostream>
using namespace std;

int main(){
  cout << "Hello World!\n"
       << "My Name is Ryan\n"
       << "Goodbye";
}
Hello World!
My Name is Ryan
Goodbye

Outputting Numbers

We can output many different types of data. We will see more of this soon, but here's an example of outputting a number. Note, that we don't need to surround numbers with quotes:

#include <iostream>
using namespace std;

int main() {
  cout << 15 << " is a number\n"
       << "Another number is " << 52;
}
15 is a number
Another number is 52

We should now have confirmed that our development environment is set up. We can write code, compile it, run it, and see our output.

Summary

In this introductory lesson, we have laid the groundwork for your journey into the world of coding. Here's a quick recap of what we've covered:

  1. Setting Up the Development Environment: We should now have our environment set up, and be ready to start programming
  2. Creating a "Hello World" Program: Our first coding exercise involved writing the classic "Hello World" program. This simple exercise ensured we have everything set up correctly, and served as an introduction to writing and running C++ programs
  3. Basic C++ Syntax: We delved into the basics of C++ syntax, covering how to write simple statements and use cout for output
  4. Understanding Line Breaks and Chaining Operators: We learned how to format output using line breaks (\n) and the concept of chaining << operators for streamlined code.
  5. Outputting Different Data Types: The lesson concluded with an introduction to outputting various data types in C++, including both text and numbers.

Preview of the Next Lesson: Objects, Types, and Variables

In our next lesson, we will dive deeper into the core concepts of C++ programming. You'll learn about:

  • Objects and Types: Understanding the fundamental building blocks of C++ programming and how data is categorized.
  • Variables: We will explore what variables are, and their role in helping to build more complex objects.
  • Data Types: A closer look at different data types in C++, including integers, floating-point numbers, and characters, and how they are used in programming.

This upcoming lesson is crucial as it lays the foundation for more advanced topics in C++, setting the stage for creating more complex programs.

Was this lesson useful?

Next Lesson

Objects, Variables and Types

An introduction to the building blocks of our software - objects, and the variables that can be associated with them.
3D art of a cleric character
Ryan McCombe
Ryan McCombe
Updated
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, Unlimited Access
Types and Variables
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 56 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Next Lesson

Objects, Variables and Types

An introduction to the building blocks of our software - objects, and the variables that can be associated with them.
3D art of a cleric character
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved