VS Code中C/CPP的完美配置(完成环境搭建、解决终端自动闪退、解决无法调试)

一、环境搭建

  • 下载VS CODE,MinGW

    VS CODE: https://code.visualstudio.com/

    MINGW:链接:https://pan.baidu.com/s/1Fw-7-_q4waHgy_UmRKWSqQ 提取码:02i9

  • 安装VS Code、部署MinGW

    1. 这里安装VS Code不再赘述,安装到任意目录即可(但不要存在中文!!!)

    2. 解压MinGW到任意目录(但不要存在中文!!!)

    3. 配置MinGW环境变量

      将MinGW的bin目录配置到Path

      此电脑—>属性—>高级系统设置—>环境变量—>编辑Path

      添加{你的盘符(目录)}\mingw64\bin (如图)

      image

      并检查VS Code路径是否存在

  • 安装C/CPP插件

    1. 打开VS Code,在插件市场中搜索C/C++并点击install安装(如图)
    image
    1. 重启VS Code
  • 编辑配置文件

    1. 创建一个工作目录并在VS Code中打开

      文件—>打开文件夹(如图)

    image
    1. 创建一个.cpp文件(如图)
    image-20200319161047123.png
    1. 进行调试(快捷键F5)

      选择C++(GDB/LLDB)—>g++.exe build and debug active file

    2. 编辑自动创建的launch.json文件

      1.修改"externalConsole"为true

      2.修改"preLaunchTask"为 "c/cpp task"

      这里给出完整配置文件可对照修改

         {
          // 使用 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": "D:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
          {
            "description": "为 gdb 启用整齐打印",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
          ],
            "preLaunchTask": "c/cpp task"
          }
          ]
         }
  1. 进行调试(快捷键F5)—>配置任务—>g++.exe build and debug active file

    image
  • 编辑弹出的task.json

    修改"label"为 "c/cpp task"

    这里给出完整配置文件可对照修改

     {
     // 有关 tasks.json 格式的文档,请参见
      // https://go.microsoft.com/fwlink/?LinkId=733558
      "version": "2.0.0",
      "tasks": [
      {
        "type": "shell",
        "label": "c/cpp task",
        "command": "D:\\mingw64\\bin\\g++.exe",
        "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
      "cwd": "D:\\mingw64\\bin"
      },
      "problemMatcher": [
      "$gcc"
      ],
      "group": "build"
      }
      ]
     }
  • 至此环境搭建完成

二、解决终端自动闪退

  • 打开launch.json文件

    1. 修改"program"为 "C:\Windows\system32\cmd.exe"

    2. "args": ["/C","{fileDirname}\{fileBasenameNoExtension}.exe","&","pause"] 此行因markdown语法显示错误,请关注下方
      这里给出完整配置文件可对照修改

         {
          // 使用 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": "C:\\Windows\\system32\\cmd.exe",
            "args": ["/C","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
          {
            "description": "为 gdb 启用整齐打印",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
          ],
            "preLaunchTask": "LaunchTask g++"
          }
          ]
         
         }
  • 若你配置正确,现在终端将在程序执行完毕后暂停(即不会闪退)

三、解决无法调试

  • 配置launch.json

    这里直接给出配置文件,不做赘述,直接按照下文配置即可

 {
      // 使用 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": "C:\\Windows\\system32\\cmd.exe",
        "args": ["/C","${fileDirname}\\${fileBasenameNoExtension}.exe","&","pause"],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "gdb",
        "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe",
        "setupCommands": [
      {
        "description": "为 gdb 启用整齐打印",
        "text": "-enable-pretty-printing",
        "ignoreFailures": true
      }
      ],
      "preLaunchTask": "c/cpp task"
      },

      {
        "name": "debug",
        "type": "cppdbg",
       "request": "launch",
        "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "gdb",
        "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe",
        "setupCommands": [
      {
        "description": "Enable pretty-printing for gdb",
        "text": "-enable-pretty-printing",
        "ignoreFailures": false
      }
      ],
        "preLaunchTask": "c/cpp task"
      }
      ]
     }
  • 配置tesk.json

    这里直接给出配置文件,不做赘述,直接按照下文配置即可

    {
     // 有关 tasks.json 格式的文档,请参见
      // https://go.microsoft.com/fwlink/?LinkId=733558
      "version": "2.0.0",
      "tasks": [
      {
        "type": "shell",
        "label": "c/cpp task",
        "command": "D:\\mingw64\\bin\\g++.exe",
        "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
        "options": {
        "cwd": "D:\\mingw64\\bin"
      },
        "problemMatcher": [
        "$gcc"
      ],
        "group": "build"
      },

      {
        "type": "shell",
        "label": "c/cpp task",
        "command": "D:\\mingw64\\bin\\g++.exe",
        "args": [
        "-o",
        "${fileBasenameNoExtension}",
        "${file}"
      ],
        "group": {
        "kind": "build",
        "isDefault": true
      }
      }
      ]
     }
  • 在运行和调试中选择debug(很重要!千万不要忽略。)


    image
  • 至此,无法调试问题解决

感谢你的阅读,谢谢~
如果你觉得以上描述不够清晰,你也可以选择浏览视频
bilibili:https://space.bilibili.com/150255283

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容