Setting Up C++ In Visual Studio Code

Posted on  by 



The recommended development environment for C++ programming in CPTR 124 and CPTR 318 this semester is Microsoft's Visual Studio Code (VSCode). VSCode is available for free for Microsoft Windows, Apple macOS, and Linux. Many other fine C++ integrated development environments exist for these platforms, but each has its own peculiar way of configuring projects for editing, building, debugging, and executing C++ programs. The process of setting up a C++ project involving external libraries and other dependencies sometimes can become complicated, and it can be tedious to diagnose and correct configuration errors in a multitude of different development systems. Students using VSCode can be assured that they will receive timely support when things are not working as they should.

Setting Up C++ In Visual Studio Code

VSCode is a programming editor coupled with a lightweight project management system. VSCode itself does include the tools necessary for compiling and debugging C++. You must install the C++ development tools (compiler, linker, and debugger) separately and then configure VSCode to use these tools. Fortunately, installing the C++ tools and VSCode and configuring everything to work for our purposes is relatively easy on Windows, macOS, and Linux.

In this tutorial, you will learn How to Set up Visual Studio Code for Creating, Executing and Debugging C Programs.00:00 Introduction00:36 Check g and gd. You can use the File Template extension. C files are not included in its default list of supported file types. But it does support adding your own templates for known VS Code language identifiers, such as.cpp for C files. To set it up, follow the instructions on the extension's page.

This document explains how to install and configure the necessary C++ development tools and VSCode on Windows, macOS, and Linux.

  • Microsoft Windows

    We will use a slightly enhanced version of Stephan T. Lavavej's distribution of the MinGW C++ compiler. Stephan's website has his current MinGW mix, but you should download and unzip MinGW-124.zip. This remix has an additional graphics library (GLUT) and a slightly modified console batch file to integrate better with VSCode. This MinGW package contains all the tools necessary for command-line development with C++ under Windows.


    Setting Up VSCode for C++ Development


    The following describes the necessary steps to install the VSCode enviroment for C++ development:

    1. Install the MinGW C++ tools.
      1. Download the file MinGW-124.zip.
      2. In a Windows Explorer window, right click on the file and select 'Extract All ...' In the ensuing dialog box be sure to type C: in the text field that specifies the folder into which the files will be extracted.

        It is important that you use this exact path, or else the VSCode configuration files will not be able to find the C++ toolset.

      3. Test your MinGW tools installation: In an explorer window, double click on the file C:MinGWvs_code_terminal.bat. You then should see a console window appear. In the console window at the C:MinGW> prompt type the command g++ -v:

        (The C:MinGW> part is prompt provided by the console, and you type only the part that follows.)

        The console window should respond by printing something similar to the following:

        Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/7.2.0/lto-wrapper.exe Target: x86_64-w64-mingw32 Configured with: ../src/configure --enable-languages=c,c++ --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-multilib --prefix=/c/temp/gcc/dest --with-sysroot=/c/temp/gcc/dest --disable-libstdcxx-pch --disable-libstdcxx-verbose --disable-nls --disable-shared --disable-win32-registry --with-tune=haswell --enable-threads=posix --enable-libgomp Thread model: posix gcc version 7.2.0 (GCC) C:MinGW>

        If instead you see

        'g++' is not recognized as an internal or external command, operable program or batch file. C:MinGW>

        then something went wrong, and you should ask for help to fix it before continuing with the steps that follow.

      This completes the installation of the MinGW C++ toolset.

    2. Install VSCode.

      Download the VSCode installer (https://code.visualstudio.com/), and install it as you would any other Windows application. It then should appear in your start menu as 'Visual Studio Code.' You can pin it to your large start menu or to your taskbar for quicker access. Launch VSCode to ensure that it installed correctly. VSCode should display a welcome screen similar to the one as shown here:

      We are not quite ready to start C++ programming, but we almost are there.

      This completes the installation of VSCode.

    3. Work with the sample C++ project.
      1. Download the archived sample C++ project, firstprogram.zip. Unzip the firstprogram.zip file. This creates a folder named FirstProgram, and in that folder you will find two files and and subfolder:
        • starter.cpp contains the source code for a very simple C++ program.
        • Makefile is a configuration file that specifies how to build the executable program from the source file.
        • .vscode is a folder that contains several files that allow VSCode to work properly with the MinGW C++ toolset.

        The file starter.cpp contains the following C++ program:

        #include <iostream> int main() { std::cout << 'Our first C++ programn'; }
      2. Launch VSCode. In the File menu, select 'Open Folder ...' and navigate to the FirstProgram folder you just downloaded and unzipped. Direct VSCode to open this FirstProgram folder.
      3. From the VSCode Explorer pane on the left you can open the starter.cpp source file in a editor by double-clicking on it.
      4. You can build and run the program as follows:
        1. Hold down the Shift and Ctrl keys and press the C key. This brings up a console window.
        2. In the console window type the command make run. This command will attempt to build the executable program from the source file, and, if it is successful, it will run the program.
        3. The program should print

    In summary, the following three steps set up VSCode for C++ development under Microsoft Windows:

    1. Unzip the custom MinGW.zip archive into C:
    2. Install VSCode
    3. Obtain a copy of an existing working C++ VSCode project folder

    You need do these three set-up steps only once.

    That is all there is to setting up VSCode for C++ development. Next we examine the typical workflow for creating a new C++ project.


    Using VSCode


    After you have created your first C++ project, creating another one is very simple. To see how simple it is, let's create a new VSCode C++ project and supply our own C++ program.

    You will follow steps similar to the following each time you want to write a C++ program using VSCode:

    1. Copy the folder and its contents of an existing VSCode C++ project. Since we currently have only one VSCode C++ project (FirstProgram), use Window Explorer to make a copy of the FirstProgram folder. For the sake of this exercise, name it SecondProgram.
    2. Open the copied folder in VSCode. In this case open the SecondProgram folder in VSCode. At this point it is an exact copy of the original project, FirstProgram.
    3. Rename the C++ source file. Since this is a new program it is a good idea to give the source file a different name. Right click on starter.cpp in VSCode's Explorer pane. Select 'Rename' to change the file's name to secondprogram.cpp.

      (This step technically is not necessary. Since each C++ source file will be in its own project folder, the source files could all have the same name. For example, program.cpp in the project folder Project1 could be an entirely different program than in the file program.cpp in the Project2 project folder.)

    4. Edit the Makefile. The Makefile is designed to work with the original project, and we need to make a small change to adapt it to this new project. The Makefile is expecting a C++ source file named starter.cpp, but we just renamed it to be secondprogram.cpp. Open Makefile in the VSCode editor. Change the first line from
      PROGNAME = starter.cpp
      to

      (Note that this step is unnecessary if you did not rename the source file in the previous step.)

    5. Edit the C++ source file. We want our new program in this SecondProgram to behave differently from the one in our FirstProgram project. Edit the secondprogram.cpp file so that it will display the following output when executed:
      *** This is our second C++ program! ***
    6. Build and run the program. Bring up a VSCode console window (press Shift + Ctrl + C) and type make run. If the program does not build properly or does run as expected, return to the VSCode editor and make changes to the source code to fix build errors and/or correct the program's errant behavior. It may take many edit-build-run cycles to achieve the finished product that runs correctly.


    Summary


    In summary, the following three steps set up VSCode for C++ development under Microsoft Windows:

    1. Unzip the custom MinGW.zip archive into C:
    2. Install VSCode
    3. Obtain a copy of an existing working C++ VSCode project folder

    You need do these three set-up steps only once.

    Once set up, the following three steps create a new C++ project in VSCode:

    1. Copy an existing working C++ VSCode project
    2. Rename the C++ source file (optional but advisable)
    3. Edit the first line of the Makefile to reflect the C++ source file's new name (not necessary if you skipped Step 2)
    4. Edit the C++ source file to implement the desired program

    You perform these four steps every time you wish to write a new program using VSCode.

    To build and run a C++ program under VSCode, bring up a VSCode console window (press Shift + Ctrl + C) and type make run. Repeat the edit-build-run sequence until the program behaves correctly.

Getting up and running with Visual Studio Code is quick and easy. It is a small download so you can install in a matter of minutes and give VS Code a try.

Cross platform

VS Code is a free code editor, which runs on the macOS, Linux, and Windows operating systems.

Follow the platform-specific guides below:

VS Code is lightweight and should run on most available hardware and platform versions. You can review the System Requirements to check if your computer configuration is supported.

Update cadence

Setup c++ in visual studio code ubuntu

VS Code releases a new version each month with new features and important bug fixes. Most platforms support auto updating and you will be prompted to install the new release when it becomes available. You can also manually check for updates by running Help > Check for Updates on Linux and Windows or running Code > Check for Updates on macOS.

Note: You can disable auto-update if you prefer to update VS Code on your own schedule.

Insiders nightly build

If you'd like to try our nightly builds to see new features early or verify bug fixes, you can install our Insiders build. The Insiders build installs side-by-side with the monthly Stable build and you can freely work with either on the same machine. The Insiders build is the same one the VS Code development team uses on a daily basis and we really appreciate people trying out new features and providing feedback.

Portable mode

Visual Studio Code supports Portable mode installation. This mode enables all data created and maintained by VS Code to live near itself, so it can be moved around across environments, for example, on a USB drive. See the VS Code Portable Mode documentation for details.

Additional components

VS Code is an editor, first and foremost, and prides itself on a small footprint. Unlike traditional IDEs that tend to include everything but the kitchen sink, you can tune your installation to the development technologies you care about. Be sure to read the Additional Components topic after reading the platform guides to learn about customizing your VS Code installation.

Extensions

C++

VS Code extensions let third parties add support for additional:

  • Languages - C++, C#, Go, Java, Python
  • Tools - ESLint, JSHint , PowerShell
  • Debuggers - Chrome, PHP XDebug.
  • Keymaps - Vim, Sublime Text, IntelliJ, Emacs, Atom, Brackets, Visual Studio, Eclipse

Extensions integrate into VS Code's UI, commands, and task running systems so you'll find it easy to work with different technologies through VS Code's shared interface. Check out the VS Code extension Marketplace to see what's available.

Next steps

Once you have installed and set up VS Code, these topics will help you learn more about VS Code:

  • Additional Components - Learn how to install Git, Node.js, TypeScript, and tools like Yeoman.
  • User Interface - A quick orientation to VS Code.
  • Basic Editing - Learn about the powerful VS Code editor.
  • Code Navigation - Move quickly through your source code.
  • Debugging - Debug your source code directly in the VS Code editor.
  • Proxy Server Support - Configure your proxy settings.

If you'd like to get something running quickly, try the Node.js tutorial walkthrough that will have you debugging a Node.js web application with VS Code in minutes.

Common questions

What are the system requirements for VS Code?

We have a list of System Requirements.

How big is VS Code?

VS Code is a small download (< 100 MB) and has a disk footprint of less than 200 MB, so you can quickly install VS Code and try it out.

How do I create and run a new project?

VS Code doesn't include a traditional File > New Project dialog or pre-installed project templates. You'll need to add additional components and scaffolders depending on your development interests. With scaffolding tools like Yeoman and the multitude of modules available through the npm package manager, you're sure to find appropriate templates and tools to create your projects.

How do I know which version I'm running?

On Linux and Windows, choose Help > About. On macOS, use Code > About Visual Studio Code.

Why is VS Code saying my installation is Unsupported?

VS Code has detected that some installation files have been modified, perhaps by an extension. Reinstalling VS Code will replace the affected files. See our FAQ topic for more details.

How can I do a 'clean' uninstall of VS Code?

If you want to remove all user data after uninstalling VS Code, you can delete the user data folders Code and .vscode. This will return you to the state before you installed VS Code. This can also be used to reset all settings if you don't want to uninstall VS Code.

Code C++ On Visual Studio

The folder locations will vary depending on your platform:

C In Visual Studio

  • Windows - Delete %APPDATA%Code and %USERPROFILE%.vscode.
  • macOS - Delete $HOME/Library/Application Support/Code and ~/.vscode.
  • Linux - Delete $HOME/.config/Code and ~/.vscode.




Coments are closed