无法在VSCode中在Mac上启用C++20,我更新了现有的tasks.json文件,使其作为标准运行C++20,但没有任何结果

6ioyuze2  于 8个月前  发布在  Vscode
关注(0)|答案(1)|浏览(173)

我试图在我的M1 MacBook Pro 13”上启用C20,但没有结果。我正在学习C的教程,老师告诉我如何在Mac上启用C++20(因为他没有Mac,所以在教程中没有实际操作)。所以我在VSCode网站上查找了如何在VSC中使用clang,并设置了VSC。

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: clang++ build active file",
      "command": "/usr/bin/clang++",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "-std=c++20",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": ["$gcc"],
      "group": "build",
      "detail": "compiler: /usr/bin/clang++"
    }
  ]
}

这是我的tasks.json文件
教师告诉运行以检查C++20是否为

#include <iostream>

int main(int argc, const char **argv)
{
    int result = (10 <=> 20) > 0;
    return 0;
}

这就是我得到的错误。

➜ clang++ main.cpp 
main.cpp:10:22: warning: '<=>' is a single token in C++20; add a space to avoid a change in behavior [-Wc++20-compat]
    int result = (10 <=> 20) > 0;
                     ^
                        
main.cpp:10:24: error: expected expression
    int result = (10 <=> 20) > 0;
                       ^
1 warning and 1 error generated.

请帮帮我

qvsjd97n

qvsjd97n1#

您遇到的编译器错误是由于使用了较旧的标准(可能是c17?)设置并尝试使用该语言的较新版本中引入的功能。
三路比较('宇宙飞船')运算符是在c
20中引入的(参见https://en.cppreference.com/w/cpp/language/operator_comparison),需要针对该标准。
此问题似乎源于c_cpp_properties.json配置不当。你可以在VSCode中通过在命令面板中输入'c++ edit configurations'来访问它。
在此之后,确保将"cppStandard"设置为"c++20"。您的json最终应该看起来像这样:

"configurations": [
    {
      "name": "Mac",
      "intelliSenseMode": "clang-x64",
      "includePath": ["${myDefaultIncludePath}", "/another/path"],
      "macFrameworkPath": ["/System/Library/Frameworks"],
      "defines": ["FOO", "BAR=100"],
      "forcedInclude": ["${workspaceFolder}/include/config.h"],
      "compilerPath": "/usr/bin/clang",
      "cStandard": "c11",
      "cppStandard": "c++20",
      "compileCommands": "/path/to/compile_commands.json",
      "browse": {
        "path": ["${workspaceFolder}"],
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
      }
    }
  ],
  "version": 4
}

(from https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference,已更新"cppStandard"

相关问题