VSCode + Mingw64 搭建 c++开发环境

20 阅读1分钟

项目的结构如下图 image.png

.vscode
   - ## c_cpp_properties.json
   - launch.json
   - settings.json
   - tasks.json
main.cpp

c_cpp_properties.json

{
  "configurations": [
    {
      "name": "windows-gcc-x64",
      "includePath": [
        "${workspaceFolder}/**"
      ],
      "compilerPath": "D:/mingw/MinGW/bin/g++.exe",
      "cStandard": "${default}",
      "cppStandard": "${default}",
      "intelliSenseMode": "gcc-x64",
      "compilerArgs": [
        ""
      ]
    }
  ],
  "version": 4
}

launch.json

{
  "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": "D:/mingw/MinGW/bin/gdb.exe",
      "preLaunchTask": "g++",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

settings.json

{
  "C_Cpp_Runner.cCompilerPath": "gcc",
  "C_Cpp_Runner.cppCompilerPath": "g++",
  "C_Cpp_Runner.debuggerPath": "gdb",
  "C_Cpp_Runner.cStandard": "",
  "C_Cpp_Runner.cppStandard": "",
  "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat",
  "C_Cpp_Runner.useMsvc": false,
  "C_Cpp_Runner.warnings": [
    "-Wall",
    "-Wextra",
    "-Wpedantic",
    "-Wshadow",
    "-Wformat=2",
    "-Wcast-align",
    "-Wconversion",
    "-Wsign-conversion",
    "-Wnull-dereference"
  ],
  "C_Cpp_Runner.msvcWarnings": [
    "/W4",
    "/permissive-",
    "/w14242",
    "/w14287",
    "/w14296",
    "/w14311",
    "/w14826",
    "/w44062",
    "/w44242",
    "/w14905",
    "/w14906",
    "/w14263",
    "/w44265",
    "/w14928"
  ],
  "C_Cpp_Runner.enableWarnings": true,
  "C_Cpp_Runner.warningsAsError": false,
  "C_Cpp_Runner.compilerArgs": [],
  "C_Cpp_Runner.linkerArgs": [],
  "C_Cpp_Runner.includePaths": [],
  "C_Cpp_Runner.includeSearch": [
    "*",
    "**/*"
  ],
  "C_Cpp_Runner.excludeSearch": [
    "**/build",
    "**/build/**",
    "**/.*",
    "**/.*/**",
    "**/.vscode",
    "**/.vscode/**"
  ],
  "C_Cpp_Runner.useAddressSanitizer": false,
  "C_Cpp_Runner.useUndefinedSanitizer": false,
  "C_Cpp_Runner.useLeakSanitizer": false,
  "C_Cpp_Runner.showCompilationTime": false,
  "C_Cpp_Runner.useLinkTimeOptimization": false,
  "C_Cpp_Runner.msvcSecureNoWarnings": false
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "g++",
        "command": "g++",
        "args": [
          "-g",
          "${file}",
          "-fexec-charset=GBK",
          "-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": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

main.cpp

// https://blog.csdn.net/weixin_74027669/article/details/138260707
#include <stdio.h>
#include <stdint.h>


uint8_t crc8(const unsigned char *data, size_t len) {
    const unsigned char *p = data;
    uint8_t crc = 0x00;

    while (len--) {
        crc ^= *p++;
        for (int i = 0; i < 8; i++) {
            if (crc & 0x80)
                crc = (crc << 1) ^ 0x07; // CRC-8 polynomial: 0x07
            else
                crc <<= 1;
        }
    }
    return crc;
}

void printHexArray(char* tag, const unsigned char *array, size_t length) {
    printf("%s: ", tag);
    for (size_t i = 0; i < length; ++i) {
        // 使用 %02X 打印每个字节为两位的 16 进制数
        printf("%02X ", array[i]);
    }
    printf("\n");
}

int main()
{
    unsigned char data[] = {0x55,0xaa,0x03,0x02,0x03};
    size_t length = sizeof(data) / sizeof(data[0]);
    printHexArray("字节数组", data, length);

    uint8_t checksum = crc8(data, length);

    unsigned char checksum_list[] = {checksum};
    printHexArray("校验和", checksum_list, 1);
    getchar();
    return 0;
}

运行结果

字节数组: 55 AA 03 02 03
校验和: EA