Search code examples
c++macosvisual-studio-codevscode-tasks

VS Code on macOS with cpptools 1.71.1+: When trying to compile code in C++, "-std=gnu++14" appears before all the flags. How to delete it?


I am a beginner, using VSCode on macOS. Trying to iterate using square brackets which is requires C++17:

for (auto [document_id, relevance] : documents) {
            cout << "{ document_id = "s << document_id << ", relevance = "s << relevance << " }"s
                 << endl;
        }

When trying to Terminal -> Run build task, -std=gnu++14 flag appears before all other flags, even if -std=gnu++17 specified in tasks.json

Here are my tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=gnu++17",
                "-stdlib=libc++",
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "/usr/bin/clang++"
        }
    ]
}

Here is the output:

 *  Executing task: C/C++: clang++ 

Starting build...
/usr/bin/clang++ -std=gnu++14 -std=gnu++17 -stdlib=libc++ -fcolor-diagnostics -fansi-escape-codes -g /Users/user/Documents/С++/000test/main.cpp -o /Users/user/Documents/С++/000test/main

Build finished successfully.
 *  Terminal will be reused by tasks, press any key to close it. 

What could be the problem?

I also tried to change the C++ version to 17 in the C/C++ extension, but to no avail.

When running with "-std=gnu++17", "-stdlib=libc++" in tasks json no warning appears, but does the second -std flag override the first one?

I also found this topic, but I dont even have CMake extension in vscode.


Solution

  • Looks like this is hard coded into the c++ extension:

    https://github.com/microsoft/vscode-cpptools/blob/dec50598bc4d169b38292d2f4ff26a216d3c6342/Extension/src/LanguageServer/cppBuildTaskProvider.ts#L370

    insertStd is set to true for clang on macos.

    I'd raise a bug with Microsoft but in the meantime just don't build directly with tasks, use cmake or another build system and you'll avoid this problem.

    This was added recently: https://github.com/microsoft/vscode-cpptools/pull/11300