1.安装VSCode插件和MingGW
用VSCode运行C语言和C++还是比较方便的,虽然说没有VS那种直接集成的大环境,但是平时自己做些小规模的测试运行都已经足够了
VSCode中支持C/C++运行的插件有很多,可以使用Code Runner,也可以使用C/C++ Complie Run
下载MinGW
装好插件还不行,在VSCode上运行C++还需要MingW,可以在SourceForge上下载:MinGW-w64 - for 32 and 64 bit Windows download | SourceForge.net
当然,也可以点击我分享的百度网盘链接下载(阿里云盘不支持该格式文件的分享。。。)
链接:https://pan.baidu.com/s/1rs6mLfuVcM6w6TuFlqZUSA
提取码:5zbo
下载成功之后可以看到是这种格式的压缩包,可以用WinRAR解压看一下,解压之后是mingw64这个文件夹
安装MinGW(路径中不要有中文)
将这个压缩包解压到你想要的地方(路径中不要有中文,可能会报错!!!!),这里我解压的路径是 D:\VSCode\VSCode_Install
配置环境变量,按下 Windows +X,再按Y,可以快速进入系统页面,点击高级系统设置,配置环境变量
添加MinGW的bin目录到Path中
配置好之后使用 Win+R,输入cmd, 输入 gcc -v,如果出现下图结果表示配置成功
2.用Code Runner运行
进入VSCode的设置,输入code runner,即可查看相关配置
进入设置,点击右上角的图标可以进入settings.json,这是VSCode的系统设置文件,直接将下面的json代码粘贴进去即可,
"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++ -fexec-charset=GBK -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "set PYTHONIOENCODING=utf8 && python",
"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"
},
"code-runner.executorMapByFileExtension": {
".vb": "cd $dir && vbc /nologo $fileName && $dir$fileNameWithoutExt",
".vbs": "cscript //Nologo",
".scala": "scala",
".jl": "julia",
".cr": "crystal",
".ml": "ocaml",
".exs": "elixir",
".hx": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
".rkt": "racket",
".scm": "csi -script",
".ahk": "autohotkey",
".au3": "autoit3",
".kt": "cd $dir && kotlinc $fileName -include-runtime -d $fileNameWithoutExt.jar && java -jar $fileNameWithoutExt.jar",
".kts": "kotlinc -script",
".dart": "dart",
".pas": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
".pp": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
".d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
".hs": "runhaskell",
".nim": "nim compile --verbosity:0 --hints:off --run",
".csproj": "dotnet run --project",
".fsproj": "dotnet run --project",
".lisp": "sbcl --script",
".kit": "kitc --run",
".v": "v run",
".vsh": "v run",
".sass": "sass --style expanded",
".cu": "cd $dir && nvcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
".ring": "ring",
".sml": "cd $dir && sml $fileName"
},
"code-runner.runInTerminal": true,
回到设置,也可以不编写settings.json文件,直接在此勾选
这个配置的意思是:是否在终端中运行
如果不勾选,Code Runner就会在OutPut栏中运行,更方便查看结果,但是不能在OutPut中输入数据!
建议勾选,OutPut栏虽然方便查看,但是不能输入这点很伤
OutPut
Terminal
配置好之后,随便编辑一段C++代码,右键选择 Run Code即可运行
3.用C Complie Run运行
Code Runner可以实现快速运行,但是不支持调试,如果想使用调试功能,还是更推荐使用C Complie Run
只不过,用VSCode每次创建一个新的工程都需要在 .vscode 文件夹中配置 task.json 、launch.json 、setting.json,不过不同工程中,这些文件的配置内容可以是相同的,这里备份一下
(1) task.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "你自己的mingw64路径\\bin\\g++.exe",// 这里替换成你自己的mingw路径
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
(2)launch.json
这里需要注意externalConsole这个属性,如果设置为true,那么运行c++文件时会像DEVC++那样弹出一个黑窗口,
如果设置为false,则程序在VSCode终端中运行
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,// 这个属性可以自己修改
"MIMode": "gdb",
"miDebuggerPath": "F:\\codeConfiguration\\MinGW\\bin\\gdb.exe", /*修改成自己bin目录下的gdb.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "task g++"
}
]
}
(3)c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": ["${workspaceFolder}/**"],
"defines": ["_DEBUG", "UNICODE", "_UNICODE"],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "F:\\codeConfiguration\\minGW\\bin\\g++.exe", /*修改成自己bin目录下的g++.exe,这里的路径和电脑里复制的文件目录有一点不一样,这里是两个反斜杠\\*/
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
3.测试运行
可以使用下面这段cpp代码:
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
cout << "你好啊!" << endl;
cout << "hello world!!" << endl;
int n;
cout << "请输入:";
cin >> n;
cout << "输出:" << n << endl;
}
运行结果:
这里我使用的是C Complie Run,我将externalConsole设置成了true,所以会出现黑色窗口
可能会遇到的问题:
1.输出乱码问题(VSCode终端)
可能有的同学运行时会出现下图的效果:
出现错误的原因是文件的编码格式不对,点击文件编码,选择"通过编码保存" (别选错了!!),选择UTF-8格式
也可以打开设置,输入file enco,修改文件默认编码为UTF-8,这样之后打开文件或者编写文件都会默认以UTF-8格式保存
2.输出乱码问题(黑色窗口)
也可能有的同学不是在终端输出,而是选择了和我一样的黑色窗口,发现中文乱码,而且自己已经将文件设置成了UTF-8的格式,使用UTF-8的格式仍然报错
发生这个情况的原因是: VSCode终端默认使用UTF-8编码,而黑色弹窗即Windows命令行窗口默认采用的是GBK编码!
所以这就出现了编码冲突问题,用终端就得用UTF-8,用命令行窗口就得用GBK。。
解决这个问题的办法
可以修改注册表,将命令行窗口默认修改为UTF-8编码格式,这里就参考这篇帖子:Windows CMD永久设置UTF-8编码_遗失的陈雪锋的博客-CSDN博客
修改好之后,开启win R,输入cmd,右键查看属性,如果看到编码格式变成UTF-8即配置完成
3.No such file or dictionary
造成这个问题的原因一般都是路径中带有中文名,改成英文就好了。之前看的很多帖子都说要修改launch.json,我试了很久,最后发现还是路径问题
参考链接:https://wcaicai.blog.csdn.net/article/details/119715544