声明:本教程仅适用于用VScode的Code Runner插件运行一个C(C++)程序,不包含调试。
本文用到的所有资源在文章末尾的链接里,如果需要可以通过文末链接下载。
安装VScode
VScode官网:https://code.visualstudio.com/
VScode下载页面:https://code.visualstudio.com/Download
在安装过程中,一定要勾选“添加到PATH(重启系统后生效)”以及“将Code注册为受支持的文件类型编辑器”。“创建桌面快捷方式”视个人情况勾选。
安装完成后重启系统,进行下一步操作。
安装MinGW
由于目前在大多数地区直接打开MinGW-w64安装程序安装时,在线下载的界面总是报错,所以我们选择采用SourceForge网站来下载MinGW的安装器。 如果各位用MinGW-w64安装程序中途安装失败时,就选择用SourceForge的下载吧。 本文介绍的是用SourceForge网站来下载MinGW的安装器这一方法。
MinGW 在 SourceForge 网站的下载链接:https://sourceforge.net/projects/mingw/files/latest/download
下载完毕后,打开 mingw-get-setup,点击 install,选择安装路径,默认的安装路径为 C:\MinGW,点击 continue 继续,然后再点击 continue 继续。
然后点击左侧 All Packages,点击 MinGW,然后在右侧寻找 mingw32-gcc、mingw32-g++、mingw32-gdb(++class属性全部为bin++)并点击小方框,点击 Mark for Installation 。
注意:只需点击 class 为 bin 的项目即可,其余的必需配件会在你勾选以上三个文件以后自动勾选上,不用动手。
确认无误后,点击左上角 Installation ,点击 Apply Changes,在弹出框点击 Apply,等待其安装。
安装完毕后,点击 Close,然后关闭 mingw-get-setup,在 C:\MinGW\bin 这个目录下寻找 gcc 、g++ 、 gdb 这三个可执行程序,如果有则证明安装成功。
配置环境变量
配置MinGW环境
鼠标右键“此电脑”->“属性”,往下滑,打开“高级系统设置”,点击“环境变量”,在系统变量里点“新建”,之后填写MinGW的安装路径,如下:
然后在“系统环境变量”找到Path,双击打开以后,点击“新建”,依次输入 %MinGW%\bin、C:\MinGW,之后点击确定。
检查环境是否生效
为了检查MinGW是否正确安装和使用,打开cmd窗口,分别输入下面两条指令:
g++ --version
gdb --version
显示如下内容则证明安装成功,并且可以使用。
g++ --version
g++ (MinGW.org GCC-6.3.0-1) 6.3.0
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
gdb --version
GNU gdb (GDB) 7.6.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
配置环境
在电脑里新建一个文件夹,文件夹以及路径的名称必须是全英文!这个文件夹将以后作为存放所有你的C(C++)代码的地方。
打开VScode,点左上角“文件”→“打开文件夹”,然后打开你所创建的那个文件夹。(如果看不懂VScode的全英界面的话,可以去插件库搜索简体中文扩展并且安装,具体方法自行百度,本文不再提及了。)
点击下图这里,创建一个名为 .vscode 的文件夹:(不要随便起文件名,必须叫 .vscode)
这时候再次打开文末链接,下载那四个 .json 文件复制到这个 .vscode 文件夹里面。
然后下载那一个 Hello.c 文件放在 .vscode 文件夹外面。如下图所示:
安装 Code Runner
在VScode内最左侧栏点击扩展(就四个正方形那个图标),然后查询插件 Code Runner,安装。然后点击最上方一栏中的 文件→首选项→设置,查找 settings.json,如下图所示:
打开该文件以后在大括号里的原有内容最后面添加一个英文逗号 (,) ,然后换行,将以下代码复制进去:
"code-runner.runInTerminal": true,
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc \"$fileName\" -o \"$fileNameWithoutExt\" && .\\\"$fileNameWithoutExt.exe\"",
"cpp": "cd $dir && g++ \"$fileName\" -o \"$fileNameWithoutExt\" && .\\\"$fileNameWithoutExt.exe\"",
"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"
}
按Ctrl + S进行保存,最后打开Hello.c文件,右击空白部分,点击Run Code 即可成功运行。
至此,配置结束。
文中用到的所有资源地址:https://cup163.lanzouf.com/itpbK029acuj 密码:3ha5