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
        }
    }
]
}
Aaron Liang
  • 51
  • 1
  • 3
  • 1
    Please do not post images of code or error messages. See [this Meta post](https://meta.stackoverflow.com/a/285557/62576) for a list of the many reasons why. Code and error messages are both textual information and should be copied/pasted directly into your question in that form. – Ken White Apr 02 '20 at 01:09
  • 1
    you need to add the c++17 flag to the intellisense compiler (although i couldn't say how to do that in VScode) – M.M Apr 02 '20 at 02:42
  • 1
    I am not sure what you mean. Can you tell more detail or specifically. – Aaron Liang Apr 02 '20 at 02:53
  • solution for this problem is explained here https://stackoverflow.com/questions/55116344/how-to-setup-vs-code-for-c-14-c-17 – Ravi Sep 27 '21 at 14:25

3 Answers3

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.
Shuwn
  • 61
  • 1
  • 4
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

Bill
  • 512
  • 4
  • 14
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.

Avi
  • 11
  • 2
  • 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? – MbBrainz Sep 08 '21 at 08:35