VSCode c++ task.json include path and libraries

1. Am I setting up VSCode correctly?

Mostly. The fact that you have to specify the include paths twice (once in c_cpp_properties.json and again in a file that describes your build) is unavoidable. In VSCode, the build system and the editor do not understand each other, and both need this information. In contrast, with Visual Studio (no "Code"), it would only be necessary to specify the paths once; that is one of the benefits of using a "true" Integrated Development Environment. (But there are drawbacks too; I'm not trying to discourage you from using VSCode.)

However, I do not recommend putting the include paths into tasks.json directly. Rather, one typically has a separate build system that can be invoked from the command line, and then tasks.json invokes that command too.

As a very common example, you could use GNU Make and replace your current tasks.json with this (untested!) Makefile:

test-sdl: main2.cpp
    g++ -g main2.cpp -ID:\\github\\dependencies\\SDL2-2.0.8\\include -LD:\\github\\dependencies\\SDL2-2.0.8\\lib\\x64 -lSDL2main -lSDL2 -lopengl32 -o test-sdl

This tells make how to build test-sdl from main2.cpp, namely by running the g++ command shown. (I have deliberately kept this Makefile very simple since the question isn't about Makefiles; just be aware that a real Makefile would break things up for better organization, and the backslashes are likely to need adjustment.)

In any case, then your tasks.json simplifies to:

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

This is better because you don't have crucial build information locked away in a file that only VSCode understands.

2. Can someone explain ... includePath and browse?

VSCode has two different systems for understanding C++ code. There is the older "Tag Parser", which uses browse.path, and the newer "Intellisense", which uses includePath. At this point (2019-08-30, VSCode 1.37.1), my understanding is basically everyone should be using the newer Intellisense system, as it provides more accurate information and should be at least as mature. Consequently, you should be able to simply ignore browse.path.

To make sure you are using Intellisense rather than Tag Parser, go into File → Preferences → Settings → C/C++ → "C_Cpp: Intelli Sense Engine" and make sure that it is "Default" rather than "Tag Parser". Note that this setting is stored in settings.json rather than c_cpp_properties.json.


I too have attempted to use libraries, and at least for now this works (I'm on Windows BTW): In c_cpp_properties.json, I have a reference to the include directory:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:\\ProgrammingLibraries\\SDL2-2.0.10\\include\\SDL2",
                "${workspaceFolder}\\src\\include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\mingw-w64\\x86_64-8.1.0-win32-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

And in tasks.json, I have A Compilation and a Linker task, and a task that runs both:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compiler",
            "type": "shell",
            "command": "g++",
            "args": [
                "-c",
                "${workspaceFolder}\\src\\main.cpp",
                "-IC:\\ProgrammingLibraries\\SDL2-2.0.10\\include\\SDL2"
            ]
        },
        {
            "label": "Linker",
            "type": "shell",
            "command": "g++",
            "args": [
                "${workspaceFolder}\\main.o",
                "-o",
                "${workspaceFolder}\\bin\\HelloSDL.exe",
                "-LC:\\ProgrammingLibraries\\SDL2-2.0.10\\lib",
                "-lmingw32",
                "-lSDL2main",
                "-lSDL2"
            ]
        },
        {
            "label": "Build HelloSDL",
            "dependsOn": [
                "Compiler",
                "Linker"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}