vscode中code runner使用c++17标准

按照官网文档troubleshooting将tasks.json修改如下,使用vscode自带的run c/c++ file会使用c++17。但是code runner是走Run Code的,还是无效

Configure VS Code for Clang/LLVM on macOS

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "type": "shell",
        "label": "C/C++: clang++ build active file",
        "command": "/usr/bin/clang++",
        "args": [
          "-std=c++17",
          "-stdlib=libc++",
          "-g",
          "${file}",
          "-o",
          "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
          "cwd": "${workspaceFolder}"
        },
        "problemMatcher": ["$gcc"],
        "group": {
          "kind": "build",
          "isDefault": true
        },
        "detail": "Task generated by Debugger."
      }
    ]
}
// {
//     "tasks": [
//         {
//             "type": "cppbuild",
//             "label": "C/C++: clang++ build active file",
//             "command": "/usr/bin/clang++",
//             "args": [
//              //   "-std=c++17",
//                 "-fcolor-diagnostics",
//                 "-fansi-escape-codes",
//                 "-g",
//                 "${file}",
//                 "-o",
//                 "${fileDirname}/${fileBasenameNoExtension}"
//             ],
//             "options": {
//                 "cwd": "${fileDirname}"
//             },
//             "problemMatcher": [
//                 "$gcc"
//             ],
//             "group": {
//                 "kind": "build",
//                 "isDefault": true
//             },
//             "detail": "Task generated by Debugger."
//         }
//     ],
//     "version": "2.0.0"
// }

成功方法: 

  • 打开 文件 -> 首选项 -> 设置 -> 输入 setting.json -> 打开 "在 setting.json 中编辑"
  • 输入 "code-runner.executorMap" -> 点击回车 -> 自动补全
  • 找到 "cpp" 选项 -> 将 " : " 之后的值改为 "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
  • 保存退出 -> 即可编译 c++17 标准的 cpp 文件

{
    "code-runner.runInTerminal": true,
    "terminal.integrated.enableMultiLinePasteWarning": false,
    "editor.fontSize": 13,
    "files.autoGuessEncoding": true,
    "editor.inlineSuggest.enabled": true,
    "files.autoSave": "onFocusChange",
    "github.copilot.enable": {
        "*": false,
        "yaml": false,
        "plaintext": true,
        "markdown": true
    },
    "workbench.editor.enablePreview": false,
    "editor.unicodeHighlight.invisibleCharacters": false,
    "editor.unicodeHighlight.ambiguousCharacters": false,
    "explorer.confirmDelete": false,
    "terminal.integrated.inheritEnv": false,
    "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++17 $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"
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 VSCodeCode Runner 插件配置 C++ 环境,需要完成以下步骤: 1. 安装 Code Runner 插件 在 VSCode 搜索并安装 Code Runner 插件,安装完成后重新启动 VSCode。 2. 安装 C++ 编译器 在 Windows ,可以安装 MinGW-w64 或者 Visual Studio 等编译器。在 Mac 上,可以安装 Xcode。在 Linux 上,可以安装 GCC。 3. 配置 Code RunnerVSCode 打开 Code Runner 插件的设置,可以通过 "File" -> "Preferences" -> "Settings" 或者快捷键 "Ctrl + ," 打开设置页面。在搜索框输入 "code-runner.executorMap",找到 "Executor Map" 选项,并点击 "Edit in settings.json" 按钮。 在 settings.json 文件,添加以下代码: ``` "code-runner.executorMap": { "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" } ``` 这里我们以 g++ 编译器为例,如果你使用的是其他编译器,可以将上面代码的 "g++" 替换为相应的编译器名字。 4. 运行代码 在 VSCode 打开一个 C++ 代码文件,按下 "Ctrl + Alt + N" 快捷键,或者点击菜单栏的 "Run Code" 按钮,即可运行代码。 注意:如果你在 Code Runner 插件的配置修改了默认的编译器,记得在代码文件第一行添加相应的编译器指令,例如: ``` // 如果你使用的是 g++ 编译器 // #include <bits/stdc++.h> // using namespace std; // #define compiler "g++ -std=c++11" ``` 这样 Code Runner 插件就可以自动识别你使用的编译器了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值