Programming - Visual Studio - Configure Visual Studio

Within .vscode,

Add some more compiler arguments (also called flags) to tasks.json.

tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "C/C++: g++ build active file",
      "command": "/usr/bin/g++",
      "args": [
        "-pthread",
        "-g",
        //"${file}",
        "${workspaceFolder}/*.cpp",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}",
        "-Wall",
        "-Wextra",
        "-Wconversion",
        "-Wsign-conversion",
        "-Wshadow",
        "-Wpedantic",
        "-std=c++23"
      ],
      "options": {
        "cwd": "${workspaceFolder}"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

NOTE:

  • The arguments -Wall, -Wextra, -Wconversion, -Wsign-conversion, and -Wshadow will instruct the compiler to warn us about many different types of common mistakes.
    • VS Code will then display these warnings right in the editor.
    • This is an incredibly useful feature, which will automatically detect and help prevent potential bugs in our code.
    • Never develop any C or C++ program without turning on all of these warning flags!
  • In addition, -Wpedantic and -std=c23 will instruct GCC to comply with the ISO C23 standard, the most recent standard, which will ensure that our program is maximally portable and can be compiled on other standards-complying compilers without too much hassle.
  • On Windows only, we should add the flag -D__USE_MINGW_ANSI_STDIO=1, which will ensure that printing to the terminal follows the C standard.

launch.json
{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++ - Build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: g++ build active file",
      "miDebuggerPath": "/usr/bin/gdb"
    }
  ]
}

NOTE: The string ${workspaceFolder} will automatically be replaced with the current workspace folder, so this will configure debugging sessions so that your program opens files from that folder.