Visual Studio Code for C++ on Linux

To be a productive developer good editor/IDE is essential. On Windows nothing could beat Microsoft Visual Studio (in my opinion). On Linux I don’t have enough experience to make decision yet.

For small projects Microsoft Visual Studio Code looks promising. Please note that VS Code is an editor, it does not have build environment, so for any language you want it to use you will have to install build environment separately: sudo apt-get install -y build-essential git cmake.

I’ll help you to give it a try. Installation is very simple, just follow instructions on Microsoft page, or use OS GUI tool (such as Ubuntu Software) to install it.

Next thing we’ll need to prime it for C++ development. We will create directory for our first project and start VS Code in it:

mkdir hellocode && cd hellocode
code .

Visual Studio Code application should start and in the Explorer tree it should point to newly create folder. Now we need to install some plugins. On the left side click Extensions and then type C++ in Search Marketplace box. Select C/C++ extension from Microsoft and click Install. We don’t need it right now, but since we are here let’s install extension for CMake – type cmake in Search Marketplace box and install CMake Tools and CMake Tools Helper.

Next thing we need to do is configure VS Code to be our C++ IDE. On the left side switch back to Explorer view. Press Ctrl+Shift+P to open command tool and start typing c++, then in drop down list select “C/C++: Edit Configuration(UI)”. Make sure that Compiler Path is not empty (if it is you need to install build package)

You will notice that in our project tree on the left side new folder appeared and inside new file were created: c_cpp_properties.json – this is our project configuration files.

But we are not done yet with configuration. Next we need configure build task and debug task. Press Ctrl+Shift+P to open command tool and start typing task, then in drop down list select “Tasks: Configure Default Build Task”. Select “Create tasks.json file from template” and select “Others”. tasks.json file will be created with some default content, let’s edit it. Change ‘label’ to “build (debug)”, change ‘command’ to “g++”, and we need add two new parameters: ‘args’ and ‘group’. Notice that Visual Studio Code has intelligent editor and will help you along the way. Here is the resulting file:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "g++",
            "args": ["-g","hellocode.cpp"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

And the last step in the configuration is configure debugging. Press Ctrl+Shift+P to open command tool and start typing Debug, then in drop down list select “Debug: Open launch.json”, then in drop down list select “C++ (GDB/LLDB)”. Change parameter ‘program’ to “${workspaceFolder}/a.out”.

After that in the Explorer tree you should see HELLOCODE folder with a subfolder .vscode with three files: c_cpp_properties.json, launch.json, and tasks.json.

Finally we can start programming. Click New File icon on the left of HELLOCODE folder and name this file hellocode.cpp (please note if .vscode folder is selected/highlighted then new file will be created in that folder, we don’t want to do it, so you want click outside .vscode to deselect it. If by mistake you created new file inside .vscode just drag-n-drop it outside .vscode )

#include <iostream>

int main()
{
    std::cout << "Hello from Visual Studio Code" << std::endl;

    return 0;
}

We can build our application now: press Ctrl+Shift+B or press Ctrl+Shift+P to open command tool and start typing tasks and select “Tasks: Run Build Task”.  Once the build starts, you should see Terminal window appear below the code editor. 

Let’s try debug our application. First we will put a breakpoint – hover with a mouse on the left of line numbers – you should see red dots appear and disappear. Click on one on line 5 (std::cout…) – dot will stay.

Now press F5 to start debugger, debugging session will start and debugger will stop at line 5 which will be highlighted in yellow. Press F5 again to run application again.

And this will conclude out tutorial.

Leave a Reply