VSCODE环境搭建

windows下安装gcc

  • 要想在VSCode编译调试C/C++文件,首先需要安装gcc环境。官网自取。当然通常由于网络问题是加载不成功的。
  • 推荐使用离线下载的方式:https://pan.baidu.com/s/1CsucOCkA_dJ53ahAW-Sxdg,提取码:enuw
  • 下载后解压缩,将下载的文件夹bin目录的绝对路径添加到环境变量里,让系统能够找到MinGW
  • 这时候,打开cmd输入gcc -v,应该就能看见版本信息了
    image.png

打开vscode,安装C/C++调试器

编译调试C++工程

  • 依次选择文件——打开文件夹——新建文件,在新建文件中输入内容如下
#include<iostream>
using namespace std;

int main(){
    cout << "hello world" << endl;
    return 0;
}
  • 这里需要一些配置文件
  • c_cpp_properties.json文件配置如下:
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "G:\\MinGW\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}
  • launch.json文件配置如下
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "preLaunchTask": "g++",
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "G:\\MinGW\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  • tasks.json配置如下
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "g++",
            "type": "shell",
            "command": "g++",
            "args": ["-g","${file}","-o","${workspaceFolder}/${fileBasenameNoExtension}.exe"],
            "options": {
                "cwd": "${workspaceFolder}"
            }
        }
    ]
}
  • settings.json配置如下
{
    "files.associations": {
        "iostream": "cpp",
        "vector": "cpp",
        "map": "cpp",
        "list": "cpp",
        "unordered_map": "cpp",
        "deque": "cpp",
        "*.tcc": "cpp",
        "array": "cpp",
        "cwchar": "cpp",
        "exception": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "istream": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "atomic": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwctype": "cpp",
        "algorithm": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "set": "cpp",
        "system_error": "cpp",
        "fstream": "cpp",
        "limits": "cpp",
        "typeinfo": "cpp",
        "random": "cpp",
        "xhash": "cpp",
        "xtree": "cpp",
        "xutility": "cpp"
    }
}
  • 把上述三个文件放在工作目录的.vscode文件夹里
  • 调试
  • 成功!!!

猜你喜欢

转载自www.cnblogs.com/guoyuhuan/p/12585001.html