C/C++ for Visual Studio Code
C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension 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.
Install the extension
- Open VS Code.
- Select the Extensions view icon on the Activity bar or use the keyboard shortcut (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
- Search for
- Select Install.
Set up your C++ Environment
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.
Check if you have a compiler installed
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).
To check if you already have them installed:
Open a new VS Code terminal window using (⌃⇧` (Windows, Linux Ctrl+Shift+`))
Use the following command to check for the GCC compiler
Or this command for the Clang compiler
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.
Install a compiler
If you don't have a compiler installed, you can follow one of our installation tutorials:
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 edition.
Example: Install MinGW-x64 on Windows
To understand the process, let's install Mingw-w64 via MSYS2. 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.
Download using this direct link to the MinGW installer.
Run the installer and follow the steps of the installation wizard. Note, MSYS2 requires 64 bit Windows 8.1 or newer.
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.
In this terminal, install the MinGW-w64 toolchain by running the following command:
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
Accept the default number of packages in the
toolchaingroup by pressing Enter.
Ywhen prompted whether to proceed with the installation.
Add the path to your MinGW-w64
binfolder to the Windows
PATHenvironment variable by using the following steps:
- In the Windows search bar, type Settings to open your Windows Settings.
- Search for Edit environment variables for your account.
- In your User variables, select the
Pathvariable and then select Edit.
- 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:
- Select OK to save the updated PATH. For the new
PATHto be available, reopen your console windows.
Check that your MinGW-w64 tools are correctly installed and available, open a new Command Prompt and type:
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.
Create a Hello World App
To make sure the compiler is installed and configured correctly, lets create a Hello World C++ program.
Create a C++ file
- 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.
- Run the following commands. They are creating an empty folder called
projectswhere 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
helloworlddirectly in VS Code using the
The "code ." command opens VS Code in the current working folder, which becomes your "workspace". Accept the Workspace Trust dialog by selecting Yes, I trust the authors since this is a folder you created.
Now create a new file called
helloworld.cpp with the New File button in the File Explorer or File > New File command.
Add Hello World source code
Paste in the following source code:
std::cout << "Hello World" << std::endl;
Now press ⌘S (Windows, Linux Ctrl+S) to save the file. You can also enable AutoSave to automatically save your file changes, by checking Auto Save in the main File menu.
Make sure you have
helloworld.cppopen so it is the active file in your editor.
Press the play button in the top right corner of the editor.
Choose C/C++: g++.exe build and debug active file from the list of detected compilers on your system.
You are only prompted to choose a compiler the first time you run
helloworld.cpp. This compiler becomes "default" compiler set in your
After the build succeeds, you should see "Hello World" appear in the integrated Terminal.
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.
Get started with C++ and VS Code with tutorials for your environment:
- GCC on Windows via MinGW
- Microsoft C++ on Windows
- GCC on Linux
- GCC on Windows Subsystem For Linux
- Clang/LLVM on macOS
- CMake Tools on Linux
You can find more documentation on using the Microsoft C/C++ extension under the C++ section of the VS Code website, where you can find articles on:
To install support for Remote Development:
- Install the VS Code Remote Development Extension Pack.
- If the remote source files are hosted in WSL, use the WSL extension.
- If you are connecting to a remote machine with SSH, use the Remote - SSH extension.
- If the remote source files are hosted in a container (for example, Docker), use the Dev Containers extension.
Enhance completions with AI
GitHub Copilot is an AI-powered code completion tool that helps you write code faster and smarter. You can use the GitHub Copilot extension in VS Code to generate code, or to learn from the code it generates.
You can learn more about how to get started with Copilot in the Copilot documentation.
If you run into any issues or have suggestions for the Microsoft C/C++ extension, please file issues and suggestions on GitHub. If you haven't already provided feedback, you can take this quick survey.