如何在 VSCode C++ 扩展中启用 C++17 标准

问题描述 投票:0回答:3

我想在 VS Code 中将 c++11 切换为 c++17。我尝试按照说明进行操作,但没有成功。

任务.json

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

c_cpp_properties.json

"configurations": [
    {
        "name": "Mac",
        "includePath": [
            "${workspaceFolder}/**"
        ],
        "defines": [],
        "macFrameworkPath": [
            
      "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
        ],
        "compilerPath": "/usr/bin/g++",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "intelliSenseMode": "${default}"
    }
],
"version": 4

启动.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": "lldb",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": [
            
        ],
        "cwd": "${fileDirname}",
        "preLaunchTask": "C/C++: g++ build active file"
    }
]

设置.json

"C_Cpp.errorSquiggles": "Enabled",
"C_Cpp.default.cppStandard": "c++17",
"files.associations": {
    "*html": "html",
    "iostream": "cpp",
    "iosfwd": "cpp",
    "ostream": "cpp",
    "istream": "cpp",
    "fstream": "cpp",
    "new": "cpp",
    "__bit_reference": "cpp",
    "__config": "cpp",
    "__debug": "cpp",
    "__errc": "cpp",
    "__functional_base": "cpp",
    "__hash_table": "cpp",
    "__locale": "cpp",
    "__mutex_base": "cpp",
    "__node_handle": "cpp",
    "__nullptr": "cpp",
    "__split_buffer": "cpp",
    "__string": "cpp",
    "__threading_support": "cpp",
    "__tree": "cpp",
    "__tuple": "cpp",
    "algorithm": "cpp",
    "array": "cpp",
    "atomic": "cpp",
    "bit": "cpp",
    "bitset": "cpp",
    "cctype": "cpp",
    "chrono": "cpp",
    "cmath": "cpp",
    "complex": "cpp",
    "cstdarg": "cpp",
    "cstddef": "cpp",
    "cstdint": "cpp",
    "cstdio": "cpp",
    "cstdlib": "cpp",
    "cstring": "cpp",
    "ctime": "cpp",
    "cwchar": "cpp",
    "cwctype": "cpp",
    "deque": "cpp",
    "exception": "cpp",
    "functional": "cpp",
    "initializer_list": "cpp",
    "iomanip": "cpp",
    "ios": "cpp",
    "iterator": "cpp",
    "limits": "cpp",
    "list": "cpp",
    "locale": "cpp",
    "map": "cpp",
    "memory": "cpp",
    "mutex": "cpp",
    "numeric": "cpp",
    "optional": "cpp",
    "queue": "cpp",
    "ratio": "cpp",
    "set": "cpp",
    "sstream": "cpp",
    "stack": "cpp",
    "stdexcept": "cpp",
    "streambuf": "cpp",
    "string": "cpp",
    "string_view": "cpp",
    "system_error": "cpp",
    "tuple": "cpp",
    "type_traits": "cpp",
    "typeinfo": "cpp",
    "unordered_map": "cpp",
    "utility": "cpp",
    "vector": "cpp",
    "thread": "cpp"
}

C/C++ 配置 (UI) 中也更改为 c++17。

由于这个问题,我在编译我的作品时遇到了困难。 生成此警告:

执行任务:C/C++:g++构建活动文件< Starting build... /usr/bin/g++ -g -Wall -std=c++17 /Users/aziznosirov/Documents/cpp_udemy/btp305_w2_p1/*.cpp -o /Users/aziznosirov/Documents/cpp_udemy/btp305_w2_p1/w2_p1 /Users/aziznosirov/Documents/cpp_udemy/btp305_w2_p1/w2_p1.cpp:43:14: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings] t.addEvent(" 0-arg Constructor");

您可以看到,虽然编译器在构建时显示为 c++17,但它仍然会生成提及 c++11 的警告。

如有任何帮助,我们将不胜感激。谢谢。

c++ c++11 visual-studio-code c++17
3个回答
0
投票

查看

Build Task
我们可以看到代码实际上是使用
-std=c++17
标志显式编译的。

我怀疑正在使用的编译器不是最新的,

GCC -7
对 C++ 17 功能有实验性支持,可以通过使用
-std=c++1z
旗帜。

从 GCC 8 开始,编译器具有完整的 C++17 支持。

我建议更新 GCC 包。


0
投票

我解决了这个问题。我只需要添加一个常量。我的 c++17 确实可以工作。


-1
投票

我也有类似的问题。 VScode Intellisense 似乎可以针对某些 C++ 标准进行配置,但不能明确针对 gnu++1z 或 c++1z,而只能针对 gnu++17 或 c++17。 它编译正确,因为我的编译器参数是 -std=c++1z。 尽管如此,智能感知还是给了我这个警告: “分解声明仅适用于 -std=c++1z 或 -std=gnu++1z”。 我怎样才能避免或抑制这个警告? “我只需要添加一个常量”是什么意思? 谢谢哈拉尔德

© www.soinside.com 2019 - 2024. All rights reserved.