4

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"
        }
    ]
2
  • What exactly does "nothing seems to work" and "didn't help either" mean? Did the compilation fail? What errors or log messages do you get in VS Code's console output? How are you running the task? (Tasks: Run Task > g++ build active file)? Have you tried running the g++ command outside of VS Code to confirm it can be compiled correctly on your machine? Commented Mar 29, 2020 at 7:24
  • Standard compilation in VS Code(-std=c++11 and earlier), as well as compilation in terminal work fine. I found a solution to my problem though, so I guess I will just post the answer myself Commented Mar 30, 2020 at 2:47

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.