如何在 VSCode C Extension 中启用 C 17 支持

新手上路,请多包涵

我不断在 std::string_view 上收到错误曲线,但我能够构建得很好。有没有办法告诉智能感知或 C++ linter 使用 C++17?

我得到的具体错误是:

 namespace "std" has no member "string_view"

原文由 ajoseps 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
2 个回答

现在这变得容易多了。在您的 vs 代码扩展设置中搜索 cppstandard 并从下拉列表中选择您希望扩展使用的 C++ 版本。

在此处输入图像描述

为了确保您的调试器使用相同的版本,请确保您的 tasks.json 有类似的内容,其中重要的行是 --std 和之后的行定义版本。

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

请注意,如果您要直接复制上面的 tasks.json ,您需要在工作区根目录中有一个名为 out 的文件夹。

原文由 giraffesyo 发布,翻译遵循 CC BY-SA 4.0 许可协议

Additional to set cppStandard to gnu++17 in c_cpp_properties.json mentioned in other posts, you need to change the __cplusplus -define to the corresponding value (例如 201703L

像那样:

 {
  "version": 4,
  "configurations": [
    {
      // ...
      "cStandard": "gnu17",
      "cppStandard": "gnu++17",
      "defines": [
        // ...
        "__cplusplus=201703L"
        // ...
      ]
    }
  ]
}

原文由 Zombie-3000 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏