How to Run C++ Code in Visual Studio Code

  1. Open Visual Studio Code.


  1. Go to the ‘Extensions’ tab, search for c++ and install the ‘C/C++ Extension Pack’.


  1. Create a ‘hello_world.cpp’ file, write the code below, and save it.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

  1. Click ‘Run Build Task’ in the ‘Terminal’ tab. <Shortcut key: Control + Shift + B>


  1. Build using g++.exe.


  1. If the above message appears in the terminal, the build is successful.


  1. You can also see that an .exe file with the same name as the .cpp file has been created.


  1. Open a new terminal <Shortcut key: Control + Shift + `> and run the ‘.\hello_world.exe’ file.


  1. If you get the above result, you have successfully run the example code.