Before starting to program in C++, we need to install an integrated development environment (IDE). Visual Studio is one of the most comprehensive and widely used for programming in C++.
C++ in Visual Studio is also called MSVC (Microsoft Visual C++), and uses a Microsoft compiler.
Visual Studio is available for Windows and its download is free in the Community version, which includes all the necessary tools for C++ development.
Let’s see how to install it and how to configure it for using C++-👇
Installing Visual Studio
Before configuring Visual Studio, we need to install it. Simply Visit the official Visual Studio site https://visualstudio.microsoft.com/es/ and select the Community version of Visual Studio.
The Community version is free and suitable for most personal projects.
Once the installer is downloaded, run it to start the installation process. During the installation, Visual Studio will allow you to choose the tools and components you want to install.
For C++, select the option Desktop development with C++. Then click on Install (be patient, it will take a little while).
More information about what it is and how to install Visual Studio
in the Introduction to Programming Course read more
Creating a C++ Project
To create a new C++ project in Visual Studio, click on File and then on New Project.
Choose the template C++ Console Application. This type of project is ideal for learning and practicing, as it allows you to execute and see results directly in the console.
Now we need to give the project a name, select the location where you want to save it, and click on Create.
Finally, the main editor window will open, with the main.cpp
file created and ready for you to put your code in.
Writing and Running Code in C++
You can start writing your code in the main.cpp
file that was automatically created when you set up the project. For example,
#include <iostream>
int main() {
std::cout << "Hello, world in C++ from LuisLlamas.es!" << std::endl;
return 0;
}
We will see each line in detail later. For now, don’t worry, we are just setting up the environment.
Compilation and Execution
To run your program click on Debug > Start Debugging (or press F5). Visual Studio will analyze the code for errors and compile it.
When the program runs, a console window will open displaying the message
Hello, world in C++ from LuisLlamas.es!
Congratulations, you now have Visual Studio ready to work with C++.
Build Configuration
Visual Studio allows you to compile in two main configurations: Debug and Release.
- Debug is used for development and debugging.
- Release is used when you want to generate an optimized version for distribution.
You can select the desired configuration in the top bar of Visual Studio, where the dropdown list with Debug and Release options appears.
Debugging
One of the main advantages of using Visual Studio is that its debugging tools are great, making it much easier to find and fix errors.
To do this, we can set breakpoints to the left of the line of code where you want the program to stop.
The program will run until it reaches the breakpoint, allowing you to examine the state of the program. This is useful for seeing the state of variables and the logic at different points in the program.
There are many more debugging tools, such as conditional breakpoints, stack visualization… you can explore them gradually.
Configuring the Environment for C++
Generally, in any project you will need to configure options. And, to be honest, it’s not one of the prettiest parts of MSVC (not everything is going to be nice).
To access the options, right-click on the project and select Properties.
For example, we can select the version of C++ we are going to use in C/C++ Settings > General. In C++ Standard, select the version you want to use, such as C++17 or C++20.
From here you can also Set the Include Directories. If your project requires external libraries, you need to tell Visual Studio where the directory is.
This is done in Project > Properties > C/C++ > General > Additional Include Directories.