【问题标题】:c++20 and c++11 standards both specified in tasks.json, still won't work?在 tasks.json 中指定的 c++20 和 c++11 标准仍然不起作用?
【发布时间】:2022-09-23 03:44:52
【问题描述】:

我正在使用 Visual Studio Code 学习 C++。我开始绞尽脑汁想弄清楚为什么编译器不能识别 C++11 标准。代码+.json文件+错误如下:

#include <iostream>
#include <vector>
using namespace std;

int main() {

    vector <int> vector_numbers;

    for (int i : vector_numbers) {
        vector_numbers[1] = i + 1;
    }

    for (int i : vector_numbers) {
        cout << \"The vector element at index [\" << i << \"] is \" << vector_numbers.at(i) << endl;
    }

    return 0;
}

tasks.json 中 \"args\" 参数的内容是

            \"args\": [
                \"-std=c++11\",
                \"-std=c++17\",
                \"-std=c++20\",
                \"-stdlib=libc++\",
                \"-fcolor-diagnostics\",
                \"-fansi-escape-codes\",
                \"-g\",
                \"${file}\",
                \"-o\",
                \"${fileDirname}/${fileBasenameNoExtension}\"
            ]

错误是:

<REDACTED> % cd \"<REDACTED PATH>\" && g++ Vectors.cpp -o Vectors && \"<REDACTED PATH>\"Vectors
Vectors.cpp:9:16: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
    for (int i : vector_numbers) {
               ^
Vectors.cpp:13:16: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
    for (int i : vector_numbers) {
               ^
2 warnings generated.
  • 从控制台输出来看,tasks.json 中的 args 实际上都没有进入编译器调用。仔细检查您实际上是否在使用该 json 文件?
  • 此外,gcc 自 6.1 版起默认启用 C++14,因此您的编译器必须是旧的。你可以考虑升级一下。
  • @Yksisarvinen CLI 命令g++ -v 返回:\'Apple clang 版本 13.1.6 (clang-1316.0.21.2.5) 目标:x86_64-apple-darwin21.6.0 线程模型:posix InstalledDir: <REDACTED>\'
  • 后面的标志会覆盖前面的标志,但是当您使用-std=c++20 时,没有理由使用-std=c++11。只有后者就足够了。但是如上所述,显示的编译实际上没有使用任何标志,因此您必须将它们放在错误的位置(或者没有保存文件或类似的东西)。另外,您使用的是 Clang,而不是 GCC。您可能在 Apple 设备上,其中g++ 的别名为clang++
  • clang 13 应该吃掉那个代码并微笑,所以一定有一个-std=c++98 潜伏在镜头外的某个地方。寻找它。

标签: c++ json c++11 visual-studio-code c++20


【解决方案1】:

好的,问题已解决并为后代发布解决方案。

问题在于 coderunner 扩展未在 CLI 参数中使用 -std=c++20。对于 coderunner 的 settings.json 文件应该是:

"code-runner.executorMap": {

        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ **-std=c++20** $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "python -u",
        "perl": "perl",
        "perl6": "perl6",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "scheme": "csi -script",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runhaskell",
        "nim": "nim compile --verbosity:0 --hints:off --run",
        "lisp": "sbcl --script",
        "kit": "kitc --run",
        "v": "v run",
        "sass": "sass --style expanded",
        "scss": "scss --style expanded",
        "less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
        "FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "sml": "cd $dir && sml $fileName"
    }

关键配置参数是 -std=c++20 用于“cpp”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-17
    • 2015-12-09
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 2021-08-28
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多