C++ Programming with Visual Studio Code (2023)

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.

C++ Programming with Visual Studio Code (1)

Install the extension

  1. Open VS Code.
  2. Select the plugin view icon in the activity bar or use the keyboard shortcut (⇧⌘X(Windows, LinuxCtrl+Shift+X)).
  3. Search'C++'.
  4. SelectTo install.

C++ Programming with Visual Studio Code (2)

After installing the extension, when you open or create one*.cppfile, it has syntax highlighting (coloring), smart completions and offsets (IntelliSense), and error checking.

C++ Programming with Visual Studio Code (3)

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 (%CAMINOand window,$RUTAon 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-MannDomain.

You need to install the full mingw-w64 toolchain (pacman -S --necesita base-devel mingw-w64-x86_64-toolchain) to get thatgdbWasher

Add the MinGW compiler to your path

Add the path to your mingw-w64compartmentWindows folderCAMINOenvironment variable with the following steps:

  1. Type “settings” in the Windows search bar to open Windows settings.
  2. SearchEdit environment variables for your account.
  3. Select theCaminovariable in youruser variablesand then selectTo edit.
  4. SelectNeuand add the mingw-w64 destination folder path, with\mingw64\binappended 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.
  5. 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 ++Ögdbis 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 (CodeOpen 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.cppwith thenew fileFile Explorer button orarchive>new fileDomain.

C++ Programming with Visual Studio Code (4)

C++ Programming with Visual Studio Code (5)

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.

C++ Programming with Visual Studio Code (6)

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.

C++ Programming with Visual Studio Code (7)

This will compilehallowelt.cppand create an executable calledholamundo.exethat appears in File Explorer.

C++ Programming with Visual Studio Code (8)

run hello world

From a command prompt or VS Code's new built-in terminal, you can now run your program by typing ".\helloworld".

C++ Programming with Visual Studio Code (9)

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

C++ Programming with Visual Studio Code (10)

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:

  1. Install VS CodeExtension pack for remote development.
  2. If the remote source files are hosted on WSL, use theWSLRenewal.
  3. If you're connecting to a remote computer with SSH, use theRemoto – SSHRenewal.
  4. 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.

C++ Programming with Visual Studio Code (11)

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

References

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated: 24/12/2023

Views: 5905

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.