前言
以下是vscode的简单配置步骤,主要围绕.vscode文件夹的配置展开,由于网上对于VSCode的安装和MinGW的下载和环境变量的配置教程较多也很简单,本文不再赘述。
第一部分是.vscode文件夹的基础配置,使用此配置文件可以简单编译,运行和调试单文件和多文件的C语言程序,不能实现其他复杂任务
第二部分是.vscode文件夹的进阶配置,增加了C++的支持以及x86,x64以及debug和release模式的切换以及其他一些小功能
如有错误,欢迎指正:)
这是一些VSCode的快捷键以及配置文件时能用到的一些知识
Ctrl+Shift+P --- 打开命令面板
Ctrl+Shift+B --- 开始执行编译任务
调试相关快捷键:
F5 --- 启动调试/全速运行到下一个断点
Shift+F5 --- 停止
Ctrl+Shift+F5 --- 重新运行
F10 --- 单步跳过
F11 --- 单步进入
Shift+F11 --- 单步跳出
${workspaceFolder} --- 在VSCode中打开文件夹的绝对路径
${workspaceRoot} --- 在VSCode中打开文件夹的绝对路径+文件夹的名字
${workspaceRootFolderName} --- 在VSCode中打开文件夹的名字
${file} --- 文件自身的绝对路径
${fileBasename} --- 当前打开的文件名+后缀名(不包括路径)
${fileBasenameNoExtension} --- 当前打开的文件的文件名(不包括路径和后缀名)
${fileDirname} --- 当前打开的文件所在的绝对路径,不包括文件名
${fileExtname} --- 当前打开的文件的后缀名
${relativeFile} --- 相对于${workspaceFolder},当前打开的文件路径
${relativeFileDirname} --- 相对于${workspaceFolder},当前打开文件的目录名
${lineNumber} --- 当前文件光标所在的行号
.vscode文件夹基础配置
c_cpp_properties.json是用mingw编译的配置文件
下面是参考配置
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22621.0",
"compilerPath": "C:/mingw32/bin/gcc.exe",
//这里给出具体编译器位置,环境变量要提前配置好
"cStandard": "c17",//这里指定c语言标准
"cppStandard": "c++17",//这里指定c++标准
"intelliSenseMode": "linux-gcc-x86",
"compilerArgs": []
}
],
"version": 4
}
tasks.json是VSCode运行编译任务时的一些配置,要注意单文件和多文件的配置不同,
下面将分别给出说明
//单文件tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "C:/mingw32/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",//这是要编译的文件名
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
/*这是自动生成的原始配置,生成的exe文件在源文件所在的文件夹内
如果要保存在当前文件夹内的bin文件夹内,则改成
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"*/
],
"options": {
"cwd": "C:/mingw32/bin"//这里依然是编译器路径
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: C:/mingw32/bin/gcc.exe"
}
]
}
多文件编译要把所有文件放到一个文件夹内
.c/.cpp文件可以建一个src文件夹
.h文件可以建一个inc文件夹
//多文件tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "C:/mingw32/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${fileDirname}\\*.c",//当前路径下所有的.c文件
"${fileDirname}\\*.h",//当前路径下所有的.h文件
/*如果把.c和.h文件分到了src和inc文件夹,目前看来只能把
${fileDirname}换成具体的路径*/
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
/*目前这样生成的.exe文件只能命名为选中源文件的名称(例如选中main.c
只能生成叫main.exe的文件),如果只单独打开这一个文件夹的话则无上述问
题,只需要改成
"${fileDirname}\\bin\\${workspaceFolderBasename}.exe"即可*/
],
"options": {
"cwd": "C:/mingw32/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: C:/mingw32/bin/gcc.exe"
}
]
}
launch.json是VSCode的调试配置文件,如果没有在调试界面点击生成即可,进入
launch.json后右下角点击添加配置,选择gdb调试,自动生成模板后内容如下,单文件和多文件配置一致,因为都是生成一个exe文件
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "输入程序名称,例如 ${workspaceFolder}/a.exe",
/*这是例子,需要按照具体情况修改
例如修改为${fileDirname}\\bin\\${fileBasenameNoExtension}.exe */
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/gdb",
/*这里是gdb路径,例如C:\\mingw32\\bin\\gdb.exe*/
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
}
基础配置不包含程序的运行,若要运行可以在VSCode中用PowerShell运行,但需要注意用powershell运行程序时必须先cd到该程序所在的文件夹(不能是父文件夹)不然会报错
.vscode文件夹进阶配置
x86和x64编译的切换
在c_cpp_properties.json中添加Win64编译选项``
{
"name": "Win64",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22621.0",
"compilerPath": "C:/mingw64/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
tasks.json中添加x64编译任务
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "C:/mingw64/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",//单文件编译,多文件搜索目录如下
//"${fileDirname}\\*.c",
//"${fileDirname}\\*.h",
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": false
},
"detail": "编译器: C:/mingw64/bin/gcc.exe"
}
debug和release的切换以及代码优化水平的设置
-g --- 用于生成调试信息,也就是debug模式
-O1 --- 代码优化水平1
-O2 --- 代码优化水平2
O1和O2都是属于release模式,代码优化水平O1<O2
-o --- 指定输出文件名称,后面接要输出文件的路径和名称
debug时自动编译
在launch.json中加入"preLaunchTask":"build"
表示在调试前先执行编译任务,值与task中的label对应
默认启动项
在tasks.json中的group里加入"inDefault": true表示该task为默认项,只能有一个默认
task否则全部当非默认处理
build后直接run
在tasks.json中加入一个新任务,增加dependsOn: "build"和
"command": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe
其中"dependsOn"表示任务依赖,表示运行这个任务前要先执行build任务,也就是编译,名称和编译任务"label"值对应
"command"中执行exe文件,位置要与build任务中exe的生成位置相同
执行task时光标位置设定
在tasks.json的"presentasion"里添加"focus": true,表示在执行task时光标在终端,方
便进行输入,但对于build任务意义不大,在执行run任务时可以改为true
debug默认在main函数入口处停下
在launch.json中加入"stopAtEntry": true,相当于在main上打断点
中文乱码情况的解决
在args中添加"-fexec-charset=GB18030"使文件以GB18030编码
这里提一下GB2312,GB18030,GBK编码的区别
GB2312是第一个汉字编码的国家标准,GBK是GB2312的升级版,增加了更多的字符,而GB18030则是GBK的升级版,兼容Unicode编码,所以用GB18030更好
C++编译的两种方法
方法一:把原先的gcc换成g++就可以自动链接标准库
方法二:在C语言编译task的基础上,在args中加入"-lstdc++",这样就可以在用gcc编译时连接C++标准库,编译C语言也不受影响,值得注意的是"-lstdc++"在args中的位置,args是从上至下执行命令,"-lstdc++"不能在寻找源文件之前运行,否则会报错,所以添加在-o找到源文件之后
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-lstdc++",
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"-fexec-charset=GB18030"
],
tasks.json中"presentation"设置详解
"echo" --- 该设置控制任务的输出是否在 VSCode 终端中显示。当设置为true时,任务输
出将显示在终端中;当设置为false时,任务输出将不显示在终端中
"reveal" --- 该设置控制任务输出是否在终端中打开。当设置为always时,每次任务运行
时,终端都会自动打开;当设置为silent时,终端不会自动打开,除非手动打开;当设置为never时,终端始终不打开
"focus" --- 该设置控制在任务运行时是否激活终端。当设置为true时,终端窗口将自动激
活;当设置为 false 时,终端窗口不会自动激活
"panel" --- 该设置控制任务输出是否与其他任务共享终端面板。当设置为shared时,任务
输出将与其他任务共享同一个终端面板;当设置为dedicated时,任务输出将在一个单独的终端面板中显示,不与其他任务共享
下面给出完整文件
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22621.0",
"compilerPath": "C:/mingw32/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x86",
"compilerArgs": []
},
{
"name": "Win64",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22621.0",
"compilerPath": "C:/mingw64/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 构建x86活动文件 (Debug)",
"command": "C:/mingw32/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-lstdc++",
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"-fexec-charset=GB18030"
],
"options": {
"cwd": "C:/mingw32/bin"
},
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": false
},
"detail": "编译器: C:/mingw32/bin/gcc.exe",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new"
}
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 构建x86活动文件 (Release)",
"command": "C:/mingw32/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-O1",
"${file}",
"-lstdc++",
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"-fexec-charset=GB18030"
],
"options": {
"cwd": "C:/mingw32/bin"
},
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": false
},
"detail": "编译器: C:/mingw32/bin/gcc.exe",
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "new",
}
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 构建x64活动文件 (Debug)",
"command": "C:/mingw64/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-lstdc++",
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"-fexec-charset=GB18030"
],
"options": {
"cwd": "C:/mingw64/bin"
},
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": false
},
"detail": "编译器: C:/mingw64/bin/gcc.exe",
"presentation": {
"echo": true,
"reveal": "never",
"focus": false,
"panel": "new"
}
},
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 构建x64活动文件 (Release)",
"command": "C:/mingw64/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-O1",
"${file}",
"-lstdc++",
"-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"-fexec-charset=GB18030"
],
"options": {
"cwd": "C:/mingw64/bin"
},
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": false
},
"detail": "编译器: C:/mingw64/bin/gcc.exe",
"presentation": {
"echo": true,
"reveal": "never",
"focus": false,
"panel": "new"
}
},
{
"label": "清理构建文件",
"type": "shell",
"command": "del ${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"group": {
"kind": "none"
},
"problemMatcher": []
},
{
"label": "运行x86文件",
"type": "shell",
"dependsOn": [
"C/C++: gcc.exe 构建x86活动文件 (Release)"
],
"command": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"group": {
"kind": "test",
"isDefault": false
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "new"
}
},
{
"label": "运行x64文件",
"type": "shell",
"dependsOn": [
"C/C++: gcc.exe 构建x64活动文件 (Release)"
],
"command": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"group": {
"kind": "test",
"isDefault": false
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "new"
}
}
]
}
launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "GDB x86 Debug",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 构建x86活动文件 (Debug)",
},
{
"name": "GDB x64 Debug",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 构建x64活动文件 (Debug)"
}
]
}