C++ programming with Visual Studio Code (2024)

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.

C++ programming with Visual Studio Code (1)

Install the extension

  1. Open VS Code.
  2. Select the Extensions view icon on the Activity bar or use the keyboard shortcut (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
  3. Search for 'C++'.
  4. Select Install.

C++ programming with Visual Studio Code (2)

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

Common compilers that already come preinstalled on some platforms are the GNU Compiler Collection (GCC) on Linux and the Clang tools with Xcode on macOS.

To check if you already have them installed:

  1. Open a new VS Code terminal window using (⌃⇧` (Windows, Linux Ctrl+Shift+`))

  2. Use the following command to check for the GCC compiler g++:

    g++ --version

    Or this command for the Clang compiler clang:

    clang --version

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:

Windows:

Go to the MSVC tutorial

Go to the MinGW tutorial

Linux:

Go to the GCC tutorial

macOS:

Go to the Clang tutorial

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.

  1. Download using this direct link to the MinGW installer.

  2. Run the installer and follow the steps of the installation wizard. Note, MSYS2 requires 64 bit Windows 8.1 or newer.

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

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

    pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
  5. Accept the default number of packages in the toolchain group by pressing Enter.

    C++ programming with Visual Studio Code (3)

  6. Enter Y when prompted whether to proceed with the installation.

  7. Add the path of your MinGW-w64 bin folder to the Windows PATH environment variable by using the following steps:

    1. In the Windows search bar, type Settings to open your Windows Settings.
    2. Search for Edit environment variables for your account.
    3. In your User variables, select the Path variable and then select Edit.
    4. 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: C:\msys64\ucrt64\bin.
    5. Select OK, and then select OK again in the Environment Variables window to update the PATH environment variable.You have to reopen any console windows for the updated PATH environment variable to be available.
  8. Check that your MinGW-w64 tools are correctly installed and available, open a new Command Prompt and type:

gcc --versiong++ --versiongdb --version

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

  1. 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.
  2. Run the following commands. They are creating an empty folder called projects where 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 helloworld directly in VS Code using the code command.
mkdir projectscd projectsmkdir helloworldcd helloworldcode .

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.

C++ programming with Visual Studio Code (4)

Add Hello World source code

Paste in the following source code:

#include <iostream>int main(){ 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.

Run helloworld.cpp

  1. Make sure you have helloworld.cpp open so it is the active file in your editor.

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

    C++ programming with Visual Studio Code (5)

  3. Choose C/C++: g++.exe build and debug active file from the list of detected compilers on your system.

    C++ programming with Visual Studio Code (6)

You are only prompted to choose a compiler the first time you run helloworld.cpp. This compiler becomes "default" compiler set in your tasks.json file.

  1. After the build succeeds, you should see "Hello World" appear in the integrated Terminal.

    C++ programming with Visual Studio Code (7)

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.

Tutorials

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

Documentation

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:

  • Debugging
  • Editing
  • Settings
  • FAQ

C++ programming with Visual Studio Code (8)

Remote Development

VS Code and the C++ extension support Remote Development allowing you to work over SSH on a remote machine or VM, inside a Docker container, or in the Windows Subsystem for Linux (WSL).

To install support for Remote Development:

  1. Install the VS Code Remote Development Extension Pack.
  2. If the remote source files are hosted in WSL, use the WSL extension.
  3. If you are connecting to a remote machine with SSH, use the Remote - SSH extension.
  4. 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.

GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, and it works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++.

You can learn more about how to get started with Copilot in the Copilot documentation.

Feedback

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.

7/24/2023

C++ programming with Visual Studio Code (2024)

FAQs

Can I program in C++ with 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.

Is Visual Studio Code a good IDE for C++? ›

The case for Visual Studio Code

It comes with built-in support for JavaScript, TypeScript, and Node. js and has a rich ecosystem of extensions for other languages (such as C++, C#, Java, Python, PHP, and Go) and runtimes (such as . NET and Unity).

Is Visual Studio good for learning C++? ›

Visual Studio is one of the best IDEs for C++ as it provides powerful functionality for quickly writing code, debugging, and testing software.

What compiler does Visual Studio Code use for C++? ›

Using GCC with MinGW. In this tutorial, you configure Visual Studio Code to use the GCC C++ compiler (g++) and GDB debugger from mingw-w64 to create programs that run on Windows. After configuring VS Code, you will compile, run, and debug a Hello World program.

Is Visual Studio Code the same as Visual C++? ›

2 Answers. Visual Studio refers to Microsoft's Integrated Development Envrioment, It includes support for C++, C#, Basic, and several other languages. Visual-C++ refers to the C++ Specific component of visual-Studio. One can Install Visual-Studio (Express, or any other version), and install only the C++ component.

Is Visual Studio good for C++ GUI? ›

To develop C++ GUI or C++ graphical user interface application, you need an IDE that supports the C++ GUI application. To create the GUI app, you must use Visual Studio 2019 because it is better suited for the C++ GUI application.

How to run C++ on Visual Studio? ›

Simply open VS Code/VS Code Insiders, open any folder, and create any file with the extension .c for the C file and .cpp for the C++ file. After writing your code, you can run the code directly using the play button you'll find in the upper right corner.

What is the best IDE to use for C++? ›

Top 10 Best IDE for C++ 2024
  1. Visual Studio Code (VS Code) Visual Studio Code (VS Code) is one of the most widely used IDEs for C++. ...
  2. Eclipse. Eclipse is another popular C++ IDE that is easy to use for new programmers. ...
  3. NetBeans. ...
  4. Atom. ...
  5. Dev C++ ...
  6. CLion. ...
  7. QTCreator. ...
  8. Xcode.
May 8, 2024

What Visual Studio should I download for C++? ›

Visual Studio 2022

The most comprehensive IDE for .NET and C++ developers on Windows for building web, cloud, desktop, mobile apps, services and games.

Do professional programmers use Visual Studio? ›

Visual Studio is a development environment that programmers use to create websites, web applications, web services, and mobile applications. It is a powerful tool with great debugging and editing experience.

How to start with Visual Studio C++? ›

To create a C++ project in Visual Studio
  1. From the main menu, choose File > New > Project to open the Create a New Project dialog box.
  2. At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Console.
  3. From the filtered list of project types, choose Console App then choose Next.
Oct 29, 2021

Is it still valuable to learn C++? ›

Learning C++ is valuable for those interested in pursuing a career in this field. Embedded Systems: C is commonly used in embedded systems programming, where resources are constrained. If you're interested in working on devices like microcontrollers or IoT devices, knowledge of C is beneficial.

Can I write C++ in VS Code? ›

Once you create the file and VS Code detects it is a C++ language file, you may be prompted to install the Microsoft C/C++ extension if you don't already have it installed. Choose Install and then Reload Required when the button is displayed in the Extensions view to complete installing the C/C++ extension.

What is the difference between C++ and Visual C++? ›

C++ is a programming language, and it does not include a debugger, code editor, and project manager. Visual C++ includes many tools like debugger, code editor, project manager, etc. C++ programming language is mostly and commonly used to design hardware.

How to run a C program in VS Code? ›

Open the existing project in Visual Studio Code and simply select the play button at the top right corner. Then click on the 'run C/C++ file' button, given that the appropriate extension is installed. Here the extension name is the C/C++ extension pack.

Is Visual Studio for C++ only? ›

Visual Studio is an Integrated Development Environment, that helps developers efficiently write code in several programming languages, including C++. Visual C++ is the name of IDE used to develop applications written in C++. Other such tools are for example QtCreator or CLion. They don't compile, they are editors.

Can Visual Studio Code run any language? ›

In Visual Studio Code, we have support for almost every major programming language.

Does Visual Studio have its own C++ compiler? ›

Microsoft Visual C++ (MSVC) is a compiler for the C, C++, C++/CLI and C++/CX programming languages by Microsoft.

Is VS Code good for C programming? ›

It provides users to write code in a very easy way. Most programmers say that it is an IDE as well as an editor, but the final decision is up to the programmer. Earlier, the coding was done on old editors or even in basic editors such as notepad++ or sublime text.

References

Top Articles
*SOLD* 2021 Benelli Ethos 28ga Blue 26" *GREAT OPTION FOR SERIOUS UPLAND HUNTERS*
Kate Spade Outlet Altoona
排期 一亩三分地
Euro Jackpot Uitslagen 2024
Car Parts Open Now
University of Louisville Libraries on LinkedIn: #bannedbooks #censorship #uofl #firstamendment #studentlife #librarylife
895 Area Code Time Zone
Use Caution: Herds of wild horses escaping Davis Fire spotted evacuating up Geiger Grade
Tyson Employee Paperless
Jocelyne Mirando
Www.1Tamilmv.con
Fireboy And Watergirl Advanced Method
Old Navy Student Discount Unidays
Ingersoll Greenwood Funeral Home Obituaries
2006 Lebanon War | Summary, Casualties, & Israel
As Trump and Harris spar, ABC's moderators grapple with conducting a debate in a polarized country
Lots 8&amp;9 Oak Hill Court, St. Charles, IL 60175 - MLS# 12162199 | CENTURY 21
JPMorgan and 6 More Companies That Are Hiring in 2024, Defying the Layoffs Trend
Aston Carter hiring HR Specialist in Inwood, WV | LinkedIn
Rainbird E4C Manual
Fajr Azan Time Today
Starter Blocked Freightliner Cascadia
3850 Colonial Blvd Suite 100 Fort Myers Fl 33966
Tri-State Dog Racing Results
Hendraheim Skyrim
Vegamovies Home
How To Use Price Chopper Points At Quiktrip
Sdsu Office Of Financial Aid
OSRS F2P Melee Combat Guide: Fastest Way From 1-99
Channel 3000 News Madison Wisconsin
Dumb Money Showtimes Near Cinemark Century Mountain View 16
Goose Band Setlists
Fuzz Bugs Factory Hop Halloween
Inland Empire Heavy Equipment For Sale By Owner
Comcast Business Downdetector
Kytty_Keeet
Ny Lottery Second Chance App
Booknet.com Contract Marriage 2
Fandafia
Feetfinder Reviews Trustpilot
Kona Airport Webcam
Directions To Pnc Near Me
Thoren Bradley Lpsg
Breitling ENDURANCE PRO X82310E51B1S1 für 2.885 € kaufen von einem Trusted Seller auf Chrono24
Drew Gulliver Bj
Skid B Gon Brake Pads
Basis Phoenix Primary Calendar
The Starling Girl Showtimes Near Alamo Drafthouse Brooklyn
Texture Ids For Custom Glove In Slap Battles
Dominos Nijmegen Daalseweg
Lanipopvip
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 6399

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.