Programming in C++ using Visual Studio Code (2023)

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*.cppthe extension adds features such as syntax highlighting (coloring), intelligent completion and scrolling (IntelliSense), and error checking.

Programming in C++ using Visual Studio Code (1)

Install the extension

  1. Open VS Code.
  2. Select the extensions view icon in the activity bar or use the keyboard shortcut (⇧⌘X(Windows, LinuxCtrl+Shift+X)).
  3. To look for„C++”.
  4. To chooseinstall.

Programming in C++ using Visual Studio Code (2)

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:

  1. Open a new VS Code terminal window with (⌃⇧`(Windows, LinuxCtrl+Shift+`))

  2. Use the following command to check the GCC compilerg++:

    g++ – version

    Or this command for the Clang compilermetallic 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 (%CAMINOi okna,$RUTAon 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.

  1. download usingthis direct link to the MinGW installer.

  2. Run the installer and follow the instructions of the installation wizard. Note that MSYS2 requires Windows 8.1 64-bit or later.

  3. 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.

  4. In this terminal, install the MinGW-w64 toolkit by running the following command:

    pacman -S --mingw-w64-x86_64-toolchain database development needed
  5. Accept the default number of packages in the filetool chaingroup pressingWalk into.

    Programming in C++ using Visual Studio Code (3)

  6. Walk intoYwhen prompted whether you want to continue with the installation.

  7. Add a route to MinGW-w64paper basketcarpet and windowsCHIMNEYenvironment variable by following these steps:

    1. In the Windows search bar, typesettingsto open Windows settings.
    2. To look forEdit account environment variables.
    3. In yoursuser variables, ChooseCaminovariable, then selectedit.
    4. 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.
    5. To chooseOKto save the updated PATH. for newCHIMNEYto make them available, reopen the console windows.
  8. 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

  1. 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.
  2. Run the following commands. They create an empty folder calledProjectswhere 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 Worlddirectly in VS Code usingcodedomain.
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.cppznew filebutton in File Explorer orArchives>new filedomain.

Programming in C++ using Visual Studio Code (4)

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

  1. Make sure you havehelloworld.cppopen to make it the active file in the editor.

  2. Press the play button in the upper right corner of the editor.

    Programming in C++ using Visual Studio Code (5)

  3. To chooseC/C++: g++.exe compiles and debugs the active filefrom the list of compilers detected on your system.

    Programming in C++ using Visual Studio Code (6)

You will only be asked to select a compiler on first runhelloworld.cpp. This compiler becomes the "default" compiler configured in the filetasks.jsonarchives.

  1. After successful compilation, you should see "Hello World" in the integrated window.Terminal.

    Programming in C++ using Visual Studio Code (7)

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

Programming in C++ using Visual Studio Code (8)

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:

  1. Install VS CodeRemote Programming Extension Pack.
  2. If remote source files are hosted on WSL, use fileWSLenlargement.
  3. If you are connecting to a remote machine using SSH, useRemote - SSHenlargement.
  4. 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

References

Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated: 09/20/2023

Views: 5857

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.