1

I'm trying to configure VS Code on a Mac (Intel) to develop with C++. I'm following the setup on the VS Code web site. Following all of the steps when I get to the Terminal - Run Build Task the build fails and indicates the it expected a ";" after 'msg'. I can run the same file in XCode with no issues, but VS Code fails. Here's the full code from the VS Code Setup site.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}
chasahodge
  • 373
  • 1
  • 4
  • 11

1 Answers1

2

I, too, had this same problem/error message with the same source code using my Mac Studio with M1 based on Apple arm64 silicon.

First, the source code comes from a tutorial: Using Clang in Visual Studio Code.

When I run the code, I get this error for vector<string> msg:

expected ';' at end of declaration 
range-based for loop is a C++11 extension [-Wc++11-extensions]

Second, as @Harry mentioned in a comment for this question:

initializing std::vector with initializer list is supported from c++11 on-wards.

Solution: add a compiler version to tasks.json that supports vector<string> msg. For me, I used -std=c++17 to the "args" list to solve the problem. As an FYI, I verified that -std=c++11 also solves the problem. Even -std=c++2b for the 2023 standard works with the Xcode version 14.2 command line extensions.

So, the default tasks.json file created for me in the tutorial from code.visualstudio.com was missing any reference to a compiler version directive.

Here is my updated tasks.json file with the one new element added to the args list:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-fcolor-diagnostics",
                "-fansi-escape-codes",
                "-std=c++17",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770