按照官网文档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"
}
}