VS Code C++ Setup: IntelliSense, Build Tasks, and Debugging

VS Code C++ Setup: IntelliSense, Build Tasks, and Debugging

이 글의 핵심

Set up VS Code for C++: fix red squiggles with compilerPath, automate builds with tasks.json, and debug with launch.json (GDB/LLDB).

[C++ Hands-On Guide #3] VS Code C++ Configuration

After reading: You can configure IntelliSense, build tasks, and debugging in VS Code for day-to-day C++ work.

“The terminal builds, but the editor is full of red squiggles”

The C++ extension needs to know which compiler you use and where system headers live. If compilerPath is wrong, <iostream> / <vector> may show errors even when g++ succeeds.

IntelliSense = completion, signature help, and error squiggles while typing.

Scenario quick hits

  1. New teammate, new PC: Commit .vscode/ with OS-specific c_cpp_properties.json variants or document compilerPath per OS.
  2. CMake project, IntelliSense broken: Generate compile_commands.json (CMAKE_EXPORT_COMPILE_COMMANDS) or configure CMake Tools.
  3. WSL builds: Open the folder in WSL (Remote - WSL); point compilerPath to /usr/bin/g++.
  4. preLaunchTask not found: tasks.json label must exactly match launch.json preLaunchTask.
  5. macOS LLDB errors: Use MIMode: lldb; install Xcode CLI tools.

Prerequisites: A working compiler (#2) and optionally CMake (#4).

flowchart TB
    subgraph editor["VS Code"]
        A[Edit sources]
        B[IntelliSense]
    end
    subgraph config[".vscode/"]
        C[c_cpp_properties.json]
        D[tasks.json]
        E[launch.json]
    end
    subgraph run["Build & run"]
        F[g++/clang++]
        G[Binary]
        H[GDB/LLDB]
    end
    C --> B
    D --> F
    F --> G
    E --> H
    H --> G

Table of contents

  1. Install VS Code
  2. IntelliSense (c_cpp_properties.json)
  3. Build tasks (tasks.json)
  4. Debugging (launch.json)
  5. Common issues
  6. Worked example
  7. Productivity
  8. Production patterns
  9. Checklist

1. Install VS Code

Download from code.visualstudio.com. Enable Add to PATH so code . works from a terminal.

Install the C/C++ extension (Microsoft). Optional: CMake Tools, C/C++ Extension Pack.


2. IntelliSense — c_cpp_properties.json

Create .vscode/c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": ["${workspaceFolder}/**"],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

Windows (MinGW) example:

"compilerPath": "C:/msys64/mingw64/bin/g++.exe",
"intelliSenseMode": "windows-gcc-x64"

macOS (Clang):

"compilerPath": "/usr/bin/clang++",
"intelliSenseMode": "macos-clang-x64"

Run which g++ / where g++ and paste the exact path into compilerPath.


3. Build tasks — tasks.json

Default build for the current file:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "C++ Build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-std=c++17",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": { "kind": "build", "isDefault": true }
        }
    ]
}

Ctrl+Shift+B (mac: Cmd+Shift+B) runs the default build task.

For multiple files, list them explicitly or switch to CMake.


4. Debugging — launch.json

Linux example (GDB):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "cwd": "${fileDirname}",
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "preLaunchTask": "C++ Build"
        }
    ]
}

macOS: "MIMode": "lldb" (often omit miDebuggerPath).
Windows MinGW: point miDebuggerPath to gdb.exe.

F5 starts debugging; ensure preLaunchTask matches tasks.json label.


5. Common issues

SymptomFix
Red squiggles on standard headersFix compilerPath + intelliSenseMode
Header not foundAdd paths to includePath
preLaunchTask not foundMatch strings exactly
C++20 features flaggedSet cppStandard to c++20 and add -std=c++20 to tasks
undefined reference in tasks buildAdd all .cpp files or use CMake
Variables “optimized out”Build with -O0 -g for debugging

Reset IntelliSense: Command Palette → C++: Reset IntelliSense Database.


6. Worked example (multi-file)

main.cpp, utils.h, utils.cpp — use a second task:

{
    "label": "C++ Build Multi-File",
    "type": "shell",
    "command": "g++",
    "args": ["-g", "-std=c++17", "main.cpp", "utils.cpp", "-o", "myapp"],
    "options": { "cwd": "${workspaceFolder}" },
    "group": "build"
}

Point launch.json program to ${workspaceFolder}/myapp and preLaunchTask to that label.

CMake: Install CMake Tools → Configure → Build (F7) → debug with the generated targets.


7. Productivity

  • Ctrl+Shift+B — build
  • F5 — debug
  • Ctrl+P — quick open
  • Ctrl+Shift+P — command palette

8. Production patterns

  • Commit .vscode/ for shared team settings (exclude only personal noise if needed).
  • Align tasks.json commands with CI (g++ ... lines) to avoid “works locally, fails in CI”.
  • For large projects, prefer compile_commands.json from CMake instead of hand-maintained includePath.

9. Checklist

  • C/C++ extension installed
  • compilerPath matches which g++ / clang++
  • cppStandard matches project
  • Build task works (Ctrl+Shift+B)
  • Debug works (F5) with matching preLaunchTask

See also

  • Environment setup (01)
  • CMake intro (04)

Keywords

VS Code C++, c_cpp_properties.json, tasks.json, launch.json, GDB, LLDB, IntelliSense, C++ debugging.


FAQ

VS Code vs Visual Studio? VS Code is lightweight and cross-platform; Visual Studio is a full Windows IDE with deep MSVC integration.

Next: CMake intro (#4)

Previous: Compiler comparison