VSCode

I switched to another IDE, and will probably not be updating this. I'll still leave this here JIC it can help someone.

Defining Build Tasks

Official Tasks Documentation

In the menu bar, navigate to Terminal->Configure Tasks.., it should create a .vscode/tasks.json file and open it for editing. Once you define a task within this file, it can be ran using Ctrl+P and typing task taskname where taskname is the value stored at "label": within the tasks.json configuration.

For example, running a task with "label": "cpp build active file" would be done by Ctrl+P and typing task cpp build active file.

Variable Reference

Straight from the Official Documentation, for quick reference -

${workspaceFolder} - the path of the folder opened in VS Code
${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
${file} - the current opened file
${relativeFile} - the current opened file relative to workspaceFolder
${relativeFileDirname} - the current opened file's dirname relative to workspaceFolder
${fileBasename} - the current opened file's basename
${fileBasenameNoExtension} - the current opened file's basename with no file extension
${fileDirname} - the current opened file's dirname
${fileExtname} - the current opened file's extension
${cwd} - the task runner's current working directory on startup
${lineNumber} - the current selected line number in the active file
${selectedText} - the current selected text in the active file
${execPath} - the path to the running VS Code executable
${defaultBuildTask} - the name of the default build task

G++ Active File

Using G++ to build the active C++ file-

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++ build active file",
      "command": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\g++.exe",
      "args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
      "options": {
        "cwd": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

G++ Projects

Using G++ to compile and build specific source code

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++ build",
            "command": "g++",
            "args": [
                "${workspaceFolder}/test.cpp",
                // This project is using and linking the SDL library for graphics programming
                "-lSDL",
                "-o",
                "${workspaceFolder}/test-exe",
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Make Projects

Using Make to build a project with a preconfigured Makefile

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "make",
            "type": "shell",
            "command": "make",
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        },
        {
            "label": "make-clean",
            "type": "shell",
            "command": "make clean",
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

You'll notice by the above, a major benefit to building with a system like Make or CMake is the removal of the following section

reduced...
            "args": [
                "${workspaceFolder}/test.cpp",
                "-lSDL",
                "-o",
                "${workspaceFolder}/test-exe",
            ],
reduced...

Since Make and CMake builds can both handle most of the operations within args we can generally remove the args section.

Launching Debuggers

You might need to install the GDB Debug extension if you want to use GDB. Once you have installed this, navigate to the debugger panel and click create a launch.json file within VSCode

    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

We should pay close attention to the program line as it contains a sentence for the option by default. At the very least, you will have to replace "program": "enter program name, for example ${workspaceFolder}/a.exe" with "program": "${workspaceFolder}/a.exe", or otherwise point it to the executable you want to debug.

miDebuggerPath requires a path to gdb, but on linux this isn't needed and I've never had to configure it manually. Leaving MIMode set to gdb will automatically search for the gdb executable on your system. So, if you are on Linux, just be sure gdb is installed and remove this line.

The only other requirement here is to change the name of the launch task to a relevant one. This is completely up to you and can be whatever you want. Once this is done, navigate to the debugger within the side panel of VSCode and notice the drop-down at the top now contains this name, allowing you to launch your application and attach a debugger within VSCode. Select your launch task and click the play button to run it.

Extensions

Here's a list of extensions that I use. They're pretty neat. ;)

Windows

Since I do find myself digging through a lot of Microsoft Forums. This section will be a collection of problems and solutions I've found in configuring development for C / C++ on Windows using VSCode. Mostly, this section will act as a quick cheat sheet for the times I need to get setup and working on a Windows machine.

Windows SDK

This can be fixed by installing the Windows 10 SDK. Go Here and download the Windows 10 SDK.

Restart VSCode if you are still running it and it will detect the changes to your system.

Replace PS with Bash

Ctrl+Shift+P to open a command prompt within vscode, type user settings and open the user settings. There should be a terminal section like the below, defining a terminal for each operating system. Click Edit in settings.json for the Integrated Windows shell.

This will open a settings.json file. Change the terminal.integrated.shell.windows and terminal.external.windowsExec to point to C:\\WINDOWS\\System32\\wsl.exe, or the relative path to WSL on your system. My configuration is seen below.

{
    "editor.tabCompletion": "on",
    "editor.acceptSuggestionOnCommitCharacter": false,
    "editor.acceptSuggestionOnEnter": "off",
    "window.zoomLevel": 0,
    "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\wsl.exe",
    "terminal.external.windowsExec": "C:\\WINDOWS\\System32\\wsl.exe",
    "terminal.integrated.automationShell.windows": ""
}

Otherwise if you dont want to use WSL you can download Git For Windows and use the absolute path to the included Git Bash shell. Note if you do this that you will also not need to follow the section to Open VSCode in WSL

Run GDB using WSL

If using Git Bash, you do not need to follow this section Skip to Run GDB using MinGW

Install GDB

Run the following within a WSL Bash terminal to install GDB on WSL

sudo apt update && sudo apt upgrade
sudo apt install gdb g++ make cmake

Verify installation location, we will need this when configuring our launch.json

which gdb

For me the path is /usr/bin/gdb

Open VSCode in WSL

Click the green button at the bottom left of the screen

This drop-down will pop up in the top center of your screen

Select Remote-WSL: Reopen Folder in WSL to open your VSCode project under WSL. After VSCode reopens the project you should notice a change to the bottom left of your window -

For some extensions, like C / C++ which provides tools to debug using gdb, you will need to reinstall them under WSL. Be sure to do so before continuing.

Configure launch.json for WSL

When defining a launch.json on Windows using the default C / C++ (gdb) Launch configuration generated by VSCode, you'll start with the following

    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

This configuration will not work in any scenario.

Note that miDebuggerPath is an actual system path pointing to your gdb executable. When using WSL, this can be set to a unix file path and directly point to /usr/bin/gdb. An example launch.json that works with WSL is seen below

    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/driver.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

We should pay close attention to the program line as it contains a sentence for the option by default. At the very least, you will have to replace "program": "enter program name, for example ${workspaceFolder}/a.exe" with "program": "${workspaceFolder}/a.exe", or otherwise point it to the executable you want to debug.

The only other requirement here is to change the name of the launch task to a relevant one. This is completely up to you and can be whatever you want. Once this is done, navigate to the debugger within the side panel of VSCode and notice the drop-down at the top now contains this name, allowing you to launch your application and attach a debugger within VSCode. Select your launch task and click the play button to run it.

Run GDB using MinGW

If using WSL, you do not need to follow this section Skip to Run GDB using WSL

Install MinGW for GDB

When defining a launch.json on Windows using the default C / C++ (gdb) Launch configuration generated by VSCode, you'll start with the following

    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

This configuration will not work in any scenario.

Note that miDebuggerPath is an actual system path pointing to your gdb executable. To install GDB on Windows, head over to the MinGW Downloads page and download the latest release of MinGW. In the setup process, you will be greeted with the below window to configure installation settings.

In the left pane, navigtate to All Packages and scroll down to select the below packages for installation

Once you've marked all of these for installation, in the menu bar at the top of the application select Installation->Apply Changes. This will download and apply the package updates to your system.

Now within MinGW, the packages should be marked as installed by appearing as below

Now depending on where you chose to install MinGW, you will have some directory similar to C:\MinGW\bin on your system. Navigate there, and verify that the gdb executable exists

Now set your systems PATH environment variable to include C:\MinGW\bin or the relevant path on your system

Configure launch.json for MinGW

Finally, we can finish our launch.json by adjusting the miDebuggerPath accordingly. Starting with -

    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/path/to/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

Note that miDebuggerPath is an actual system path pointing to your gdb executable. When using MinGW, this should be set to a Windows file path and point to C:\\MinGW\\bin\\gdb.

We should pay close attention to the program line as it contains a sentence for the option by default. At the very least, you will have to replace "program": "enter program name, for example ${workspaceFolder}/a.exe" with "program": "${workspaceFolder}/a.exe", or otherwise point it to the executable you want to debug.

The only other requirement here is to change the name of the launch task to a relevant one. This is completely up to you and can be whatever you want.

An example launch.json that works with MinGW is seen below

    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/driver.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\MinGW\\bin\\gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

Once this is done, navigate to the debugger within the side panel of VSCode and notice the drop-down at the top now contains this name, allowing you to launch your application and attach a debugger within VSCode. Select your launch task and click the play button to run it.


Revision #13
Created Fri, Jun 5, 2020 3:57 PM by Shaun Reed
Updated Mon, Jun 29, 2020 4:05 AM by Shaun Reed