VSCode C++ configuration

To set up Visual Studio Code (VSCode) to use C++17 by default when compiling your C++ programs, you can configure your build tasks and settings. Here's how to do it:

Step 1: Install the Necessary Extensions

First, ensure you have the C++ extension for VSCode installed:

  1. Open VSCode.

  2. Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or pressing Ctrl+Shift+X.

  3. Search for "C++" and install the "C/C++" extension provided by Microsoft.

Step 2: Set Up the tasks.json File

Next, you'll set up a task in VSCode to compile your C++ code using C++17.

  1. Open your project folder in VSCode.

  2. Go to the Command Palette (Ctrl+Shift+P) and type "Tasks: Configure Default Build Task". Select it.

  3. Choose "Create tasks.json file from template" and select "Others".

  4. This will create a tasks.json file in a .vscode folder in your project.

  5. Modify the tasks.json file to look something like this:

    jsonCopy code{
        "version": "2.0.0",
        "tasks": [
            {
                "label": "build",
                "type": "shell",
                "command": "/usr/bin/g++",
                "args": [
                    "-std=c++17",
                    "-o",
                    "${fileDirname}/${fileBasenameNoExtension}",
                    "${file}"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "problemMatcher": ["$gcc"],
                "detail": "Generated task by VS Code."
            }
        ]
    }
    • /usr/bin/g++: This is the path to the g++ compiler. If it's installed elsewhere, update this path accordingly.

    • -std=c++17: This flag tells the compiler to use C++17.

    • ${file}: This refers to the currently open file in the editor.

    • ${fileDirname} and ${fileBasenameNoExtension}: These refer to the output file path and name.

Step 3: Configure the c_cpp_properties.json File

To ensure IntelliSense and other VSCode features use the correct C++ standard, you should also configure the c_cpp_properties.json file.

  1. Go to the Command Palette (Ctrl+Shift+P) and type "C/C++: Edit Configurations (UI)".

  2. Set the C++ Standard to c++17 in the UI, or directly edit the c_cpp_properties.json file:

Step 4: Build and Run Your Code

Now, whenever you want to build your code:

  1. Open the file you want to compile.

  2. Press Ctrl+Shift+B to run the build task. This will compile your code using the C++17 standard.

  3. If your code compiles successfully, you can run the output binary from the terminal.

By setting up these configurations, VSCode will now use C++17 for compiling and IntelliSense.

To modify your existing tasks.json configuration to use the C++17 standard, you need to add the -std=c++17 flag to the args array. Here's how you can update your tasks.json file:

Explanation:

  • -std=c++17: This flag tells the compiler to use the C++17 standard.

  • The rest of your configuration remains the same, ensuring that the active file is compiled with the correct standard.

After making this change, your tasks in VSCode will compile C++ files using the C++17 standard by default when you run the build task (Ctrl+Shift+B).

Last updated