VSCode 自定义编译任务与调试¶
本页最后更新于 ,内容可能过时
第一期:单文件与多文件调试¶
调试环境由两个配置文件组成,这期视频演示的就是它们的生成过程:
.vscode/tasks.json:编译任务,由 C/C++ 插件生成,决定用哪条命令编译,以及确定生成的可执行文件叫什么名字.vscode/launch.json:调试任务,由 C/C++ 插件生成(视频中 MacOS 还需要使用 CodeLLDB 插件),决定调试器选择调试哪个可执行文件
这期的流程讲得最清楚,建议完整看一遍。
延伸:GNU 系与 LLVM 系的来历
课程里装的是 GCC 和 GDB,属于 GNU 系;视频中用到的工具是 Clang 和 LLDB,属于 LLVM 系。
| GNU 系 | LLVM 系 | |
|---|---|---|
| 编译器 | gcc / g++ |
clang |
| 调试器 | gdb |
lldb |
GCC 是 GNU Compiler Collection 的缩写,最初叫 GNU C Compiler(1987 年,Richard Stallman 发起)。
LLVM 是 Apple 为了换掉 GCC 的开源生态而发展起来的:Apple 的自家产品一向不开源,而 GCC 从 4.3 版起改用 GPLv3,改过的编译器必须连源码一起发布,在闭源产品里放一份自己改的 GNU 工具链因此不可行。
因为使用的编译套件不同,所以看这期视频的时候不用照抄配置,重点是弄清两个文件各管什么,点击「运行」和「调试」的背后依次发生了什么。
第二期:换成 GCC 与 GDB¶
这期在《VSCode 入门教程》里出现过,讲的是 C/C++ 开发环境的整套配置,从这里开始看即可。
照着配之前有一点要自己换:这期演示的是 C++,编译命令用的是 g++。而课程里写的是 C 代码,配置的时候把 g++ 换成 gcc 即可,其余不需要改。
助教的 VSCode 完整配置文件¶
settings.json
// 只保留这个默认配置文件 Default 就够用了
// 其他的用户自定义配置文件都可以删除
{
"window.zoomLevel": 0,
"window.zoomPerWindow": true,
"window.commandCenter": true,
"window.title": "${rootName}",
"window.newWindowProfile": "Default",
"window.dialogStyle": "custom",
"window.restoreWindows": "none",
"window.menuBarVisibility": "hidden",
"window.enableMenuBarMnemonics": false,
"window.newWindowDimensions": "maximized",
"security.workspace.trust.enabled": false,
"workbench.colorTheme": "GitHub Dark",
"workbench.iconTheme": "vscode-icons",
"workbench.productIconTheme": "fluent-icons",
"workbench.settings.editor": "json",
"workbench.experimental.modernUI": false,
"workbench.layoutControl.enabled": false,
"workbench.panel.defaultLocation": "right",
"workbench.editorLargeFileConfirmation": 4,
"workbench.navigationControl.enabled": false,
"workbench.secondarySideBar.defaultVisibility": "hidden",
"workbench.editor.useModal": "off",
"workbench.editor.wrapTabs": true,
"workbench.editor.empty.hint": "hidden",
"workbench.editor.scrollToSwitchTabs": true,
"workbench.editor.alwaysShowEditorActions": true,
"workbench.editor.doubleClickTabToToggleEditorGroupSizes": "off",
"workbench.editor.customLabels.enabled": true,
"workbench.editor.customLabels.patterns": {
"**/SKILL.md": "${dirname} /SKILL.md",
},
"explorer.confirmDelete": false,
"explorer.confirmDragAndDrop": false,
"explorer.confirmPasteNative": false,
"explorer.fileNesting.enabled": true,
"explorer.fileNesting.expand": false,
"explorer.incrementalNaming": "smart",
"explorer.fileNesting.patterns": {
"package.json": "package-lock.json",
"pyproject.toml": "uv.lock",
},
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 300,
"files.exclude": {
"**/.gitignore": true,
"**/.gitkeep": true,
"**/__pycache__": true,
"**/uv.lock": true,
},
"editor.tabSize": 4,
"editor.fontSize": 15,
"editor.lineHeight": 1.65,
"editor.fontLigatures": false,
"editor.fontFamily": "Cascadia Code",
"editor.lightbulb.enabled": "off",
"editor.unicodeHighlight.ambiguousCharacters": false,
"terminal.integrated.fontSize": 14,
"terminal.integrated.fontFamily": "Cascadia Code",
"terminal.integrated.defaultProfile.windows": "PowerShell",
"terminal.integrated.inheritEnv": false,
"terminal.integrated.initialHint": false,
"terminal.integrated.enableMultiLinePasteWarning": "never",
"scm.diffDecorations": "none",
"search.useIgnoreFiles": true,
"network.meteredConnection": "off",
"extensions.ignoreRecommendations": true,
"settingsSync.keybindingsPerPlatform": false,
"vsicons.dontShowNewVersionMessage": true,
"git.autofetch": true,
"git.confirmSync": false,
"git.enableSmartCommit": true,
"git.decorations.enabled": false,
"git.openRepositoryInParentFolders": "never",
"github.copilot.nextEditSuggestions.enabled": false,
"github.copilot.chat.localeOverride": "zh-CN",
"claudeCode.useTerminal": true,
"markdown.preview.fontSize": 15,
"markdown.preview.breaks": true,
"markdown.preview.linkify": true,
"markdown.preview.lineHeight": 1.6,
"markdown.preview.markEditorSelection": false,
"markdown.preview.scrollPreviewWithEditor": true,
"markdown.preview.scrollEditorWithPreview": false,
"markdown.preview.doubleClickToSwitchToEditor": true,
"markdown.preview.fontFamily": "Cascadia Code",
"markdown.styles": [ "https://cdn.jsdelivr.net/gh/EthanCaol/mathpix-markdown-studio@f9822203519b440daed65d5d767af4425e9cb1e2/styles/Markdown.css" ],
"[markdown]": {
"editor.wordWrap": "off",
"editor.wordWrapColumn": 60,
"editor.tabSize": 4,
"editor.defaultFormatter": "yzhang.markdown-all-in-one"
},
"code-runner.saveAllFilesBeforeRun": true,
"code-runner.runInTerminal": true,
"code-runner.executorMap": {
"typescript": "cd $dir && npm run dev",
"python": "cd $dir && python $fileName",
"cpp": "cd $dir && g++ -o $fileNameWithoutExt $fileName && $dir$fileNameWithoutExt",
"c": "cd $dir && gcc -o $fileNameWithoutExt $fileName && $dir$fileNameWithoutExt",
},
"code-runner.executorMapByFileExtension": {
".cu": "cd $dir && nvcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
".vue": "cd $dir && npm run dev",
},
"bashIde.shfmt.path": "shfmt",
"bashIde.shellcheckPath": "shellcheck",
"[shellscript]": {
"editor.defaultFormatter": "mads-hartmann.bash-ide-vscode"
},
"C_Cpp.default.cStandard": "gnu23",
"C_Cpp.default.cppStandard": "gnu++23",
"C_Cpp.formatting": "clangFormat",
"C_Cpp.clang_format_sortIncludes": false,
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: WebKit, IndentWidth: 4, ColumnLimit: 120, AlignTrailingComments: true, IndentCaseLabels: true, BreakTemplateDeclarations: true }",
"C_Cpp.inlayHints.parameterNames.enabled": true,
"C_Cpp.inlayHints.referenceOperator.enabled": false,
"C_Cpp.inlayHints.autoDeclarationTypes.enabled": true,
"C_Cpp.inlayHints.autoDeclarationTypes.showOnLeft": true,
"C_Cpp.inlayHints.parameterNames.hideLeadingUnderscores": true,
"C_Cpp.inlayHints.parameterNames.suppressWhenArgumentContainsName": true,
"makefile.makePath": "make",
"makefile.configureOnOpen": false,
"makefile.configureOnEdit": false,
"makefile.makefilePath": "${workspaceFolder}/Makefile",
"cmake.cmakePath": "cmake",
"cmake.configureOnOpen": false,
"cmake.configureOnEdit": false,
"cmake.generator": "Unix Makefiles",
"cmake.buildBeforeRun": true,
"cmake.clearOutputBeforeBuild": true,
"cmake.sourceDirectory": "${workspaceFolder}",
"cmake.buildDirectory": "${workspaceFolder}/build",
"cmake.debugConfig": { "args": [] },
"cmake.pinnedCommands": [
"workbench.action.tasks.configureTaskRunner",
"workbench.action.tasks.runTask"
],
"python.analysis.typeCheckingMode": "off",
"python.analysis.inlayHints.variableTypes": true,
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.callArgumentNames": "partial",
"python.terminal.activateEnvironment": false,
"python.terminal.activateEnvInCurrentTerminal": false,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
},
"jupyter.askForKernelRestart": false,
"jupyter.interactiveWindow.textEditor.executeSelection": true,
"notebook.markup.fontSize": 20,
"notebook.stickyScroll.enabled": true,
"notebook.output.fontSize": 15,
"notebook.output.lineHeight": 0,
"notebook.output.wordWrap": false,
"notebook.output.scrolling": false,
"notebook.output.textLineLimit": 40,
"notebook.output.linkifyFilePaths": true,
"notebook.output.fontFamily": "Cascadia Code",
"remote.SSH.remotePlatform": {
"mind-city": "linux"
},
}
文档完成登记:完成本文全部流程后告知助教

