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

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

我正在尝试在我的 M1 MacBook Pro 13" 上启用 C++20,但没有结果。我正在按照教程学习 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.

请帮帮我

c++ c++20
2个回答
0
投票

您遇到的编译器错误是由于设置了较旧的标准(可能是 c++17?)并尝试使用该语言的较新版本中引入的功能。

三向比较(“太空船”)运算符是在 c++20 中引入的(请参阅 https://en.cppreference.com/w/cpp/language/operator_comparison),并且需要针对该标准。

此问题似乎源于配置不当

c_cpp_properties.json
。您可以在 VSCode 中通过在命令面板中键入“c++ 编辑配置”来访问它。

到达那里后,确保将

"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
}

(来自 https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference,已更新

"cppStandard"


0
投票

您在 Mac 上成功运行 C++20 了吗?我在这个问题上挣扎了 2 天,如果您能帮助我,我将不胜感激。

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