VS Code Setup for C++
In this article, you will learn about the Visual Studio Code Setup in C++ with different operating systems. Before discussing the VS Code Setup, you must know something about C++.
What is C++?
C++ is the general-purpose programming language that was developed by Bjarne Stroustrup. It is a compiled and statically typed language renowned for its effectiveness and performance. Linux, embedded devices, and game development are just a few of the applications that frequently use C++.
Objects are used for storing data and behavior in C++ because it is an object-oriented programming language. Because of this, C++ is a strong language for building sophisticated applications.
Here are detailed instructions for downloading and setting up the Visual Studio Code on various operating systems:
Windows
Use the following steps to install VS code in Windows OS:
- First, visit https://code.visualstudio.com/ to access the Visual Studio Code website, and then select "Download" from the menu.
- After that, a .exe file containing the installer will download. Run the downloaded file and adhere to the prompts on the screen.
- In the end, Visual Studio Code (VS Code) will start after the installation process is finished.
macOS
Use the following steps to install VS code on MacOS:
- First, click the "Download for macOS" option on the Visual Studio Code site at https://code.visualstudio.com.
- Here, a .zip file containing the installer will download. To unzip the .zip file, double-click on it.
- To the "Applications" folder, drag the "Visual Studio Code.app" file.
- Now, start coding by opening "Visual Studio Code.app".
Linux
Use the following steps to install VS code in Linux:
- First, click the "Download for Linux" option on the Visual Studio Code (VS Code) website at https://code.visualstudio.com.
- Here, a .deb file containing the installer will download. Run this command in a terminal window: sudo apt install./file>.deb.
- After the installation process is finished, Visual Studio Code (VS Code) will start.
Vitality of Extensions
A fantastic approach to increasing Visual Studio Code's capabilities is using extensions. Extensions are provided for a range of tasks, like C++ programming. Extensions can offer a variety of advantages, such as:
Additional features: Extensions can give Visual Studio Code new capabilities like syntax emphasizing completion of code, and debugging assistance.
Increased productivity: Extensions might make you more productive by automating processes or offering shortcuts.
Personalization: Visual Studio Code can be tailored to your preferences via extensions.
Extensions that Are Essential for C++ Development:
There are several essential extensions for C++ development, such as:
- Microsoft's C/C++ plugin for Visual Studio Code is available here. Numerous features are included, including syntax emphasizing completion of code and debugging assistance.
- Microsoft's CMake Tools extension supports CMake, which is a build mechanism for C/C++ projects.
- Yuval Greenfield's C++ IntelliSense is an extension that gives C++ better IntelliSense functionality.
- The Cppcheck static evaluation tool is integrated into Visual Studio Code through this plugin, which is called Cppcheck by Cppcheck.
- The add-on integrates the Clang-Format source formatter into VS Code. It was created by ArcticCoder.
These are only a few of the numerous C++ development extensions that are available. You can search within the Visual Studio Code Store for additional extensions.
Putting in Extensions
Open Visual Studio Code and choose the Extensions view (Ctrl+Shift+X) to install an extension. Click the Install button after searching for the add-on you want to install.
By choosing the Extension view and selecting the radio button next to the extension, you can either enable or disable an extension once it has been installed.
Configuring a C++ Compiler
As the name suggests, a C++ compiler is a piece of software that converts C++ source code into computer-executable machine code. There are numerous C++ compilers available, but a few of the most well-known ones are as follows:
- The GNU Compiler Collection, also known as GCC, is an open-source and free compiler that may run on a range of different platforms.
- Apple created the C++ compiler known as Clang. Additionally, it supports a wide range of platforms.
- A C++ compiler is part of the commercial integrated programming environment (IDE) known as Visual Studio.
Setting up a C++ compiler
According to your operating system, a C++ compiler installation process may be different. The fundamental steps are as follows, though:
- Visit the installation installer's website for the compiler for C++ that you want to use.
- Run the setup file and adhere to the prompts on the screen.
- You ought to be able to build C++ code once the setup is done.
PATH Environment Variable Configuration
The operating system looks in the directories listed by the PATH environmental variable for executable files. You should include the directory where the C++ compiler is installed in the PATH environment variables to make it simple to reach the compiler.
According to your operating system, there may be a different process for setting up the environment's PATH variable. The fundamental steps are as follows, though:
- First, open a text editor, and then make a new file .bashrc.
- After that, add the next line to the document:
export PATH=$PATH:<path_to_compiler>
- Next, close the content editor after saving the document.
- Run this command in a fresh terminal window:
echo $PATH
- The directory wherein the compiler for C++ is installed should be displayed in the command's output.
New C++ Project Creation in Visual Studio Code
It's easy to start a new C++ application in Visual Studio Code. The steps are as follows:
- Start VS Code.
- Select New File from the File menu by clicking.
- Enter main.cpp in the name of the file field.
- Select C++ in the Language field.
- Select "Create" from the menu.
- It will add a new main.cpp C++ source file to your existing workspace.
A Project Directory Structure's Configuration
Setting up a project's directory arrangement for your C++ projects is a smart idea. You can better organize your code and locate files by doing this.
For a C++ project, a straightforward directory structure may resemble this:
project_name/
main.cpp
include/
header.h
src/
main.cpp
The main folder of the project is called project_name. Source files are located in the src directory, whereas header files are located in the include directory.
The first C++ source code creation:
Your first project's C++ source file should contain a straightforward "Namaste, world!" program. The code below displays a straightforward "Namaste, world!" program:
#include <iostream>
int main() {
std::cout << "Namaste, world!" <<std::endl;
return 0;
}
Output:
![VS Code Setup for C++](https://static.tutorialandexample.com/cpp/vs-code-setup-for-cpp1.png)
Start the program by using the Run icon in the Visual Studio Code toolbar. The program can also be launched by using Ctrl+Shift+B.
Using VS Code to write C++ code:
C++ code can be written using the well-known coding editor VS Code. It has several characteristics making it a good option for C++ development, such as:
- Highlighting syntax
- Completed code
- Debugging assistance
- IntelliSense Extensions
Using VS Code to write C++ code
You can access a current C++ file or start a new C++ project in Visual Studio Code to develop C++ code. You can begin writing code after opening a C++ file. You may write C++ code more easily with the help of VS Code's various capabilities, which include:
- The syntax in your C++ code will be highlighted by VS Code, making it simpler to read and comprehend.
- Code completion: VS Code will offer ideas for code completion to help you write code fast and properly.
- Support for debugging: VS Code may be used to discover and correct issues in C++ code.
- IntelliSense is an option that offers documentation and code suggestions for C++ methods and classes.
- Extensions: There are a variety of extensions available for VS Code that are able to offer more functionality for C++ development.
Create and compile C++ code in Visual Studio Code:
You can create and execute C++ code with Visual Studio Code. You must have a compiler for C++ loaded on the machine to accomplish this.
Following these instructions will allow you to create and execute C++ code in Visual Studio Code after you have the following C++ compiler installed:
- The C++ file you intend to create and compile should be opened.
- The VS Code toolbar's Run button should be clicked.
- Select the create and debug option in the Run dialogue box.
- Select "Run" from the menu.
The C++ code will be compiled by VS Code before the program is executed.
Compiler Errors and Warnings Handling:
If the C++ code contains any errors or warnings, VS Code will show these in the Output window. The Output panel can be used to fix errors and warnings. You must change the C++ code such that it builds without errors to fix a mistake. You have two options for fixing warnings: either edit the code in C++ or disregard the warning.
Setting up IntelliSense in C++:
For C++ code, VS Code's IntelliSense tool offers auto-completions and code suggestions. It can be set up to offer a number of functionalities, such as:
- As you type, IntelliSense might provide code completion recommendations.
- Code auto-completion is possible with IntelliSense as you type.
- IntelliSense's documentation feature is available for C++ classes and functions.
- Rapid information: IntelliSense can give rapid information about C++ classes and functions.
- IntelliSense's parameter hints feature is available for C++ functions.
Setting up IntelliSense:
You can utilize the IntelliSense options in the Visual Studio Code (VS Code) settings to set up IntelliSense. You can regulate the following characteristics of IntelliSense using these settings:
- IntelliSense's list of compilers includes: The set of compilers which IntelliSense utilizes to offer code ideas and auto-completions can be specified.
- IntelliSense's collection of header files includes: The list of header files that IntelliSense uses for code suggestions, and auto-completions can be specified.
- The degree of detail offered by IntelliSense is: The level of information that IntelliSense offers for code recommendations and auto-completions is customizable.
Setting IntelliSense Options Specifically:
You can alter IntelliSense settings in addition to those found in the settings menu by using the keyboard shortcuts listed below:
- The keyboard shortcut Ctrl+Space causes IntelliSense to propose possible code.
- The keyboard shortcut Ctrl+Shift+Space causes IntelliSense to offer auto-completions.
- F1: This keystroke opens the current symbol's documentation.
- Ctrl+Q: This keyboard shortcut displays the current symbol's quick information.
VS Code's C++ code debugging:
A built-in debugger in VS Code can be used to troubleshoot C++ code. You may set breakpoints, analyze variables, and step over the code step by step with the debugger.
Configuring the Debugger
You need to generate a launch .json file in the project directory to configure the debugger. A configuration file called launch .json tells VS Code where to debug your code.
Example:
An illustration of a launch .json file associated with a C++ project is given below:
{
"version": "1.0.0",
"configurations": [
{
"name": "C++ Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/main.cpp",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "build"
}
]
}
Explanation:
- The launch's name property is the title of the debug configuration which is specified in the .json file. The kind of debugger is specified by the type property. In this instance, the type is cppdbg (the C++ debugger).
- The kind of debugging request is specified by the request attribute. In this instance, VS Code will run the program, after which it will begin to be debugged because the request is launched.
- The road to the program that you wish to debug is specified by the program attribute. The application in question is main.cpp.
- The arguments you want to send to the program are specified by the args attribute.
- Whether or not the debugging tool should halt at the program's entry point is determined by the stopAtEntry attribute.
- The debugger's current working directory is specified by the cwd property.
- You can set environment variables for the debugger using the environment property.
- If an external console should be opened for the debugger, VS Code will open it or not, according to the externalConsole attribute.
- The task that you wish to execute before the debugger launches are specified by the preLaunchTask attribute. Since the task in this instance is built, VS Code will construct the project before launching the debugger.
Establishing breakpoints
When you set a breakpoint in your code, the debugger will halt execution at that moment. Breakpoints can be set by pressing on the left-hand side of the code editor.
You can examine the values for variables and step over the source code line by line when your debugger pauses at a breakpoint.
Examining the Variables:
Use the Variables pane to look at the values of the variables. The values of each variable that is in range at the present breakpoint are displayed in the Variables pane.
Following the Code Step by Step:
You may utilize the Step icon in the debugger bar to step around the code word by line. The next piece of code is entered into the debugger by pressing the Step button.
You may also utilize the Move Over and Step Out buttons to move through the code. Even if the subsequent line of code contains a function call, pressing the Step Over key will advance the debugger to that line of code. When you press the Step Out button, the debugger will exit the active function.
Run C++ Programs from Visual Studio Code:
Programs written in C++ can be run straight from the IDE using VS Code. To accomplish this, use the F5 keyboard command or the Run option in the toolbar.
The IDE will build the program before launching it when you execute a C++ program from VS Code. If the program is a console use, a console window will open so you may view the program's output.
Executing Console-Based Programs:
A program that runs in a command-line window is referred to as a console-based application. Console programs are frequently used for straightforward tasks like writing text to the terminal or reading user input.
The procedures listed below can be used to launch a console-based application using Visual Studio Code:
- Open the console application's C++ source file.
- Press F5 or select the Run option from the toolbar.
- The program will launch, and a console window will appear.
Taking input from user:
Applications running on a console can read user input and print results to the console. The std::cin object can be used to capture user input. Use the std::cout object for printing output to the console.
Example:
An illustration of an application for the console that accepts user input and prints it to the console can be seen in the code below:
#include <iostream>
int main() {
std::string input;
std::cout << "Enter any text: ";
std::cin >> input;
std::cout << "The text you entered: " << input << std::endl;
return 0;
}
Output:
![VS Code Setup for C++](https://static.tutorialandexample.com/cpp/vs-code-setup-for-cpp2.png)