C/C++ support for Visual Studio Code is provided by aMicrosoft C/C++ extensionto enable cross-platform development of C and C++ on Windows, Linux and macOS.
Install the extension
- Open VS Code.
- Select the plugin view icon in the activity bar or use the keyboard shortcut (⇧⌘X(Windows, LinuxCtrl+Shift+X)).
- Search
'C++'
. - SelectTo install.
After installing the extension, when you open or create one*.cpp
file, it has syntax highlighting (coloring), smart completions and offsets (IntelliSense), and error checking.
Install a compiler
C++ is a compiled language, which means that your program's source code must be translated (compiled) before it can run on your computer. Primarily an editor, VS Code relies on command-line tools to perform much of the development workflow. The C/C++ extension does not include a C++ compiler or debugger. You must install these tools or use the ones already installed on your computer.
There may already be a C++ compiler and debugger provided by your academic or professional development environment. Check with your instructors or peers for instructions on installing the recommended C++ toolset (compiler, debugger, project system, linter).
A C++ compiler is already installed on some platforms such as Linux or macOS. Most Linux distributions have theseGNU Compiler Collection(GCC) installed and macOS users can use themetallic soundtools withx-Code.
Check if you have a compiler installed
Make sure your compiler executable is in your platform path (%CAMINO
and window,$RUTA
on Linux and macOS) so the C/C++ extension can find them. You can check the availability of your C++ tools by opening the built-in terminal (⌃`(Windows, LinuxCtrl+`)) in VS Code and try to run the compiler directly.
Review of the GCC compilerg ++
:
g++ --version
Clang compiler reviewmetallic sound
:
clang --version
Use: If you prefer a fully integrated development environment (IDE) with build, debug and built-in project templates (File > New Project), you have many options such as:Visual Studio-Communityedition.
If you don't have a compiler installed, in the following example we describe how to install the GNU Minimalist Tools for Windows (MinGW) C++ (compiler and debugger). MinGW is a popular free toolkit for Windows. If you're running VS Code on another platform, you can use theC++-Tutorials, covering C++ configurations for Linux and macOS.
Example: Install MinGW-x64
We will install Mingw-w64 viaMSYS2, which provides current native builds of GCC, Mingw-w64 and other useful C++ tools and libraries. You can download or use the latest installer from MSYS2 siteLink to installer.
follow thatInstallationinstructions on theMSYS2-Websiteto install Mingw-w64. Be sure to run any required Start Menu andPac-Mann
Domain.
You need to install the full mingw-w64 toolchain (pacman -S --necesita base-devel mingw-w64-x86_64-toolchain
) to get thatgdb
Washer
Add the MinGW compiler to your path
Add the path to your mingw-w64compartment
Windows folderCAMINO
environment variable with the following steps:
- Type “settings” in the Windows search bar to open Windows settings.
- SearchEdit environment variables for your account.
- Select the
Camino
variable in youruser variablesand then selectTo edit. - SelectNeuand 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 configuration above to install mingw-w64, add this to the path:C:\msys64\mingw64\bin
. - SelectOKto save the updated PATH. You must reopen all console windows for the new PATH location to be available.
Check your MinGW installation
To verify that your mingw-w64 tools are correctly installed and available, open aneucommand prompt and type:
gcc --versiong++ --versiongdb --version
If you do not see the expected result org ++
Ögdb
is not a recognized command, make sure your PATH entry matches the mingw-w64 binary location where the compiler tools reside.
If the compilers are not present in this PATH entry, be sure to follow the instructions in theMSYS2-Websiteto install Mingw-w64.
Hello World
To make sure the compiler is installed and configured correctly, let's create the simpler Hello World C++ program.
Create a folder named "HelloWorld" and open VS Code in that folder (Code
Open VS Code in current folder):
mkdir hello worldCDHello WorldCode.
The code ." command opens VS Code in the current working folder, which becomes your "workspace". Accept theWorkspace TrustSelect dialog boxYes, I trust the authors.since this is a folder that you created.
Now create a new file namedhallowelt.cpp
with thenew fileFile Explorer button orarchive>new fileDomain.
Add the Hello World source code
Now paste this source code:
#contain <iostream>and t principal(){ Standard::cout<<"Hello World"<<Standard::endl;}
press now⌘S(Windows, LinuxControl + S)to save the file. You can also activatesave automaticallyto automatically save changes to your file by markingsave automaticallygeneralarchiveMenu.
build hello world
Now that we have a simple C++ program, let's build it. ChooseTerminal>Run build taskdomain (⇧⌘B(Windows, LinuxCtrl+Shift+B)) from the main menu.
This will bring up a drop down menu with various compiler task options. If you use a GCC toolkit like MinGW, I would chooseC/C++: active build file g++.exe.
This will compilehallowelt.cpp
and create an executable calledholamundo.exe
that appears in File Explorer.
run hello world
From a command prompt or VS Code's new built-in terminal, you can now run your program by typing ".\helloworld".
If everything is set up correctly, you should see the "Hello World" output.
This was a very simple example to get you started with C++ development in VS Code. The next step is to try one of the tutorials below on your platform (Windows, Linux, or macOS) with your favorite toolset (GCC, Clang, Microsoft C++) and learn more about the language capabilities of the extension. Microsoft C/C++, such as IntelliSense. , code navigation, build configuration, and debugging.
Tutorials
Get started with C++ and VS Code with tutorials for your environment:
- GCC on Windows via MinGW
- Microsoft C++ and Windows
- CCG unter Linux
- GCC on the Windows Subsystem for Linux
- Clang/LLVM on macOS
- CMake-Tools unter Linux
Documentation
For more documentation on using the Microsoft C/C++ extension, seeC++ sectionfrom the VS Code website, where you can find topics on:
- Debugging
- edition
- the settings
- frequently asked Questions
remote development
VS Code and C++ extension supportremote developmentSo you can work over SSH on a remote machine or VM, in a Docker container or on theWindows subsystem for Linux(WSL).
To install remote development support:
- Install VS CodeExtension pack for remote development.
- If the remote source files are hosted on WSL, use theWSLRenewal.
- If you're connecting to a remote computer with SSH, use theRemoto – SSHRenewal.
- If the remote source files are hosted in a container (e.g. Docker), use thedevelopment containerRenewal.
Improve degrees with AI
GitHub-Co-Pilotis an AI-powered code completion tool that helps you write code faster and smarter. you can use the...GitHub Copilot extensionin VS Code to generate code or learn from the generated code.
GitHub Copilot has suggestions for many languages and a variety of frameworks, and works particularly well for Python, JavaScript, TypeScript, Ruby, Go, C#, and C++.
For more information on getting started with Copilot, see theCo-Pilot Documentation.
Comment
If you have any problems or suggestions for Microsoft C/C++ extension, please send them to usIssues and suggestions on GitHub. If you haven't provided feedback yet, please accept itquick surveyto customize this extension for your needs.
21.01.2022