Visual Studio太过庞大,为了方便平常练习,想配置一下VSCode,捣鼓了几个小时才配好,故有此文给大家分享经验。
安装GCC
这里以MinGW为例(我用的)
下载地址Releases · niXman/mingw-builds-binaries(MinGW官网给出的Windows下载地址,目前没有找到镜像,实在进不去我开个网盘放评论区吧)
其他系统请移步官网Pre-built Toolchains - mingw-w64
下载完成后直接解压即可(我的目录为E:\MinGW\)
然后进入bin目录,在上方目录栏输入cmd,然后输入以下命令mingw-get install gcc g++,安装g++(我这里已经安装过了,没安装过的等待下载即可)
配置VSCode
安装以下插件
接下来打开你的VSCode创建以下目录和文件(practice是我的项目名,自定义你自己的即可)
然后给出我的文件配置,各位照抄即可
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"E:\\MinGW\\bin"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "E:\\MinGW\\bin\\gcc.exe",
"cStandard": "c11",
"intelliSenseMode": "windows-gcc-x64",
"cppStandard": "c++11"
}
],
"version": 4
}
launch.json
{
"version": "0.2.0",
"configurations": [
{ //‘调试(Debug)
"name": "Debug",
"type": "cppdbg",
// cppdbg对应cpptools提供的调试功能;只能是cppdbg
"request": "launch",
//这里program指编译好的exe可执行文件的路径,与tasks中要对应
"program": "${fileDirname}\\output\\${fileBasenameNoExtension}.exe", //(单文件调试)
//"program": "${workspaceFolder}\\${workspaceRootFolderName}.exe", //(多文件调试)
"args": [],
"stopAtEntry": false, // 这里改为true作用等同于在main处打断点
"cwd": "${fileDirname}", // 调试程序时的工作目录,即为源代码所在目录,不用改
"environment": [],
"externalConsole": true, // 改为true时为使用cmd终端,推荐使用vscode内部终端
"internalConsoleOptions": "neverOpen", // 设为true为调试时聚焦调试控制台,新手用不到
"MIMode": "gdb",
"miDebuggerPath": "E:\\MinGW\\bin\\gdb.exe",// 指定调试器所在路径,注意间隔是\\,请修改为你的路径
"preLaunchTask": "build" // 调试开始前执行的任务(任务依赖),与tasks.json的label相对应
}
]
}
settings.json
{
//?系统配置
"debug.onTaskErrors": "debugAnyway",
//调试时忽略异常
"diffEditor.wordWrap": "on",
//视距折行
"editor.cursorBlinking": "smooth",
//光标闪烁
"editor.cursorSmoothCaretAnimation": "on",
//光标动画
"editor.fontSize": 20,
//字体大小
"editor.guides.bracketPairs": true,
//匹配括号
"editor.mouseWheelZoom": true,
//缩放
"editor.smoothScrolling": true,
//平滑滚动
"editor.suggestSelection": "first",
//建议选择
"editor.wordWrap": "on",
//视距折行
"editor.rulers":[120],
//设置标尺
"editor.tabSize": 4,
//设置tab缩进
"explorer.confirmDelete": false,
//删除文件时不询问
"files.autoGuessEncoding": true,
//自动猜测编码
"files.autoSave": "afterDelay",
//自动保存
"security.workspace.trust.untrustedFiles": "open",
//信任打开的文件
"terminal.integrated.enableMultiLinePasteWarning": false,
//禁止多行粘贴警告
"terminal.integrated.smoothScrolling": true,
//平滑滚动
"window.dialogStyle": "custom",
//自定义对话框样式
"workbench.colorTheme": "One Dark Pro",
//主题
"workbench.iconTheme": "material-icon-theme",
//图标主题
"workbench.list.smoothScrolling": true,
//平滑滚动
//?c语言插件配置
"cmake.configureOnOpen": true,
//在打开文件时配置CMake项目
//?oneDarkPro主题配置
"oneDarkPro.italic": false,
//不使用斜体
//?codesnap插件配置
"codesnap.backgroundColor": "#f2f2f2",
//背景颜色
"codesnap.boxShadow": "5px 5px 60px 0px #888888",
//阴影
"codesnap.containerPadding": "3em",
//容器内边距
"codesnap.roundedCorners": true,
//圆角
"codesnap.showWindowControls": false,
//显示窗口控制按钮
"codesnap.showWindowTitle": false,
//显示窗口标题
"codesnap.showLineNumbers": true,
//显示行号
"codesnap.realLineNumbers": false,
//使用真实行号
"codesnap.transparentBackground": false,
//透明背景
"codesnap.target": "container",
//目标
//?codegeex插件配置
"Codegeex.Comment.LanguagePreference": "中文",
//语言偏好
"Codegeex.CompletionDelay": 1,
//完成延迟
"Codegeex.GenerationPreference": "line by line",
//生成偏好
"Codegeex.UseSimilarFileForPrompt": true,
//使用相似文件进行提示
"Codegeex.Privacy": false,
//不使用隐私
"cmake.showOptionsMovedNotification": false,
"C_Cpp.default.compilerPath": "E:\\MinGW\\bin",
"files.associations": {
"iomanip": "cpp",
"iostream": "cpp",
"iosfwd": "cpp"
},
//不显示选项已移动的通知
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"${file}",
"-o",
"${fileDirname}\\output\\${fileBasenameNoExtension}.exe",
"-g",
"-Wall",
"-static-libgcc",
"-fexec-charset=GBK",
"-std=c11"
],
"group": "build",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new"
},
"problemMatcher": "$gcc"
},
{
"label": "run",
"type": "shell",
"dependsOn": "build",
"command": "${fileDirname}\\output\\${fileBasenameNoExtension}.exe",
// "command": "${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe",
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "new"
}
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "E:\\MinGW\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\output\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
]
}
创建完成后,创建你的C/C++代码即可
运行F6
调试,直接打断点调试即可