5

I am trying to set up vscode environment on my Mac. I follow the procedure on the website https://code.visualstudio.com/docs/cpp/config-clang-mac But When I try to debug my program, it show error and can not step over. I am not sure it is because of version or something.

#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 << " ";
}

vector<string> aaa {"H", "E", "L", "L", "O"};
for (const string& word : aaa)
{
    cout << word << " ";
}


cout << endl;
}

And the error message shows

expect ; at the end of declaration [9, 23] expect ; at the end of declaration [16, 23] range-based for loop is a c++11 extension [11, 29] range-based for loop is a c++11 extension [17, 29]

My launch.json file is

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
    // "name": "(lldb) Launch",
        "name": "clang++ - Build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": true,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "lldb",
        "preLaunchTask": "clang++ build active file"
    }
]
}

My task.json file is

{
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "clang++ build active file",
        "command": "/usr/bin/clang++",
        "args": [
            "-std=c++17",
            "-stdlib=libc++",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]
}
2
  • 2
    you need to add the c++17 flag to the intellisense compiler (although i couldn't say how to do that in VScode) Commented Apr 2, 2020 at 2:42
  • 1
    I am not sure what you mean. Can you tell more detail or specifically. Commented Apr 2, 2020 at 2:53

3 Answers 3

6

This error is caused by problemMatcher judgment.

Because it doesn't judge with the correct C++ version.

The solution is as follows.

  • Settings will open up
  • Find code-runner: Executor Map and click on the edit in settings.json
  • Find the cpp and add -std=c++17 after cd $dir && g++ Like this "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
  • Save it and you are all done! You can try again.
Sign up to request clarification or add additional context in comments.

Comments

1

did you get this to work? I added a c_cpp_properties.json in .vscode directory with the following:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
            ],
            "macFrameworkPath": [
                "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

and I able to debug the same code example

Comments

1

I was also facing the same problem. But the problem was that the C/C++ configuration by default was not what was shown in the tutorial. So if you follow the tutorial step-by-step, you migh end up not configuring the c/c++ compiler to use. What I did was to skip the debugging step and directly go to the C/C++ configuration step and then added .vscode/c_cpp_properties.json as shown in the tutorial (also by @Bill in the post above). Then if you try to debug, it won't show that error because now you have configured the compiler as c++17.

1 Comment

I tried what you suggested and if i build the file it doesn't show errors, however if i debug it using the debug button of VSCode it does. My tasks.json is exactly the same as the file from the instructions of VSCode. I also went through the configurations step as you mentioned. Any further suggestions?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.