【问题标题】:unable to run c++17 program in vscode无法在vscode中运行c++17程序
【发布时间】:2020-12-12 23:47:03
【问题描述】:

我已经启用了 vscode 中的所有 c++ 扩展和设置来运行 c++17 代码,每当我单击运行 (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 设置:

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

【问题讨论】:

  • 您使用的是哪个版本的g++
  • "g++ test_compiler.cpp -o test_compiler" 没有在您的配置中提及 -std=c++17...
  • 您的设置似乎对命令行没有影响。你忘记保存了吗?
  • 我确定已经保存了设置。 GCC版本:gcc 7.5.0版(Ubuntu 7.5.0-3ubuntu1~18.04)
  • @Jarod42,是的,虽然我在 tasks.json 中指定使用 -std=c++17 运行程序,但是当我按下 ctrl+alt+n 时,vscode 没有运行它。但是,当我按 ctrl+shift+B 时,程序正在成功编译。不知道为什么正常的 compile+run cmd(ctrl+alt+n) 不起作用

标签: c++ visual-studio-code vscode-settings


【解决方案1】:

转到设置>用户设置在这里,搜索运行代码配置:

在这个菜单下,找到:“code-runner.executorMap”

通过将其添加到用户设置来编辑此设置,如下所示以支持 C++17:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 2021-04-07
    • 2013-03-06
    相关资源
    最近更新 更多