簡體   English   中英

無法在 vscode 中運行 c++17 程序

[英]unable to run c++17 program in vscode

我已經啟用了 vscode 中的所有 c++ 擴展和設置來運行 c++17 代碼,每當我單擊 run(ctrl+alt+n) 時,都會出現錯誤。 如果我錯過了什么,有人可以幫忙嗎

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

錯誤:

    ~/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 設置:

任務.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
        }
    }
]

}

啟動.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

轉到設置>用戶設置在這里,搜索運行代碼配置:

在這個菜單下,找到:“code-runner.executorMap”

通過將其添加到用戶設置來編輯此設置,如下所示以支持 C++17:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM