跳到主要内容

配置VSCode,打造舒适的C/C++学习环境

· 阅读需 5 分钟

好久没写过博客了,有很多想写的,又感觉自己写不好 😢.

Winlibs.com 中下载最新构建的 MinGW-w64。

一般选择 GCC _ + LLVM/Clang/LLD/LLDB _ + MinGW-w64 _ (UCRT) - release _ ,win64 即可。

Winlibs

关于 UCRT 与 MSVCRT 的区别请看 MSYS2 的官网说明:

These are two variants of the C standard library on Microsoft Windows.

MSVCRT (Microsoft Visual C++ Runtime) is available by default on all Microsoft Windows versions, but due to backwards compatibility issues is stuck in the past, not C99 compatible and is missing some features.

  • It isn't C99 compatible, for example the printf() function family, but...
  • mingw-w64 provides replacement functions to make things C99 compatible in many cases
  • It doesn't support the UTF-8 locale
  • Binaries linked with MSVCRT should not be mixed with UCRT ones because the internal structures and data types are different. (More strictly, object files or static libraries built for different targets shouldn't be mixed. DLLs built for different CRTs can be mixed as long as they don't share CRT objects, e.g. FILE*, across DLL boundaries.) Same rule is applied for MSVC compiled binaries because MSVC uses UCRT by default (if not changed).
  • Works out of the box on every Microsoft Windows versions.

UCRT (Universal C Runtime) is a newer version which is also used by Microsoft Visual Studio by default. It should work and behave as if the code was compiled with MSVC.

  • Better compatibility with MSVC, both at build time and at run time.
  • It only ships by default on Windows 10 and for older versions you have to provide it yourself or depend on the user having it installed.

解压到某个位置,例如 C:\mingw64 。

然后将 C:\mingw64\bin 添加到 PATH 环境变量中。 可以按 win+r 输入 rundll32.exe sysdm.cpl,EditEnvironmentVariables 打开环境变量设置,也可以手动打开:设置-系统信息-高级系统设置-环境变量。

一般添加到用户环境变量的 PATH 中即可。

打开终端输入 gcc -v 有输出即成功。

新建一个文件夹,例如 C:\Studio\Code 。使用 VS Code 打开此文件夹。

点击左侧 “拓展” ,安装 CodeLLDBClangd.

CodeLLDB 可能安装失败,需要挂代理,或者从 GitHub 下载手动安装。

新建一个 .vscode 文件夹,里面新建 tasks.jsonlaunch.json 这两个文件相当于当前工作区的配置文件。 新建一个 .build 文件夹,里面会存放我们编译完成的程序。 这里我给一个示例:

{
"version": "2.0.0",
"tasks": [
{
"type": "process",
"label": "build", //任务名称
"command": "clang++", //程序名称
"args": [
//arguments 参数
"${file}",
"-o",
"${workspaceFolder}\\.build\\${fileBasenameNoExtension}.exe",
//添加调试信息
"-glldb",
//设置c++版本为17
"-std=c++17",
//启用彩色警告
"-fdiagnostics-color=always",
// 启用诊断
"-pedantic",
"-Wall",
"-Wextra"
],
"options": {
"cwd": "${fileDirname}" //程序工作目录
},
"presentation": {
// 执行任务时是否跳转到终端面板,可以为always,silent,never。具体参见VSC的文档,即使设为never,手动点进去还是可以看到
"reveal": "never",
// 不同的文件的编译信息共享一个终端面板
"panel": "shared"
},
"group": {
"kind": "build",
"isDefault": true //设置为默认生成任务
},
"detail": "编译C/C++程序"
}
]
}
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb", //有黄色波浪线说明没有安装codelldb插件
"request": "launch",
"name": "C/C++单文件调试", //名称
"program": "${workspaceFolder}\\.build\\${fileBasenameNoExtension}.exe", //程序路径
"args": [], //启动参数
"cwd": "${fileDirname}", //程序工作目录
//设置成为neverOpen,这样在第一次运行的时候不会跳转到调试控制台
"internalConsoleOptions": "neverOpen",
"preLaunchTask": "build" //需要提前执行的任务
}
]
}

新建一个 cpp 文件:

#include<iostream>

int main() {
std::cout<<"Hello World!"<<std::endl;
return 0;
}

F5 ,输出 Hello World! 即成功。