Alex.Kh
Alex.Kh

Reputation: 612

Configurate tasks.json file in VS Code to compile C++ with specific version of C++ on Mac

I am trying to compile some C++ code using VS Code and have some trouble with using different C++ version. I would like to compile it with an option -std=c++17 as some things I need to test out work in C++17 only (By default, Clang uses C++14). So, I tried editing my tasks.json file to manually add an option to use C++17. However, even after doing that nothing seems to work.

Initially, I only edited the options part for g++ build active file but , as it didn't seem to work, I added that option to all tasks. Unfortunately, this didn't help either. Can you tell me where exactly I made a mistake there? You can find tasks part of tasks,json file below.

"tasks": [
        {
            "type": "shell",
            "label": "clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++17"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "type": "shell",
            "label": "clang build active file",
            "command": "/usr/bin/clang",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++17"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        },
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-std=c++17"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]

Upvotes: 4

Views: 7133

Answers (1)

Alex.Kh
Alex.Kh

Reputation: 612

Okay, so the answer was simple , and I was actually doing the right things but in the wrong place.

First of all, it is possible just to add -std=c++17 to any of your tasks manually or just make a separate task with a specific name. As an example(taken from a VS Code website),

        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}",
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }

This is a task that has a name clang++ build active file and uses std=c++17 as a C++ version. Then, instead of pressing the Run code button you would have to use Terminal->Run build task option.

I initially though there would be a way to override the Run code button behaviour to use a different C++ version, but I guess the only way to do it is through adding a new/editing an old task.

Upvotes: 3

Related Questions