vscode搭建C/C++环境

文章目录

一、安装vscode

二、下载安装g++

三、安装VSCode插件

四、配置运行环境

一、安装vscode

直接官网免费下载:下载完成后进行安装,记得更换安装路径Visual Studio Code - Code Editing. RedefinedVisual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.https://code.visualstudio.com/

二、下载安装g++

1. 点击连接下载64位,下载载完成后进行安装,要记住安装路径(不要出现中文),一会还要用

MinGW Distro - nuwen.net

2. 找到复制刚才安装的路径

3. 接着打开编辑环境变量

4. 接着单击Path,再点击编辑

5. 点击右侧新建,然后将刚刚复制的路径添加上去。接着一直点击确定,直到退出。

6. 打开cmd验证是否安装成功,输入:g++ --version

7. 出现这个就代表安装成功了。

三、安装VSCode插件

1. 打开vscode,点击左侧的扩展按钮

2. 搜索C,安装第一个插件

3. 接着搜索安装Code Runner

4. 如果想更换中文,可以再搜索chinese

四、配置运行环境

1. 首先建一个文件夹,文件名不可以用中文,这个文件夹就是以后存放并运行调试C/C++代码的地方,只要放在这个文件夹里就可以正常调试。

2. 打开vscode,点击文件(file),再点击打开文件夹(Open Folder),然后打开刚刚创建的文件夹。

3. 接着点击文件名旁边的新建文件夹,文件夹命名为.vscode(不能修改)

4. 接着在这个文件夹中创建四个文件

//c_cpp_properties.json
//launch.json
//settings.json
//tasks.json

5. 然后在每个文件夹中输入以下内容。

5.1 c_cpp_properties.json

注意compilerPath这一项要把路径改成安装g++.exe的安装路径,和刚刚配置path时的地址类似,找到安装路径中的bin文件,然后打开选择g++.exe。

C:/Program Files/mingw64/bin/g++.exe

{
    "configurations": [
      {
        "name": "Win64",
        "includePath": ["${workspaceFolder}/**"],
        "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
        "windowsSdkVersion": "10.0.18362.0",
        "compilerPath": "C:/Program Files/mingw64/bin/g++.exe",
        "cStandard": "c17",
        "cppStandard": "c++17",
        "intelliSenseMode": "gcc-x64"
      }
    ],
    "version": 4
  }

5.2 launch.json

注意miDebuggerPath这一项也要把路径改成安装gdb.exe的安装路径,和刚刚地址一样,只是后面选择的文件不一样。

C:\\Program Files\\mingw64\\bin\\gdb.exe

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "(gdb) Launch", 
        "type": "cppdbg", 
        "request": "launch", 
        "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", 
        "args": [], 
        "stopAtEntry": false,
        "cwd": "${workspaceRoot}",
        "environment": [],
        "externalConsole": true, 
        "MIMode": "gdb",
        "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",
        "preLaunchTask": "g++",
        "setupCommands": [
          {
            "description": "Enable pretty-printing for gdb",
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
          }
        ]
      }
    ]
  }

5.3 settings.json

这里没有要修改的

{
    "files.associations": {
      "*.py": "python",
      "iostream": "cpp",
      "*.tcc": "cpp",
      "string": "cpp",
      "unordered_map": "cpp",
      "vector": "cpp",
      "ostream": "cpp",
      "new": "cpp",
      "typeinfo": "cpp",
      "deque": "cpp",
      "initializer_list": "cpp",
      "iosfwd": "cpp",
      "fstream": "cpp",
      "sstream": "cpp",
      "map": "c",
      "stdio.h": "c",
      "algorithm": "cpp",
      "atomic": "cpp",
      "bit": "cpp",
      "cctype": "cpp",
      "clocale": "cpp",
      "cmath": "cpp",
      "compare": "cpp",
      "concepts": "cpp",
      "cstddef": "cpp",
      "cstdint": "cpp",
      "cstdio": "cpp",
      "cstdlib": "cpp",
      "cstring": "cpp",
      "ctime": "cpp",
      "cwchar": "cpp",
      "exception": "cpp",
      "ios": "cpp",
      "istream": "cpp",
      "iterator": "cpp",
      "limits": "cpp",
      "memory": "cpp",
      "random": "cpp",
      "set": "cpp",
      "stack": "cpp",
      "stdexcept": "cpp",
      "streambuf": "cpp",
      "system_error": "cpp",
      "tuple": "cpp",
      "type_traits": "cpp",
      "utility": "cpp",
      "xfacet": "cpp",
      "xiosbase": "cpp",
      "xlocale": "cpp",
      "xlocinfo": "cpp",
      "xlocnum": "cpp",
      "xmemory": "cpp",
      "xstddef": "cpp",
      "xstring": "cpp",
      "xtr1common": "cpp",
      "xtree": "cpp",
      "xutility": "cpp",
      "stdlib.h": "c",
      "string.h": "c"
    },
    "editor.suggest.snippetsPreventQuickSuggestions": false,
    "aiXcoder.showTrayIcon": true
  }

5.4 tasks.json

这里也没有要修改的。

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "g++",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            },
            "group": "build"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",
            "command": "D:\\Qt\\Tools\\mingw1310_64\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ]
}

6. 保存以后就完成了所有配置,以后所有的C/C++文件都要放到这个文件夹中运行调试。

7. 想要运行调试代码点击右上角的三角就可以了。

上一篇:Qt 创建控件的两种方式


下一篇:即插即用模块详解SCConv:用于特征冗余的空间和通道重构卷积