Skip to content

Update C++ Language Doc #6534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 75 additions & 63 deletions docs/languages/cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Area: languages
TOCTitle: C++
ContentId: D06C8C5C-2D3A-4B2E-B31F-12F1907E6402
PageTitle: C++ programming with Visual Studio Code
DateApproved: 1/21/2022
DateApproved: 7/24/2023
MetaDescription: Find out how to get the best out of Visual Studio Code and C++.
MetaSocialImage: images/cpp/languages_cpp.png
---
# C/C++ for Visual Studio Code

C/C++ support for Visual Studio Code is provided by a [Microsoft C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) to enable cross-platform C and C++ development on Windows, Linux, and macOS.
C/C++ support for Visual Studio Code is provided by a [Microsoft C/C++ extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) to enable cross-platform C and C++ development on Windows, Linux, and macOS. When you create a `*.cpp` file, the extension adds features such as syntax highlighting (colorization), smart completions and hovers (IntelliSense), and error checking.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


![cpp extension](images/cpp/cpp-extension.png)
![C++ language features](images/cpp/msg-intellisense.png)

## Install the extension

Expand All @@ -21,81 +21,100 @@ C/C++ support for Visual Studio Code is provided by a [Microsoft C/C++ extension
1. Search for `'C++'`.
1. Select **Install**.

![Search for c++ in the Extensions view](images/cpp/search-cpp-extension.png)
![C/C++ extension](images/cpp/cpp-extension.png)

After you install the extension, when you open or create a `*.cpp` file, you will have syntax highlighting (colorization), smart completions and hovers (IntelliSense), and error checking.
## Set up your C++ Environment

![C++ language features](images/cpp/msg-intellisense.png)
C++ is a compiled language meaning your program's source code must be translated (compiled) before it can be run on your computer. The C/C++ extension doesn't include a C++ compiler or debugger, since VS Code as an editor relies on command-line tools for the development workflow. You need to install these tools or use the tools already installed on your computer.

## Install a compiler
### Check if you have a compiler installed

C++ is a compiled language meaning your program's source code must be translated (compiled) before it can be run on your computer. VS Code is first and foremost an editor, and relies on command-line tools to do much of the development workflow. The C/C++ extension does not include a C++ compiler or debugger. You will need to install these tools or use those already installed on your computer.
> **Note**: There may already be a C++ compiler and debugger provided by your academic or work development environment. Check with your instructors or colleagues for guidance on installing the recommended C++ toolset (compiler, debugger, project system, linter).

There may already be a C++ compiler and debugger provided by your academic or work development environment. Check with your instructors or colleagues for guidance on installing the recommended C++ toolset (compiler, debugger, project system, linter).
Common compilers that already come preinstalled on some platforms are the [GNU Compiler Collection](https://wikipedia.org/wiki/GNU_Compiler_Collection) (GCC) on Linux and the [Clang](https://wikipedia.org/wiki/Clang) tools with [Xcode](https://developer.apple.com/xcode/) on macOS.

Some platforms, such as Linux or macOS, have a C++ compiler already installed. Most Linux distributions have the [GNU Compiler Collection](https://wikipedia.org/wiki/GNU_Compiler_Collection) (GCC) installed and macOS users can get the [Clang](https://wikipedia.org/wiki/Clang) tools with [Xcode](https://developer.apple.com/xcode/).
To check if you already have them installed:

### Check if you have a compiler installed
1. Open a new VS Code terminal window using (`kb(workbench.action.terminal.new)`)
2. Use the following command to check for the GCC compiler `g++`:
```bash
g++ --version
```

Make sure your compiler executable is in your platform path (`%PATH` on Windows, `$PATH` on Linux and macOS) so that the C/C++ extension can find it. You can check availability of your C++ tools by opening the Integrated Terminal (`kb(workbench.action.terminal.toggleTerminal)`) in VS Code and trying to directly run the compiler.
Or this command for the Clang compiler `clang`:

Checking for the GCC compiler `g++`:
```bash
clang --version
```

```bash
g++ --version
```
The output should show you the compiler version and details. If neither are found, make sure your compiler executable is in your platform path (`%PATH` on Windows, `$PATH` on Linux and macOS) so that the C/C++ extension can find it. Otherwise, use the instructions in the section below to install a compiler.

Checking for the Clang compiler `clang`:
### Install a compiler

```bash
clang --version
```
If you don't have a compiler installed, you can follow one of our installation tutorials:

- Windows:
<a class="next-topic-btn" href="/docs/cpp/config-msvc">Installing MSVC Tutorial</a> or
<a class="next-topic-btn" href="/docs/cpp/config-mingw">Installing MinGW Tutorial</a>
- Linux:
<a class="next-topic-btn" href="/docs/cpp/config-linux">Installing GCC Tutorial</a>
- macOS:
<a class="next-topic-btn" href="/docs/cpp/config-clang-mac">Installing Clang Tutorial</a>

> **Note**: If you would prefer a full Integrated Development Environment (IDE), with built-in compilation, debugging, and project templates (File > New Project), there are many options available, such as the [Visual Studio Community](https://visualstudio.microsoft.com/vs/community) edition.

If you don't have a compiler installed, in the example below, we describe how to install the Minimalist GNU for Windows (MinGW) C++ tools (compiler and debugger). MinGW is a popular, free toolset for Windows. If you are running VS Code on another platform, you can read the [C++ tutorials](#tutorials), which cover C++ configurations for Linux and macOS.
## Example: Install MinGW-x64 on Windows

## Example: Install MinGW-x64
To understand the process, let's install Mingw-w64 via [MSYS2](https://www.msys2.org/). Mingw-w64 is a popular, free toolset on Windows. It provides up-to-date native builds of GCC, Mingw-w64, and other helpful C++ tools and libraries.

We will install Mingw-w64 via [MSYS2](https://www.msys2.org/), which provides up-to-date native builds of GCC, Mingw-w64, and other helpful C++ tools and libraries. You can download the latest installer from the MSYS2 page or use this [link to the installer](https://github.com/msys2/msys2-installer/releases/download/2022-06-03/msys2-x86_64-20220603.exe).
1. Download using [**this direct link to the MinGW installer**](https://github.com/msys2/msys2-installer/releases/download/2023-05-26/msys2-x86_64-20230526.exe).

Follow the **Installation** instructions on the [MSYS2 website](https://www.msys2.org/) to install Mingw-w64. Take care to run each required Start menu and `pacman` command.
1. Run the installer and follow the steps of the installation wizard. Note, MSYS2 requires 64 bit Windows 8.1 or newer.

You will need to install the full Mingw-w64 toolchain (`pacman -S --needed base-devel mingw-w64-x86_64-toolchain`) to get the `gdb` debugger.
1. In the wizard, choose your desired Installation Folder. Record this directory for later. In most cases, the recommended directory is acceptable. The same applies when you get to setting the start menu shortcuts step. When complete, ensure the **Run MSYS2 now** box is checked and select **Finish**. A MSYS2 terminal window will then automatically open.

### Add the MinGW compiler to your path
1. In this terminal, install the MinGW-w64 toolchain by running the following command:

Add the path to your Mingw-w64 `bin` folder to the Windows `PATH` environment variable by using the following steps:
```MSYS2
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
```

1. In the Windows search bar, type 'settings' to open your Windows Settings.
1. Search for **Edit environment variables for your account**.
1. Choose the `Path` variable in your **User variables** and then select **Edit**.
1. Select **New** and add the Mingw-w64 destination folder path, with `\mingw64\bin` appended, to the system path. The exact path depends on which version of Mingw-w64 you have installed and where you installed it. If you used the settings above to install Mingw-w64, then add this to the path: `C:\msys64\mingw64\bin`.
1. Select **OK** to save the updated PATH. You will need to reopen any console windows for the new PATH location to be available.
1. Accept the default number of packages in the `toolchain` group by pressing `kbstyle(Enter)`.

### Check your MinGW installation
![MYSS2 Installer](images/cpp/cpp-install-msys2-toolchain.png)

To check that your Mingw-w64 tools are correctly installed and available, open a **new** Command Prompt and type:
1. Enter `Y` when prompted whether to proceed with the installation.

1. Add the path to your MinGW-w64 `bin` folder to the Windows `PATH` environment variable by using the following steps:
1. In the Windows search bar, type **Settings** to open your Windows Settings.
1. Search for **Edit environment variables for your account**.
1. In your **User variables**, select the `Path` variable and then select **Edit**.
1. Select **New** and add the MinGW-w64 destination folder you recorded during the installation process to the list. If you selected the default installation steps, the path is: `C:\msys64\mingw64\bin`.
1. Select **OK** to save the updated PATH. For the new `PATH` to be available, reopen your console windows.
1. Check that your MinGW-w64 tools are correctly installed and available, open a **new** Command Prompt and type:

```bash
gcc --version
g++ --version
gdb --version
```

If you don't see the expected output or `g++` or `gdb` is not a recognized command, make sure your PATH entry matches the Mingw-w64 binary location where the compiler tools are located.
You should see output that states which versions of GCC, g++ and GDB you have installed. If this is not the case, make sure your PATH entry matches the Mingw-w64 binary location where the compiler tools are located or reference the [troubleshooting section](/docs/cpp/config-mingw.md#_check-your-mingw-installation).

If the compilers do not exist at that PATH entry, make sure you followed the instructions on the [MSYS2 website](https://www.msys2.org/) to install Mingw-w64.
## Create a Hello World App

## Hello World
To make sure the compiler is installed and configured correctly, lets create a Hello World C++ program.

To make sure the compiler is installed and configured correctly, we'll create the simplest Hello World C++ program.
### Create a C++ file

Create a folder called "HelloWorld" and open VS Code in that folder (`code .` opens VS Code in the current folder):
1. On Windows, launch a Windows command prompt (Enter **Windows command prompt** in the Windows search bar). On macOS and Linux, you can enter these commands in the terminal.
1. Run the following commands. They are creating an empty folder called `projects` where you can place all your VS Code projects. The next commands create and navigate you to a subfolder called `helloworld`. From there, you are opening `helloworld` directly in VS Code using the `code` command.

```bash
mkdir HelloWorld
cd HelloWorld
```bat
mkdir projects
cd projects
mkdir helloworld
cd helloworld
code .
```

Expand All @@ -105,11 +124,9 @@ Now create a new file called `helloworld.cpp` with the **New File** button in th

![File Explorer New File button](images/cpp/new-file.png)

![helloworld.cpp file](images/cpp/hello-world-cpp.png)

### Add Hello World source code

Now paste in this source code:
Paste in the following source code:

```cpp
#include <iostream>
Expand All @@ -120,31 +137,26 @@ int main()
}
```

Now press `kb(workbench.action.files.save)` to save the file. You can also enable [Auto Save](/docs/editor/codebasics.md#save-auto-save) to automatically save your file changes, by checking **Auto Save** in the main **File** menu.

### Build Hello World

Now that we have a simple C++ program, let's build it. Select the **Terminal** > **Run Build Task** command (`kb(workbench.action.tasks.build)`) from the main menu.

![Run Build Task menu option](images/cpp/run-build-task.png)
Now press `kb(workbench.action.files.save)` to save the file. You can also enable [AutoSave](/docs/editor/codebasics.md#save-auto-save) to automatically save your file changes, by checking **Auto Save** in the main **File** menu.

This will display a dropdown with various compiler task options. If you are using a GCC toolset like MinGW, you would choose **C/C++: g++.exe build active file**.
## Run helloworld.cpp

![Select g++.exe task](images/cpp/gpp-build-task-msys64.png)
1. Make sure you have `helloworld.cpp` open so it is the active file in your editor.
2. Press the play button in the top right corner of the editor.

This will compile `helloworld.cpp` and create an executable file called `helloworld.exe`, which will appear in the File Explorer.
![Screenshot of helloworld.cpp and play button](images/cpp/run-play-button.png)

![helloworld.exe in the File Explorer](images/cpp/hello-world-exe.png)
3. Choose **C/C++: g++.exe build and debug active file** from the list of detected compilers on your system.

### Run Hello World
![C++ debug configuration dropdown](images/cpp/select-gcc-compiler.png)

From a command prompt or a new VS Code Integrated Terminal, you can now run your program by typing ".\helloworld".
You are only prompted to choose a compiler the first time you run `helloworld.cpp`. This compiler becomes "default" compiler set in your `tasks.json` file.

![Run hello world in the VS Code Integrated Terminal](images/cpp/run-hello-world.png)
4. After the build succeeds, you should see "Hello World" appear in the integrated **Terminal**.

If everything is set up correctly, you should see the output "Hello World".
![screenshot of program output](images/cpp/helloworld-terminal-output.png)

This has been a very simple example to help you get started with C++ development in VS Code. The next step is to try one of the tutorials listed below on your platform (Windows, Linux, or macOS) with your preferred toolset (GCC, Clang, Microsoft C++) and learn more about the Microsoft C/C++ extension's language features such as IntelliSense, code navigation, build configuration, and debugging.
Congratulations! You've just run your first C++ program in VS Code! The next step is to learn more about the Microsoft C/C++ extension's language features such as IntelliSense, code navigation, build configuration, and debugging using one of the Tutorials in the next section.

## Tutorials

Expand All @@ -159,7 +171,7 @@ Get started with C++ and VS Code with tutorials for your environment:

## Documentation

You can find more documentation on using the Microsoft C/C++ extension under the [C++ section](/docs/cpp) of the VS Code website, where you'll find topics on:
You can find more documentation on using the Microsoft C/C++ extension under the [C++ section](/docs/cpp) of the VS Code website, where you can find articles on:

- [Debugging](/docs/cpp/cpp-debug.md)
- [Editing](/docs/cpp/cpp-ide.md)
Expand Down Expand Up @@ -191,4 +203,4 @@ You can learn more about how to get started with Copilot in the [Copilot documen

## Feedback

If you run into any issues or have suggestions for the Microsoft C/C++ extension, please file [issues and suggestions on GitHub](https://github.com/microsoft/vscode-cpptools/issues).
If you run into any issues or have suggestions for the Microsoft C/C++ extension, please file [issues and suggestions on GitHub](https://github.com/microsoft/vscode-cpptools/issues). If you haven't already provided feedback, you can take this [quick survey](https://www.research.net/r/VBVV6C6).
4 changes: 2 additions & 2 deletions docs/languages/images/cpp/cpp-extension.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/languages/images/cpp/cpp-install-msys2-toolchain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/languages/images/cpp/helloworld-terminal-output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/languages/images/cpp/run-play-button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions docs/languages/images/cpp/search-cpp-extension.png

This file was deleted.

3 changes: 3 additions & 0 deletions docs/languages/images/cpp/select-gcc-compiler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.