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