AnuragSatish
AnuragSatish

Reputation: 11

unable to run c++17 program in vscode

I've enabled all c++ extensions and settings in vscode to run c++17 code, whenever I click run(ctrl+alt+n), getting errors. Can someone please help if I had missed anything

#include <array>
#include <iostream>
#include <string_view>
#include <tuple>
#include <type_traits>
 
namespace a::b::c
{
    inline constexpr std::string_view str{ "hello" };
}
 
template <class... T>
std::tuple<std::size_t, std::common_type_t<T...>> sum(T... args)
{
    return { sizeof...(T), (args + ...) };
}
 
int main()
{
    auto [iNumbers, iSum]{ sum(1, 2, 3) };
    std::cout << a::b::c::str << ' ' << iNumbers << ' ' << iSum << '\n';
 
    std::array arr{ 1, 2, 3 };
 
    std::cout << std::size(arr) << '\n';
 
    return 0;
}

Error:

    ~/cpp_pract $ cd "/home/anurag.satish/cpp_pract/" && g++ test_compiler.cpp -o test_compiler && "/home/anurag.satish/cpp_pract/"test_compiler
test_compiler.cpp:9:27: error: ‘string_view’ in namespace ‘std’ does not name a type
     inline constexpr std::string_view str{ "hello" };
                           ^~~~~~~~~~~
test_compiler.cpp: In function ‘std::tuple<long unsigned int, typename std::common_type<_Tp>::type> sum(T ...)’:

Vscode settings:

tasks.json:

{
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "C/C++: g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-g",
            "${file}",
            "-std=c++17",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "/usr/bin"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

}

launch.json:

{
// 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": "g++ - Build and debug active file",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [],
        "stopAtEntry": true,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "C/C++: g++ build active file",
        "miDebuggerPath": "/usr/bin/gdb"
    }
]

}

c_cpp_properties.json:

{
"configurations": [
    {
        "name": "Linux",
        "includePath": [
            "${workspaceFolder}/**"
        ],
        "defines": [],
        "cStandard": "c11",
        "cppStandard": "c++17",
        "compilerPath": "/usr/bin/gcc",
        "compilerArgs": [
            "-std=c++17"
        ],
        "intelliSenseMode": "clang-x64"
    }
],
"version": 4

Upvotes: 0

Views: 2183

Answers (1)

AnuragSatish
AnuragSatish

Reputation: 11

Go to Settings > User Settings In here, search for Run Code Configuration:

Under this menu, find: "code-runner.executorMap"

Edit this Setting by adding it to User Setting as below for C++17 support:

"code-runner.executorMap":{
    "cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},

Upvotes: 1

Related Questions