• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

VSCode配置避坑指南

武飞扬头像
NianLee
帮助1

前言

以下是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文件夹基础配置

  1. c_cpp_properties.json是用mingw编译的配置文件

下面是参考配置

  1.  
    {
  2.  
    "configurations": [
  3.  
    {
  4.  
    "name": "Win32",
  5.  
    "includePath": [
  6.  
    "${workspaceFolder}/**"
  7.  
    ],
  8.  
    "defines": [
  9.  
    "_DEBUG",
  10.  
    "UNICODE",
  11.  
    "_UNICODE"
  12.  
    ],
  13.  
    "windowsSdkVersion": "10.0.22621.0",
  14.  
    "compilerPath": "C:/mingw32/bin/gcc.exe",
  15.  
    //这里给出具体编译器位置,环境变量要提前配置好
  16.  
    "cStandard": "c17",//这里指定c语言标准
  17.  
    "cppStandard": "c 17",//这里指定c 标准
  18.  
    "intelliSenseMode": "linux-gcc-x86",
  19.  
    "compilerArgs": []
  20.  
    }
  21.  
    ],
  22.  
    "version": 4
  23.  
    }
学新通

  1. tasks.json是VSCode运行编译任务时的一些配置,要注意单文件和多文件的配置不同,

下面将分别给出说明

  1.  
    //单文件tasks.json
  2.  
    {
  3.  
    "version": "2.0.0",
  4.  
    "tasks": [
  5.  
    {
  6.  
    "type": "cppbuild",
  7.  
    "label": "C/C : gcc.exe 生成活动文件",
  8.  
    "command": "C:/mingw32/bin/gcc.exe",
  9.  
    "args": [
  10.  
    "-fdiagnostics-color=always",
  11.  
    "-g",
  12.  
    "${file}",//这是要编译的文件名
  13.  
    "-o",
  14.  
    "${fileDirname}\\${fileBasenameNoExtension}.exe"
  15.  
    /*这是自动生成的原始配置,生成的exe文件在源文件所在的文件夹内
  16.  
    如果要保存在当前文件夹内的bin文件夹内,则改成
  17.  
    "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"*/
  18.  
    ],
  19.  
    "options": {
  20.  
    "cwd": "C:/mingw32/bin"//这里依然是编译器路径
  21.  
    },
  22.  
    "problemMatcher": [
  23.  
    "$gcc"
  24.  
    ],
  25.  
    "group": "build",
  26.  
    "detail": "编译器: C:/mingw32/bin/gcc.exe"
  27.  
    }
  28.  
    ]
  29.  
    }
学新通

多文件编译要把所有文件放到一个文件夹内

.c/.cpp文件可以建一个src文件夹

.h文件可以建一个inc文件夹

  1.  
    //多文件tasks.json
  2.  
    {
  3.  
    "version": "2.0.0",
  4.  
    "tasks": [
  5.  
    {
  6.  
    "type": "cppbuild",
  7.  
    "label": "C/C : gcc.exe 生成活动文件",
  8.  
    "command": "C:/mingw32/bin/gcc.exe",
  9.  
    "args": [
  10.  
    "-fdiagnostics-color=always",
  11.  
    "-g",
  12.  
    "${fileDirname}\\*.c",//当前路径下所有的.c文件
  13.  
    "${fileDirname}\\*.h",//当前路径下所有的.h文件
  14.  
    /*如果把.c和.h文件分到了src和inc文件夹,目前看来只能把
  15.  
    ${fileDirname}换成具体的路径*/
  16.  
    "-o",
  17.  
    "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
  18.  
    /*目前这样生成的.exe文件只能命名为选中源文件的名称(例如选中main.c
  19.  
    只能生成叫main.exe的文件),如果只单独打开这一个文件夹的话则无上述问
  20.  
    题,只需要改成
  21.  
    "${fileDirname}\\bin\\${workspaceFolderBasename}.exe"即可*/
  22.  
    ],
  23.  
    "options": {
  24.  
    "cwd": "C:/mingw32/bin"
  25.  
    },
  26.  
    "problemMatcher": [
  27.  
    "$gcc"
  28.  
    ],
  29.  
    "group": "build",
  30.  
    "detail": "编译器: C:/mingw32/bin/gcc.exe"
  31.  
    }
  32.  
    ]
  33.  
    }
学新通

  1. launch.json是VSCode的调试配置文件,如果没有在调试界面点击生成即可,进入

launch.json后右下角点击添加配置,选择gdb调试,自动生成模板后内容如下,单文件和多文件配置一致,因为都是生成一个exe文件

  1.  
    {
  2.  
    // 使用 IntelliSense 了解相关属性。
  3.  
    // 悬停以查看现有属性的描述。
  4.  
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5.  
    "version": "0.2.0",
  6.  
    "configurations": [
  7.  
    {
  8.  
    "name": "(gdb) 启动",
  9.  
    "type": "cppdbg",
  10.  
    "request": "launch",
  11.  
    "program": "输入程序名称,例如 ${workspaceFolder}/a.exe",
  12.  
    /*这是例子,需要按照具体情况修改
  13.  
    例如修改为${fileDirname}\\bin\\${fileBasenameNoExtension}.exe */
  14.  
    "args": [],
  15.  
    "stopAtEntry": false,
  16.  
    "cwd": "${fileDirname}",
  17.  
    "environment": [],
  18.  
    "externalConsole": false,
  19.  
    "MIMode": "gdb",
  20.  
    "miDebuggerPath": "/path/to/gdb",
  21.  
    /*这里是gdb路径,例如C:\\mingw32\\bin\\gdb.exe*/
  22.  
    "setupCommands": [
  23.  
    {
  24.  
    "description": "为 gdb 启用整齐打印",
  25.  
    "text": "-enable-pretty-printing",
  26.  
    "ignoreFailures": true
  27.  
    },
  28.  
    {
  29.  
    "description": "将反汇编风格设置为 Intel",
  30.  
    "text": "-gdb-set disassembly-flavor intel",
  31.  
    "ignoreFailures": true
  32.  
    }
  33.  
    ]
  34.  
    },
  35.  
    }
学新通

基础配置不包含程序的运行,若要运行可以在VSCode中用PowerShell运行,但需要注意用powershell运行程序时必须先cd到该程序所在的文件夹(不能是父文件夹)不然会报错


.vscode文件夹进阶配置

  1. x86和x64编译的切换

在c_cpp_properties.json中添加Win64编译选项``

  1.  
    {
  2.  
    "name": "Win64",
  3.  
    "includePath": [
  4.  
    "${workspaceFolder}/**"
  5.  
    ],
  6.  
    "defines": [
  7.  
    "_DEBUG",
  8.  
    "UNICODE",
  9.  
    "_UNICODE"
  10.  
    ],
  11.  
    "windowsSdkVersion": "10.0.22621.0",
  12.  
    "compilerPath": "C:/mingw64/bin/gcc.exe",
  13.  
    "cStandard": "c17",
  14.  
    "cppStandard": "c 17",
  15.  
    "intelliSenseMode": "windows-gcc-x64"
  16.  
    }
学新通

tasks.json中添加x64编译任务

  1.  
    {
  2.  
    "type": "cppbuild",
  3.  
    "label": "C/C : gcc.exe 生成活动文件",
  4.  
    "command": "C:/mingw64/bin/gcc.exe",
  5.  
    "args": [
  6.  
    "-fdiagnostics-color=always",
  7.  
    "-g",
  8.  
    "${file}",//单文件编译,多文件搜索目录如下
  9.  
    //"${fileDirname}\\*.c",
  10.  
    //"${fileDirname}\\*.h",
  11.  
    "-o",
  12.  
    "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
  13.  
    ],
  14.  
    "options": {
  15.  
    "cwd": "C:/mingw64/bin"
  16.  
    },
  17.  
    "problemMatcher": [
  18.  
    "$gcc"
  19.  
    ],
  20.  
    "group": {
  21.  
    "kind": "build",
  22.  
    "isDefault": false
  23.  
    },
  24.  
    "detail": "编译器: C:/mingw64/bin/gcc.exe"
  25.  
    }
学新通
  1. debug和release的切换以及代码优化水平的设置

-g --- 用于生成调试信息,也就是debug模式

-O1 --- 代码优化水平1

-O2 --- 代码优化水平2

O1和O2都是属于release模式,代码优化水平O1<O2

-o --- 指定输出文件名称,后面接要输出文件的路径和名称

  1. debug时自动编译

在launch.json中加入"preLaunchTask":"build"

表示在调试前先执行编译任务,值与task中的label对应

  1. 默认启动项

在tasks.json中的group里加入"inDefault": true表示该task为默认项,只能有一个默认

task否则全部当非默认处理

  1. build后直接run

在tasks.json中加入一个新任务,增加dependsOn: "build"和

"command": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe

其中"dependsOn"表示任务依赖,表示运行这个任务前要先执行build任务,也就是编译,名称和编译任务"label"值对应

"command"中执行exe文件,位置要与build任务中exe的生成位置相同

  1. 执行task时光标位置设定

在tasks.json的"presentasion"里添加"focus": true,表示在执行task时光标在终端,方

便进行输入,但对于build任务意义不大,在执行run任务时可以改为true

  1. debug默认在main函数入口处停下

在launch.json中加入"stopAtEntry": true,相当于在main上打断点

  1. 中文乱码情况的解决

在args中添加"-fexec-charset=GB18030"使文件以GB18030编码

这里提一下GB2312,GB18030,GBK编码的区别

GB2312是第一个汉字编码的国家标准,GBK是GB2312的升级版,增加了更多的字符,而GB18030则是GBK的升级版,兼容Unicode编码,所以用GB18030更好

  1. C 编译的两种方法

  • 方法一:把原先的gcc换成g 就可以自动链接标准库

  • 方法二:在C语言编译task的基础上,在args中加入"-lstdc ",这样就可以在用gcc编译时连接C 标准库,编译C语言也不受影响,值得注意的是"-lstdc "在args中的位置,args是从上至下执行命令,"-lstdc "不能在寻找源文件之前运行,否则会报错,所以添加在-o找到源文件之后

  1.  
    "args": [
  2.  
    "-fdiagnostics-color=always",
  3.  
    "-g",
  4.  
    "${file}",
  5.  
    "-lstdc ",
  6.  
    "-o",
  7.  
    "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
  8.  
    "-fexec-charset=GB18030"
  9.  
    ],
  1. tasks.json中"presentation"设置详解

"echo" --- 该设置控制任务的输出是否在 VSCode 终端中显示。当设置为true时,任务输

出将显示在终端中;当设置为false时,任务输出将不显示在终端中

"reveal" --- 该设置控制任务输出是否在终端中打开。当设置为always时,每次任务运行

时,终端都会自动打开;当设置为silent时,终端不会自动打开,除非手动打开;当设置为never时,终端始终不打开

"focus" --- 该设置控制在任务运行时是否激活终端。当设置为true时,终端窗口将自动激

活;当设置为 false 时,终端窗口不会自动激活

"panel" --- 该设置控制任务输出是否与其他任务共享终端面板。当设置为shared时,任务

输出将与其他任务共享同一个终端面板;当设置为dedicated时,任务输出将在一个单独的终端面板中显示,不与其他任务共享


下面给出完整文件

c_cpp_properties.json

  1.  
    {
  2.  
    "configurations": [
  3.  
    {
  4.  
    "name": "Win32",
  5.  
    "includePath": [
  6.  
    "${workspaceFolder}/**"
  7.  
    ],
  8.  
    "defines": [
  9.  
    "_DEBUG",
  10.  
    "UNICODE",
  11.  
    "_UNICODE"
  12.  
    ],
  13.  
    "windowsSdkVersion": "10.0.22621.0",
  14.  
    "compilerPath": "C:/mingw32/bin/gcc.exe",
  15.  
    "cStandard": "c17",
  16.  
    "cppStandard": "c 17",
  17.  
    "intelliSenseMode": "windows-gcc-x86",
  18.  
    "compilerArgs": []
  19.  
    },
  20.  
    {
  21.  
    "name": "Win64",
  22.  
    "includePath": [
  23.  
    "${workspaceFolder}/**"
  24.  
    ],
  25.  
    "defines": [
  26.  
    "_DEBUG",
  27.  
    "UNICODE",
  28.  
    "_UNICODE"
  29.  
    ],
  30.  
    "windowsSdkVersion": "10.0.22621.0",
  31.  
    "compilerPath": "C:/mingw64/bin/gcc.exe",
  32.  
    "cStandard": "c17",
  33.  
    "cppStandard": "c 17",
  34.  
    "intelliSenseMode": "windows-gcc-x64"
  35.  
    }
  36.  
    ],
  37.  
    "version": 4
  38.  
    }
学新通

tasks.json

  1.  
    {
  2.  
    "version": "2.0.0",
  3.  
    "tasks": [
  4.  
    {
  5.  
    "type": "cppbuild",
  6.  
    "label": "C/C : gcc.exe 构建x86活动文件 (Debug)",
  7.  
    "command": "C:/mingw32/bin/gcc.exe",
  8.  
    "args": [
  9.  
    "-fdiagnostics-color=always",
  10.  
    "-g",
  11.  
    "${file}",
  12.  
    "-lstdc ",
  13.  
    "-o",
  14.  
    "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
  15.  
    "-fexec-charset=GB18030"
  16.  
    ],
  17.  
    "options": {
  18.  
    "cwd": "C:/mingw32/bin"
  19.  
    },
  20.  
    "problemMatcher": "$gcc",
  21.  
    "group": {
  22.  
    "kind": "build",
  23.  
    "isDefault": false
  24.  
    },
  25.  
    "detail": "编译器: C:/mingw32/bin/gcc.exe",
  26.  
    "presentation": {
  27.  
    "echo": true,
  28.  
    "reveal": "always",
  29.  
    "focus": false,
  30.  
    "panel": "new"
  31.  
    }
  32.  
    },
  33.  
    {
  34.  
    "type": "cppbuild",
  35.  
    "label": "C/C : gcc.exe 构建x86活动文件 (Release)",
  36.  
    "command": "C:/mingw32/bin/gcc.exe",
  37.  
    "args": [
  38.  
    "-fdiagnostics-color=always",
  39.  
    "-O1",
  40.  
    "${file}",
  41.  
    "-lstdc ",
  42.  
    "-o",
  43.  
    "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
  44.  
    "-fexec-charset=GB18030"
  45.  
    ],
  46.  
    "options": {
  47.  
    "cwd": "C:/mingw32/bin"
  48.  
    },
  49.  
    "problemMatcher": "$gcc",
  50.  
    "group": {
  51.  
    "kind": "build",
  52.  
    "isDefault": false
  53.  
    },
  54.  
    "detail": "编译器: C:/mingw32/bin/gcc.exe",
  55.  
    "presentation": {
  56.  
    "echo": true,
  57.  
    "reveal": "always",
  58.  
    "focus": false,
  59.  
    "panel": "new",
  60.  
    }
  61.  
    },
  62.  
    {
  63.  
    "type": "cppbuild",
  64.  
    "label": "C/C : gcc.exe 构建x64活动文件 (Debug)",
  65.  
    "command": "C:/mingw64/bin/gcc.exe",
  66.  
    "args": [
  67.  
    "-fdiagnostics-color=always",
  68.  
    "-g",
  69.  
    "${file}",
  70.  
    "-lstdc ",
  71.  
    "-o",
  72.  
    "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
  73.  
    "-fexec-charset=GB18030"
  74.  
    ],
  75.  
    "options": {
  76.  
    "cwd": "C:/mingw64/bin"
  77.  
    },
  78.  
    "problemMatcher": "$gcc",
  79.  
    "group": {
  80.  
    "kind": "build",
  81.  
    "isDefault": false
  82.  
    },
  83.  
    "detail": "编译器: C:/mingw64/bin/gcc.exe",
  84.  
    "presentation": {
  85.  
    "echo": true,
  86.  
    "reveal": "never",
  87.  
    "focus": false,
  88.  
    "panel": "new"
  89.  
    }
  90.  
    },
  91.  
    {
  92.  
    "type": "cppbuild",
  93.  
    "label": "C/C : gcc.exe 构建x64活动文件 (Release)",
  94.  
    "command": "C:/mingw64/bin/gcc.exe",
  95.  
    "args": [
  96.  
    "-fdiagnostics-color=always",
  97.  
    "-O1",
  98.  
    "${file}",
  99.  
    "-lstdc ",
  100.  
    "-o",
  101.  
    "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
  102.  
    "-fexec-charset=GB18030"
  103.  
    ],
  104.  
    "options": {
  105.  
    "cwd": "C:/mingw64/bin"
  106.  
    },
  107.  
    "problemMatcher": "$gcc",
  108.  
    "group": {
  109.  
    "kind": "build",
  110.  
    "isDefault": false
  111.  
    },
  112.  
    "detail": "编译器: C:/mingw64/bin/gcc.exe",
  113.  
    "presentation": {
  114.  
    "echo": true,
  115.  
    "reveal": "never",
  116.  
    "focus": false,
  117.  
    "panel": "new"
  118.  
    }
  119.  
    },
  120.  
    {
  121.  
    "label": "清理构建文件",
  122.  
    "type": "shell",
  123.  
    "command": "del ${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
  124.  
    "group": {
  125.  
    "kind": "none"
  126.  
    },
  127.  
    "problemMatcher": []
  128.  
    },
  129.  
    {
  130.  
    "label": "运行x86文件",
  131.  
    "type": "shell",
  132.  
    "dependsOn": [
  133.  
    "C/C : gcc.exe 构建x86活动文件 (Release)"
  134.  
    ],
  135.  
    "command": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
  136.  
    "group": {
  137.  
    "kind": "test",
  138.  
    "isDefault": false
  139.  
    },
  140.  
    "presentation": {
  141.  
    "echo": true,
  142.  
    "reveal": "always",
  143.  
    "focus": true,
  144.  
    "panel": "new"
  145.  
    }
  146.  
    },
  147.  
    {
  148.  
    "label": "运行x64文件",
  149.  
    "type": "shell",
  150.  
    "dependsOn": [
  151.  
    "C/C : gcc.exe 构建x64活动文件 (Release)"
  152.  
    ],
  153.  
    "command": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
  154.  
    "group": {
  155.  
    "kind": "test",
  156.  
    "isDefault": false
  157.  
    },
  158.  
    "presentation": {
  159.  
    "echo": true,
  160.  
    "reveal": "always",
  161.  
    "focus": true,
  162.  
    "panel": "new"
  163.  
    }
  164.  
    }
  165.  
    ]
  166.  
    }
学新通

launch.json

  1.  
    {
  2.  
    // 使用 IntelliSense 了解相关属性。
  3.  
    // 悬停以查看现有属性的描述。
  4.  
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5.  
    "version": "0.2.0",
  6.  
    "configurations": [
  7.  
    {
  8.  
    "name": "GDB x86 Debug",
  9.  
    "type": "cppdbg",
  10.  
    "request": "launch",
  11.  
    "program": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
  12.  
    "args": [],
  13.  
    "stopAtEntry": false,
  14.  
    "cwd": "${fileDirname}",
  15.  
    "environment": [],
  16.  
    "externalConsole": false,
  17.  
    "MIMode": "gdb",
  18.  
    "miDebuggerPath": "C:\\mingw32\\bin\\gdb.exe",
  19.  
    "setupCommands": [
  20.  
    {
  21.  
    "description": "为 gdb 启用整齐打印",
  22.  
    "text": "-enable-pretty-printing",
  23.  
    "ignoreFailures": true
  24.  
    },
  25.  
    {
  26.  
    "description": "将反汇编风格设置为 Intel",
  27.  
    "text": "-gdb-set disassembly-flavor intel",
  28.  
    "ignoreFailures": true
  29.  
    }
  30.  
    ],
  31.  
    "preLaunchTask": "C/C : gcc.exe 构建x86活动文件 (Debug)",
  32.  
    },
  33.  
    {
  34.  
    "name": "GDB x64 Debug",
  35.  
    "type": "cppdbg",
  36.  
    "request": "launch",
  37.  
    "program": "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe",
  38.  
    "args": [],
  39.  
    "stopAtEntry": false,
  40.  
    "cwd": "${fileDirname}",
  41.  
    "environment": [],
  42.  
    "externalConsole": false,
  43.  
    "MIMode": "gdb",
  44.  
    "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
  45.  
    "setupCommands": [
  46.  
    {
  47.  
    "description": "为 gdb 启用整齐打印",
  48.  
    "text": "-enable-pretty-printing",
  49.  
    "ignoreFailures": true
  50.  
    },
  51.  
    {
  52.  
    "description": "将反汇编风格设置为 Intel",
  53.  
    "text": "-gdb-set disassembly-flavor intel",
  54.  
    "ignoreFailures": true
  55.  
    }
  56.  
    ],
  57.  
    "preLaunchTask": "C/C : gcc.exe 构建x64活动文件 (Debug)"
  58.  
    }
  59.  
    ]
  60.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggihhc
系列文章
更多 icon
同类精品
更多 icon
继续加载