How to Setup VS Code For C++ 14 /C ++17 [duplicate]
Asked Answered
K

5

13

I tried to run a .cpp file from workspace but giving me this error about not adding c++11/higher flags but I have added them in task.json

Error

[Running] cd "c:\Users\Nuhash\Desktop\test\" && g++ main.cpp -o main && "c:\Users\Nuhash\Desktop\test\"main
main.cpp:8:1: error: expected unqualified-id before 'using'
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
^
main.cpp:10:1: error: expected unqualified-id before 'using'
using ordered_set_rev = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
^
main.cpp:12:1: error: expected unqualified-id before 'using'
using dijkstra = priority_queue<T, vector<T>, greater<T>>;
^
main.cpp:62:31: warning: variadic templates only available with -std=c++11 or -std=gnu++11
template <typename T, typename... Args>
                               ^
main.cpp:63:52: warning: variadic templates only available with -std=c++11 or -std=gnu++11
void err(istream_iterator<string> it, T a, Args... args) {


Task.Json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "-o",
                "test",
                "-std=c++14",
                "main.cpp"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

Error message:

[Running] cd "c:\Users\Nuhash\Desktop\test\" && g++ main.cpp -o main && "c:\Users\Nuhash\Desktop\test\"main
main.cpp:8:1: error: expected unqualified-id before 'using'
 using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 ^
main.cpp:10:1: error: expected unqualified-id before 'using'
 using ordered_set_rev = tree<T, null_type, greater<T>, rb_tree_tag, tree_order_statistics_node_update>;
 ^
main.cpp:12:1: error: expected unqualified-id before 'using'
 using dijkstra = priority_queue<T, vector<T>, greater<T>>;
 ^
main.cpp:62:31: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 template <typename T, typename... Args>
                               ^
main.cpp:63:52: warning: variadic templates only available with -std=c++11 or -std=gnu++11
 void err(istream_iterator<string> it, T a, Args... args) {

c_cpp_properties:

    {

        "name": "Win32",
        "includePath": [
            "${workspaceFolder}"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "intelliSenseMode": "clang-x64",
        "browse": {
            "path": [
                "${workspaceFolder}"
            ],
            "limitSymbolsToIncludedHeaders": true,
            "databaseFilename": ""
        },
        "compilerPath": "F:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\gcc.exe",
        "cStandard": "c11",
        "cppStandard": "c++17"
    }
Kazan answered 12/3, 2019 at 7:41 Comment(4)
are you sure that is the correct tasks.json? it doesn't match the error messageDendrology
Yes It was working fine . Then I re installed windows and vs code but then it's not working for some reason. Task. json was in the folder from the beginning...Kazan
I second Alan's comment. The build output [Running] cd "c:\Users\Nuhash\Desktop\test\" && g++ main.cpp -o main && "c:\Users\Nuhash\Desktop\test\"main does not match your tasks.json.Ute
Yes But Tasks.json is correct. Dont know why command isn't matching with the tasks.json i recreated it several times. Anyway i fixed it with changing code runner config in settings.jsonKazan
K
24

I added code runner and added this in settings.json Seems to work for me :D

"code-runner.executorMap": {
    "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},
Kazan answered 12/3, 2019 at 22:27 Comment(2)
Code runner is an extension with very limited functionality, not useful for even most basic C++ development. Don't use it.Spineless
@n.m. question was about running a single .cpp file. To run single file is most cases code runner is usefulKazan
A
5

Add "-std=c++11", to the "args" value in your task.json file. That should resolve your c++11 issue. So finally your task.json will look like

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build hello world",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",                
                "-std=c++11",
                "-std=c++14",
                "main.cpp",
                "-o",
                "test"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}
Actuality answered 21/8, 2019 at 23:49 Comment(1)
@uma_8331 You can press Ctrl+Shift+P and choose "Tasks: Configure Task" and VSCode will create one for youBanka
F
4
  • Open VS Code
  • Go to the extensions (Ctrl +Shift + X)
  • Then Simply click on the code runner
  • Then click on the settings icon there
  • Then choose Extension Settings
  • Photo
  • Settings will open up,

  • Click on the edit in settings.json under Code-Runner -Executer Map

  • Photo

  • Find the "cpp" in the json file and add -std=c++17 after && g++
  • Finally it will look like this "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
  • Save it and you are all done]
  • Photo
  • Try this code out if this code runs without any error you have enabled C++17 Link
  • Make sure that when you run the code terminal look like this
  • Photo
  • Note: If you don't use code Runner I will advise you to do so
  • If you are using windows and your Compiler don't support c++17
  • You can install the MinGw latest compiler(9.2.0) from the below link
  • Link
Flaccid answered 3/3, 2021 at 6:9 Comment(0)
N
1

Set cppStandard to c++17 or c++14 respectively.

You need the C++ extension for that https://github.com/Microsoft/vscode-cpptools

Nutritionist answered 12/3, 2019 at 8:6 Comment(1)
I already have that extension and cppStandard is set to c++17Kazan
K
0

I didn't added MinGW header files in c_cpp_properties.json . After It worked fine c_cpp_properties.json lookes like this ...

{
"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**",
            "C:\\Program Files (x86)\\CodeBlocks\\MinGW\\include"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "compilerPath": "\"C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin\\g++.exe\"",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "gcc-x86"
    }
],
"version": 4
}

and change my tasks.json to this ...

{
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "g++.exe build active file",
        "command": "g++.exe -std=c++14 -g ${file} -o ${fileDirname}\\${fileBasenameNoExtension}.exe && ${fileDirname}\\${fileBasenameNoExtension}.exe",
        "options": {
            "cwd": "C:\\Program Files (x86)\\CodeBlocks\\MinGW\\bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build"
    }
]
}

now it builds and runs the compiled file

Kazan answered 8/11, 2019 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.