当前位置:   article > 正文

Windows配置VScode的C++环境

Windows配置VScode的C++环境

1.下载VScode

https://code.visualstudio.com/Download
在这里插入图片描述

2.安装扩展

在这里插入图片描述

3.下载MinGW

下载地址https://sourceforge.net/projects/mingw-w64/files/,下拉选择x86_64-posix-seh
在这里插入图片描述
下载下来后是一个压缩包,直接解压缩,并记住解压缩后文件的所在位置,我这里是D:\mingw64
在这里插入图片描述

4.配置环境变量

开始菜单里面搜索“编辑系统环境变量”
在这里插入图片描述
环境变量——系统变量——双击path
在这里插入图片描述
新建变量,输入mingw64文件下面bin文件的路径,我这里路径为D:\mingw64\bin,然后点击确定。

随后按win+R,输入cmd,回车,输入g++,回车,出现以下信息表示配置成功
在这里插入图片描述
随后新建一个文件夹用以存放代码
在这里插入图片描述

5.启动VScode

打开文件夹——选择code_file——新建一个文件helloworld.cpp,并输入以下代码,F5运行
在这里插入图片描述

#include <stdio.h>
#include <windows.h>
int main()
{
    printf("Hello World\n");
    system("pause");
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

6.点击提示框的打开"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": true,      //修改此项,让其弹出终端
             "MIMode": "gdb",
             "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe",
             "setupCommands": [
                 {
                     "description": "Enable pretty-printing for gdb",
                     "text": "-enable-pretty-printing",
                     "ignoreFailures": true
                 }
             ],
             "preLaunchTask": "task g++" //修改此项
         }
     ]
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

随后F5运行

7.点击提示框中的"配置任务"

在这里插入图片描述
使用模板创建——Others——创建一个tasks.json,并用以下代码替换内容

{
     "version": "2.0.0",
     "tasks": [
         {
             "type": "shell",
             "label": "task g++",   
             "command": "D:\\mingw64\\bin\\g++.exe",
             "args": [
                 "-g",
                 "${file}",
                 "-o",
                 "${fileDirname}\\${fileBasenameNoExtension}.exe"
             ],
             "options": {
                 "cwd": "D:\\mingw64\\bin"
             },
             "problemMatcher": [
                 "$gcc"
             ],
             "group": "build"
         }
     ]
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

8.返回helloworld.cpp,F5运行

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/430589
推荐阅读
相关标签
  

闽ICP备14008679号