C/C++ support for Visual Studio Code is provided by aMicrosoft C/C++ extensionto enable cross-platform C and C++ development on Windows, Linux, and macOS. When you create the file*.cpp
the extension adds features such as syntax highlighting (coloring), intelligent completion and scrolling (IntelliSense), and error checking.
Install the extension
- Open VS Code.
- Select the extensions view icon in the activity bar or use the keyboard shortcut (⇧⌘X(Windows, LinuxCtrl+Shift+X)).
- To look for
„C++”
. - To chooseinstall.
Set up a C++ environment
C++ is a compiled language, which means that the source code of a program must be translated (compiled) before it can be run on a computer. The C/C++ extension does not include a C++ compiler or debugger because VS Code relies on command-line tools for its development workflow as an editor. Install these tools or use tools that are already installed on your computer.
Check if you have a compiler installed.
Use: A C++ compiler and debugger may already exist in your academic or professional environment. Contact your instructors or colleagues for guidance on installing the recommended C++ toolset (compiler, debugger, project system, linter).
Typical compilers that come pre-installed on some platforms are:The GNU Compiler Collection(GCC) on Linux iMetallic soundtools withCodena macOS-ie.
To check if you already have them installed:
Open a new VS Code terminal window with (⌃⇧`(Windows, LinuxCtrl+Shift+`))
Use the following command to check the GCC compiler
g++
:g++ – version
Or this command for the Clang compiler
metallic sound
:clang - version
The output should show the compiler version and details. If none are found, make sure the compiler executable is in the platform path (%CAMINO
i okna,$RUTA
on Linux and macOS) so that the C/C++ extension can find them. Otherwise, use the instructions in the next section to install the compiler.
Install the compiler
If you don't have a compiler installed, you can follow one of our installation tutorials:
windows:
Go to the MSVC tutorial
Go to the MinGW tutorial
Linux:
Go to the GCC tutorial
Mac operating system:
Go to the Clang tutorial
Use: If you prefer a fully integrated development environment (IDE) with build, debug and built-in project templates (File > New Project), there are many options available such asVisual Studio Communityedition.
Example: Install MinGW-x64 on Windows
To understand the process, let's install Mingw-w64 viaMSYS2. Mingw-w64 is a popular and free toolkit for Windows. Includes up-to-date native builds of GCC, Mingw-w64, and other useful C++ tools and libraries.
download usingthis direct link to the MinGW installer.
Run the installer and follow the instructions of the installation wizard. Note that MSYS2 requires Windows 8.1 64-bit or later.
In the wizard, select the desired installation folder. Save this directory for later. In most cases, the recommended directory is acceptable. The same goes for configuring the Start Menu Shortcuts step. Make sure when you're doneStart MSYS2 nowthe box is checked and selectfinalize. The MSYS2 terminal window will open automatically.
In this terminal, install the MinGW-w64 toolkit by running the following command:
pacman -S --mingw-w64-x86_64-toolchain database development needed
Accept the default number of packages in the file
tool chain
group pressingWalk into.Walk into
Y
when prompted whether you want to continue with the installation.Add a route to MinGW-w64
paper basket
carpet and windowsCHIMNEY
environment variable by following these steps:- In the Windows search bar, typesettingsto open Windows settings.
- To look forEdit account environment variables.
- In yoursuser variables, Choose
Camino
variable, then selectedit. - To chooseNewand add the MinGW-w64 destination folder registered during the installation process to the list. If you chose the default installation steps, the path is:
C:\msys64\mingw64\bin
. - To chooseOKto save the updated PATH. for new
CHIMNEY
to make them available, reopen the console windows.
Verify that the MinGW-w64 tools are correctly installed and available, open the file anewCommand Prompt and type:
gcc --versiong++ – versiongdb --version
You should see output indicating which versions of GCC, g++ and GDB have been installed. If not, make sure the PATH entry matches the Mingw-w64 binary where the compiler tools are located, or refer totroubleshooting section.
Create a Hello World app
To ensure that the compiler is installed and configured correctly, let's create a Hello World C++ program.
Create a C++ file
- On Windows, run the Windows Command Prompt (EnterWindows symbolin the Windows search bar). On macOS and Linux, you can enter these commands in the terminal.
- Run the following commands. They create an empty folder called
Projects
where you can put all your VS Code projects. The following commands create it and move it to a subfolder namedHello World
. From there you openHello World
directly in VS Code usingcode
domain.
mkdirProjectsCDProjectsmkdirHello WorldCDHello Worldcode.
code command." opens VS Code in your current working folder, which becomes your "workspace". acceptTrust in the workplacedialog box selectionYes, I trust the authors.because this is the folder you created.
Now create a new file namedhelloworld.cpp
znew filebutton in File Explorer orArchives>new filedomain.
Add the Hello World source code
Paste the following source code:
#switch on and T main(){ sexually transmitted disease::exit <<"Hello World"<<sexually transmitted disease::endl;}
now press⌘S(Windows, LinuxCtrl+S)to save the file. You can also enableself-defenseto automatically save changes to the file by checkingautomatic savinggenerallyArchivesmenu.
Run helloworld.cpp
Make sure you have
helloworld.cpp
open to make it the active file in the editor.Press the play button in the upper right corner of the editor.
To chooseC/C++: g++.exe compiles and debugs the active filefrom the list of compilers detected on your system.
You will only be asked to select a compiler on first runhelloworld.cpp
. This compiler becomes the "default" compiler configured in the filetasks.json
archives.
After successful compilation, you should see "Hello World" in the integrated window.Terminal.
Congratulations! You just ran your first C++ program in VS Code! The next step is to learn more about Microsoft C/C++ extension language features such as IntelliSense, code navigation, build configuration, and debugging with one of the tutorials in the next section.
tutorial
Get started with C++ and VS Code with tutorials for your environment:
- GCC on Windows via MinGW
- Microsoft C++ on Windows
- CCG i Linux
- GCC in Windows Subsystem for Linux
- Clink/LLVM on macOS
- CMake tools on Linux
documentation
More documentation on using the Microsoft C/C++ extension can be found atC++ sectionfrom the VS Code website where you can find articles on:
- Debugowanie
- edition
- settings
- Frequently asked questions
remote development
Support for VS Code and C++ extensionsremote developmentallowing you to work via SSH on a remote machine or virtual machine, in or on a Docker containerWindows Subsystem for Linux(WSL).
To install remote development support:
- Install VS CodeRemote Programming Extension Pack.
- If remote source files are hosted on WSL, use fileWSLenlargement.
- If you are connecting to a remote machine using SSH, useRemote - SSHenlargement.
- If remote source files are hosted in a container (for example Docker), use filedevelopment containersenlargement.
Improve graduations with AI
The second GitHub pilotis an AI-powered code completion tool that helps you write code faster and smarter. you can useCopilot GitHub extensionin VS Code to generate code or learn from generated code.
GitHub Copilot provides guidance for many languages and a wide variety of frameworks, and is particularly good at Python, JavaScript, TypeScript, Ruby, Go, C#, and C++.
More information on getting started with Copilot can be found atCo-pilot documentation.
Comment
If you have any problems or suggestions for the Microsoft C/C++ extension, please upload the fileissues and suggestions on GitHub. If you haven't provided feedback yet, you can do soquick survey.
24.07.2023