Visual Studio Code配置C/C++环境(win10)


Ⅰ. 安装编辑器Vscode

vscode 选择适合自己系统的版本。

Ⅱ. 安装编译器mingw64

windows 下安装 mingw64 编译器有点麻烦,不过,我都准备好了,直接下载直接使用就好了。

链接: https://pan.baidu.com/s/1-MhgLjwG0xHtFtyNy-M_Fg

提取码: 3ia5

添加环境变量

如图,点击新建,把 mingw64bin 文件夹添加到 path 确定退出。

查看环境变量是否添加成功

win + r 输入 cmd

再输入 g++ -v

如果出现这个就是成功了。

Ⅲ. 给vscode添加插件

这步的主要目的是方便代码运行和提高编写代码效率~

快捷键 Ctrl + Shift + X

或者鼠标点击下图红色框框

上图是笔者在 C/C++ 环境下使用的插件,具体作用可以百度,也可以查看官方文档。

Ⅳ. 配置vscode调试环境

完成了前三步,跑代码是没问题的了,但是需要 debug 的同学看过来🙋‍

为了使 vscode 能够顺利进行 debug 我们还需要添加一些配置信息。

创建开发工作区:

创建一个代码工作区。

工作区所在路径仅由字母、数字、下划线组成,不要包含其他的符号;

绝对不能有中文,中文路径调试时会报错的。

点击 vscode 左上角的 file 打开文件夹。

添加配置信息

在工作区的文件夹建立一个 .vscode 文件夹。

.vscode 文件夹包含如下内容:

特别注意:下面所有出现 mingw64 的地方都要改成你电脑 mingw64 的目录。

c_cpp_properties.json 内容:

{
  "configurations": [
    {
      "name": "Win64",
      "includePath": [
        "${workspaceFolder}/**"
      ],
      "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
      ],
      "compilerPath": "E:\\mingw64\\bin\\gcc.exe",
      "cStandard": "c11",
      "cppStandard": "gnu++20",
      "intelliSenseMode": "gcc-x64"
    }
  ],
  "version": 4
}

launch.json 内容:

{
  "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": false,
      "MIMode": "gdb",
      "miDebuggerPath": "E:\\mingw64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "g++.exe build active file"
    }
  ]
}

tasks.json 内容:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++.exe build active file",
      "command": "E:\\mingw64\\bin\\g++.exe",
      "args": [
        "-g",
        "-Wall",
        "-fexec-charset=GBK",
        "-std=c++17",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "E:\\mingw64\\bin"
      },
      "problemMatcher": [
        "$gcc"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

参考:GCC on Windows

Ⅴ. 编写第一个程序:

#include <stdio.h>

int main() {
  printf("Helo World\n");
  return 0;
}

点击右边的三角形符号,可以运行代码,或者直接用快捷键 ctrl + alt + n

设置断点,点击工具栏运行,进行调试即可。

再次强调一遍,上面的三个配置文件出现了 mingw64 的路径,都要改成你电脑实际的路径。

Ⅵ. 进阶

我们可以通过对 vscode 进行全局配置,实现我们想要的配置,也可以进行局部配置,但推荐全局配置。

File->preferences->settings 快捷键 ctrl +,

点击这个图标就打开了 vscode 的全局配置区域。

笔者配置如下:

{
  "code-runner.executorMap": {
    "c": "cd $dir && gcc $fileName -Wall -fexec-charset=GBK -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "cpp": "cd $dir && g++ $fileName -Wall -std=c++2a -fexec-charset=GBK -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "cc": "cd $dir && g++ $fileName -Wall -std=c++2a -fexec-charset=GBK -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
  },
  "editor.cursorStyle": "line",
  "editor.mouseWheelZoom": true,
  "editor.formatOnSave": true,
  "files.autoSave": "onFocusChange",
  "editor.fontFamily": "Consolas",
  "editor.fontLigatures": true,
  "editor.fontSize": 15,
  "editor.lineHeight": 19,
  "editor.lineNumbers": "on",
  "editor.minimap.enabled": true,
  "editor.renderIndentGuides": false,
  "editor.rulers": [
    120
  ],
  "editor.fontWeight": "normal",
  "terminal.integrated.fontFamily": "Consolas",
  "editor.formatOnPaste": false,
  "editor.suggestFontSize": 0,
  "editor.wordWrap": "on",
  "editor.tabSize": 2,
  "code-runner.runInTerminal": true,
  "workbench.colorCustomizations": {
    "editor.selectionBackground": "#aa0000"
  },
  "workbench.iconTheme": "vscode-icons",
  "files.autoGuessEncoding": true,
  "files.autoSaveDelay": 100,
  "task.autoDetect": "off",
  "code-runner.ignoreSelection": true,
  "code-runner.saveFileBeforeRun": true,
  "code-runner.preserveFocus": false,
  "editor.tabCompletion": "on",
  "editor.suggestSelection": "first",
  "C_Cpp.intelliSenseEngine": "Default",
  "C_Cpp.errorSquiggles": "Enabled",
  "C_Cpp.clang_format_sortIncludes": true,
  "terminal.integrated.rendererType": "dom",
  "C_Cpp.default.cStandard": "c99",
  "C_Cpp.default.intelliSenseMode": "gcc-x64",
  "C_Cpp.clang_format_style": "Google",
  "workbench.sideBar.location": "right",
  "C_Cpp.clang_format_fallbackStyle": "Google",
  "editor.detectIndentation": false,
  "[c]": {
    "editor.defaultFormatter": "ms-vscode.cpptools"
  },
  "[cpp]": {
    "editor.defaultFormatter": "ms-vscode.cpptools"
  },
  "vim.useSystemClipboard": true,
  "explorer.confirmDelete": false,
  "markdown.preview.breaks": true,
  "markdown.preview.fontFamily": "Fira Code",
  "editor.tokenColorCustomizations": {
    "comments": "#82e0aa", // 注释
    "keywords": "#0a0", // 关键字
    "variables": "#f00", // 变量名
    "strings": "#e2d75dbd", // 字符串
    "functions": "#5b99fcc9", // 函数名
    "numbers": "#AE81FF", // 数字
    "textMateRules": [
      {
        "name": "italic font",
        "scope": [
          "comment",
          "keyword",
          "storage",
          "keyword.control",
          "keyword.control.from",
          "keyword.control.flow",
          "keyword.operator.new",
          "keyword.control.import",
          "keyword.control.export",
          "keyword.control.default",
          "keyword.control.trycatch",
          "keyword.control.conditional",
          "storage.type",
          "storage.type.class",
          "storage.modifier.tsx",
          "storage.type.function",
          "storage.modifier.async",
          "variable.language",
          "variable.language.this",
          "variable.language.super",
          "meta.class",
          "meta.var.expr",
          "constant.language.null",
          "support.type.primitive",
          "entity.name.method.js",
          "entity.other.attribute-name",
          "punctuation.definition.comment",
          "text.html.basic entity.other.attribute-name",
          "tag.decorator.js entity.name.tag.js",
          "tag.decorator.js punctuation.definition.tag.js",
          "source.js constant.other.object.key.js string.unquoted.label.js",
        ],
        "settings": {
          "fontStyle": "italic",
        }
      },
    ]
  },
  "workbench.colorTheme": "One Dark Pro",
  "explorer.confirmDragAndDrop": false,
}

具体某一句的功能就不解释了,需要知道的把鼠标放到相应行,就会有提示的。

Ⅶ. 总结

使用 vscode 一年多了,越用越爽,特别是 wsl2 出来后,在 win 上进行 linux 开发还是非常方便的。


文章作者: incipe
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 incipe !
评论
 上一篇
Visual Studio 2019设置自定义背景及特效插件 Visual Studio 2019设置自定义背景及特效插件
1.安装插件:Background(ClaudiaIDE) Color Theme Editor for Visual Studio 2019 路径:【工具】->【扩展和更新】->【联机】 2.设置:【工具】->【选项】-
2020-05-15
下一篇 
keil简单实用 keil简单实用
注:本文适合绝对0基础,如果有一定的基础,可以移步。 一. keil简单介绍Keil 提供了包括C编译器、宏汇编、链接器、库管理和一个功能强大的仿真调试器等在内的完整开发方案,通过一个集成开发环境(μVision)将这些部分组合在一起。 更
2020-05-15
  目录