(编辑:jimmy 日期: 2024/12/23 浏览:2)
vscode简介
什么是vscode
简单来说,vscode是一款文本编辑器,而不是ide。这就是说,vscode只提供编辑的环境而不提供编译的环境,如果想要用vscode来集成开发环境,则必须经过以下几步:
同时,一个文本编辑器肯定不会提供运行程序的功能,vscode也是如此。要运行程序,有如下几种方法:
这样,我们大致了解了使用vscode编译、运行程序的方法。
vscode的配置
首先,我们要明确vscode的配置的最小单元是文件夹,即可以使用打开文件夹功能我们可以把不同的文件夹链接到不同的外部编译器、实现不同的快捷任务、快速进行debug和详细设定拓展插件的功能等等。
因此,我们先速览vscode的配置结构,如下图:
可以看到,
现在,我们一项一项来说:
文件结构
vscode提供三级文件结构,从工作区到文件夹到单个文件:
settings.json
在这个json文件中,我们可以通过键值对的方式设置vscode内置的或拓展插件的各项属性,其中就包括外部编译器地址、各项编译偏好等等。
同时,vscode提供层层嵌套的settings,
tasks.json
launch.json
这其中的内容主要是用来对调试提供支持。**针对不同语言的拓展插件会提供不同的模板。**按f5可以选择模板并生成文件。
vscode的优势
配置实例
了解了vscode的配置原理,我在下面给出一些我自己配置的内容以供参考。
settings.json全局配置
{ //vscode的默认终端,此处设置为cmd "terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe", //拖拽移动文件时不要确认提示 "explorer.confirmDragAndDrop": false, //手动升级vscode "update.mode": "manual", //自动保存,此处设置为永远自动保存 "files.autoSave": "afterDelay", //task的下拉列表中显示历史常用的个数 "task.quickOpen.history": 0, }
对python
{ //控制是否激活pylint //"python.linting.pylintEnabled": true, "python.linting.enabled": true, //如果使用pylint可能会导致奇怪的报错,加上这几句可以取消 //"python.linting.pylintArgs": [ // "--generate-members" //], //是否在自动补全的函数后自动添加括号 "python.autoComplete.addBrackets": false, //是否使用python插件内部的自动补全功能 "python.jediEnabled": true, //python环境的路径 "python.pythonPath": "C:\\apps\\Anaconda\\python.exe", //python自动补全的搜索路径 "python.autoComplete.extraPaths": [ "C:/apps/Anaconda/Lib/site-packages/", "C:/apps/Anaconda/Scripts/" ], }
对c,c++
{ "files.associations": { "*.tcc": "cpp", "array": "cpp", "atomic": "cpp", "cctype": "cpp", "clocale": "cpp", "cmath": "cpp", "cstdarg": "cpp", "cstddef": "cpp", "cstdint": "cpp", "cstdio": "cpp", "cstdlib": "cpp", "cwchar": "cpp", "cwctype": "cpp", "deque": "cpp", "unordered_map": "cpp", "vector": "cpp", "exception": "cpp", "algorithm": "cpp", "functional": "cpp", "iterator": "cpp", "memory": "cpp", "memory_resource": "cpp", "numeric": "cpp", "optional": "cpp", "random": "cpp", "string": "cpp", "string_view": "cpp", "system_error": "cpp", "tuple": "cpp", "type_traits": "cpp", "utility": "cpp", "fstream": "cpp", "initializer_list": "cpp", "iosfwd": "cpp", "iostream": "cpp", "istream": "cpp", "limits": "cpp", "new": "cpp", "ostream": "cpp", "sstream": "cpp", "stdexcept": "cpp", "streambuf": "cpp", "typeinfo": "cpp" } }
对latex
{ //编译方式 "latex-workshop.latex.tools": [ { "name": "latexmk", "command": "latexmk", "args": [ "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "-pdf", "%DOC%" ] }, { "name": "xelatex", "command": "xelatex", "args": [ "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "%DOC%" ] }, { "name": "pdflatex", "command": "pdflatex", "args": [ "-synctex=1", "-interaction=nonstopmode", "-file-line-error", "%DOC%" ] }, { "name": "bibtex", "command": "bibtex", "args": [ "%DOCFILE%" ] } ], "latex-workshop.latex.recipes": [ { "name": "xelatex", "tools": [ "xelatex" ] }, { "name": "latexmk", "tools": [ "latexmk" ] }, { "name": "pdflatex -> bibtex -> pdflatex*2", "tools": [ "pdflatex", "bibtex", "pdflatex", "pdflatex" ] } ], //需要清除文件的后缀 "latex-workshop.latex.clean.fileTypes": [ "*.aux", "*.bbl", "*.blg", "*.idx", "*.ind", "*.lof", "*.lot", "*.out", "*.toc", "*.acn", "*.acr", "*.alg", "*.glg", "*.glo", "*.gls", "*.ist", "*.fls", "*.log", "*.fdb_latexmk", "*.gz" ], //不要显示错误弹窗 "latex-workshop.message.error.show": false, //不要显示信息弹窗 "latex-workshop.message.information.show": false, //不要显示警报弹窗 "latex-workshop.message.warning.show": false, //保存时不要自动编译 "latex-workshop.latex.autoBuild.run": "never", //默认在右边tab预览 "latex-workshop.view.pdf.viewer": "tab", }
tasks.json对C/C++
{ "version": "2.0.0", "command": "g++", "args": ["-g","${file}","-o","${fileBasenameNoExtension}.exe"], // 编译命令参数 "problemMatcher": { "owner": "cpp", "fileLocation": ["relative", "\\"], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } } }
用asymptote画图后进行编译.asy文件,删除多余文件,将结果移动至特定文件夹
{ // See https://go.microsoft.com/fwlink/"version": "2.0.0", "tasks": [ //编译 { //在task列表中显示的文字 "label": "Generate EPS", //运行环境 "type": "shell", //命令 "command": "asy", //参数 "args": [ "-f", "eps", //$开头的都是vscode提供的封装好的变量,具体内容 //可以自己尝试 "${file}" ], //所述属组 "group": "build", //报错处理 "problemMatcher": [] }, //删除文件 { "label": "Delete FILE", "type": "shell", "command": "del", "args": [ "*.aux", "*.log", "*.dvi", "*.pdf" ], //设置为none的task不会在build task的下拉列表中展示 "group": "none", "problemMatcher": [] }, //将生成的eps移动至相应文件夹 { "label": "Move EPS", "type": "shell", "command": "move", "args": [ "/Y", //$开头的都是vscode提供的封装好的变量,具体内容 //可以自己尝试 "${workspaceFolder}\\${fileBasenameNoExtension}.eps", "C:\\Pt_Latex\\Reources\\" ], "group": "build", //在运行这个任务之前,需要提前运行的任务 "dependsOn": [ "Delete FILE" ], "problemMatcher": [] } ] }
launch.json
对C/C++
{ "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示 "type": "cppdbg", // 配置类型,这里只能为cppdbg "request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加) "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",// 将要进行调试的程序的路径 "args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可 "stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false "cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录 "environment": [], "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台 "MIMode": "gdb", "miDebuggerPath": "C:\\apps\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应 "preLaunchTask": "g++", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
对python(默认的就成)
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/"version": "0.2.0", "configurations": [ { "name": "Python: 当前文件", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", //是否在程序开始处停止 "stopOnEntry": false } ] }