回答上篇文章。
参考教程并作了一定修改 https://blog.csdn.net/weixin_43269020/article/details/106041513
全局一键运行&调试C++, 就是不需要每次都建task.json和launch.json了,直接按快捷键即可。
全局一键运行是基于Code Runner, 在VSCode的扩展中心安装即可,运行的快捷键是ctrl+alt+N, 或者右击-Run Code. 为了能调试,还需要修改cpp对应的代码。安装Code Runner后打开settings.json, 找到cpp对应的项,修改为:
- ctrl+shift+P可以打开settings.json
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt -g && $dir$fileNameWithoutExt",
全局一键调试则需要修改VSCode的settings.json文件,添加如下内容即可:
- 调试之前必须先用Code Runner运行,生成.exe文件才可以调试F5
- 然后可以再任意位置新建一个.cpp文件,输入内容F5调试并运行。不需要配置任务、添加配置,也不需要在vscode中打开文件夹。
- 注意:F5之前在左侧的调试窗口选择你想使用的调试configuration的name
- 要使用 全局Global的还是当前工作区的 configuration
- 要调试C++还是Python
2023.5.24 VSCode 1.78.2
{
// "launch"用来全局一键调试
// VSCode Debug Doc https://code.visualstudio.com/docs/editor/debugging
// .json中的变量 ${workspaceFolder}, ${fileDirname}, ${file}, ${fileBasenameNoExtension}
// 全部变量 https://code.visualstudio.com/docs/editor/variables-reference
"launch":{
"version":"0.2.0",
"configurations":[
{
"name":"Global_C++_Debug",
"type":"cppdbg",
"request":"launch",
"cwd":"${fileDirname}",
"environment":[],
"program":"${fileDirname}/${fileBasenameNoExtension}.exe", // linux去掉`.exe`
"args":[],
"stopAtEntry":false,
"externalConsole":false,
"MIMode":"gdb",
"miDebuggerPath":"E:/MinGW64/mingw64/bin/gdb.exe", // linux修改为"gdb"
"setupCommands":[
{
"description":"为gdb启用整齐打印",
"text":"-enable-pretty-printing",
"ignoreFailures":true
}
],
"presentation": {
"echo": true
}
},
{
"name":"Global_Python_Debug",
"type":"python",
"request":"launch",
"cwd":"${fileDirname}",
"env": {
// 设置了Python path, 可从当前目录的其他文件夹导入py文件
"PYTHONPATH": "${workspaceFolder}"
},
"program":"${file}",
"args":[],
"console":"integratedTerminal"
}
]
},
// Code Runner的命令
"code-runner.terminalRoot": "/", // for running in git bash
"code-runner.ignoreSelection": true, // 运行文件,而不是当前选择的文本
"code-runner.runInTerminal": true,
"code-runner.preserveFocus": true, // 不要移动光标位置
"code-runner.saveFileBeforeRun": true, // 运行前保存
"code-runner.enableAppInsights": 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++11 $fileName -o $fileNameWithoutExt -g && $dir$fileNameWithoutExt", // 添加了 -g
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "cd $dir && python3 -u $fileName", // 改成了python3
"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"
},
"C_Cpp.default.compilerPath": "E:\\MinGW64\\mingw64\\bin\\g++.exe",
"python.defaultInterpreterPath": "E:\\Miniconda3\\anzhuang\\envs\\ysy\\python.exe",
"telemetry.telemetryLevel": "off",
"update.enableWindowsBackgroundUpdates": false, // 关闭后台自动下载更新
"workbench.startupEditor": "newUntitledFile",
"editor.minimap.enabled": false,
"editor.mouseWheelZoom": true,
"security.workspace.trust.untrustedFiles": "open",
"security.workspace.trust.enabled": false,
"explorer.confirmDelete": false,
"explorer.confirmDragAndDrop": false,
// "editor.acceptSuggestionOnEnter": "off", // 回车时不接受提示,强制换行
"editor.suggestSelection": "first",
"editor.suggest.snippetsPreventQuickSuggestions": false,
"extensions.ignoreRecommendations": true,
"workbench.editor.untitled.hint": "hidden",
"workbench.editorAssociations": {
"*.ipynb": "jupyter-notebook"
},
"notebook.cellToolbarLocation": {
"default": "right",
"jupyter-notebook": "left"
},
"gitlens.menus": false,
"gitlens.telemetry.enabled": false,
"gitlens.defaultDateFormat": null,
// 选择默认终端 Settings/Terminal/Integrated/Default Profile/Windows
"terminal.integrated.defaultProfile.windows": "Git Bash", // PowerShell, Command Prompt, Git Bash
"terminal.integrated.fontFamily": "Consolas",
"terminal.integrated.tabs.enabled": false,
"cmake.configureOnOpen": false,
"markdown.updateLinksOnFileMove.enabled": "prompt",
"gitlens.hovers.currentLine.enabled": false
}