本文主要介绍了Windows平台下VSCode C语言环境的配置教程。

我将介绍两种编译链的配置方式。

MINGW(Minimalist GNU for Windows)

这种方式网上教程比较普遍,我所了解的最简单的方式是下载谷雨同学VS Code Config Helper,按照软件步骤下载MINGW,然后生成.vscode配置即可食用。

CLANG+LLVM

LLVM(Low Level Virtual Machine)被认为是比GCC设计更好的一个编译系统,CLANG是LLVM的前端(编译原理中的概念)。网上的普遍评价是CLANG的性能比GCC更好,在大型项目中明显表现更优,也有人说近年来GCC不断改进性能跟CLANG已经相差不大了,对我而言,使用CLANG的体验是获得的代码提示会更好。通过官网下载的CLANG在Windows中会缺少依赖库,因为CLANG的目标平台并不是Windows。网上的其他教程会推荐下载MYSY2将CLANG目录移动到MYSY2目录,这样就补足了CLANG的依赖库。

我个人是通过Scoop安装的,Scoop的安装请查阅官网或者其他资料。

  1. 先添加extra

    1
    scoop bucket add extra
  2. 搜索llvm相关库

    1
    scoop search llvm

    image-20240318195811872

  3. 下载包含Windows依赖项的llvm,我这里是mingw-mstorsjo-llvm-ucrt

    1
    scoop install mingw-mstorsjo-llvm-ucrt
  4. 重启电脑,llvm相关的环境变量已经配置好,这也是推荐使用scoop的原因。

    image-20240318200033944

  5. 配置VSCode。首先,需要首先禁用或者卸载微软官方的C/C++插件。然后,安装Clangd插件和CodeLLDB插件。

  6. 最后是按照VSCode的方式书写配置,VSCode的配置在项目目录下的.vscode文件夹中。在.vscode文件夹中创建launch.jsontask.json文件,同时创建build文件夹(主要是为了放置编译生成的二进制文件,也可以选择不创建,并且删除下方build字段)书写如下代码。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    # 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": [
    {
    "type": "lldb",
    "request": "launch",
    "name": "Debug",
    "program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
    "args": [],
    "cwd": "${workspaceFolder}",
    "preLaunchTask": "build",
    }
    ]
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # task.json
    {
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
    {
    "label": "build",
    "type": "shell",
    "command": "clang",
    "args": [
    "-g",
    "-o",
    "${workspaceFolder}/build/${fileBasenameNoExtension}",
    "${file}",
    ]
    }
    ]
    }
  7. 至此,所有配置项已完成。如有其他问题,请留言评论区。