输入关键词开始搜索

VS Code 开发环境配置

必装插件

插件用途ID
C/C++语言支持 + IntelliSensems-vscode.cpptools
CMake ToolsCMake 集成ms-vscode.cmake-tools
CMake语法高亮twxs.cmake
clangd替代 C++ 插件(更快)llvm-vs-code-extensions.vscode-clangd
Qt toolsQt ui/qrc 支持tonka3000.qtvsctools
GitLensGit 超级增强eamodio.gitlens
Error Lens行内显示错误usernamehw.errorlens
Even Better TOML.toml 支持tamasfe.even-better-toml

settings.json 模板

{
  // ── 编辑器 ──
  "editor.fontSize": 14,
  "editor.fontFamily": "'Cascadia Code', 'JetBrains Mono', Consolas, monospace",
  "editor.fontLigatures": true,
  "editor.tabSize": 4,
  "editor.insertSpaces": true,
  "editor.rulers": [100],
  "editor.renderWhitespace": "boundary",
  "editor.minimap.enabled": false,
  "editor.bracketPairColorization.enabled": true,

  // ── 文件 ──
  "files.autoSave": "onFocusChange",
  "files.exclude": {
    "**/build": true,
    "**/.git": true
  },

  // ── C++ ──
  "[cpp]": {
    "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd"
  },
  "C_Cpp.intelliSenseEngine": "disabled",  // 用 clangd 时关掉
  "clangd.arguments": [
    "--background-index",
    "--compile-commands-dir=build",
    "-j=4"
  ],

  // ── CMake ──
  "cmake.configureOnOpen": false,
  "cmake.buildDirectory": "${workspaceFolder}/build",
  "cmake.generator": "Ninja",

  // ── 终端 ──
  "terminal.integrated.fontSize": 13,
  "terminal.integrated.defaultProfile.windows": "PowerShell"
}

tasks.json — 构建任务

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "cmake configure",
      "type": "shell",
      "command": "cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug",
      "problemMatcher": []
    },
    {
      "label": "cmake build",
      "type": "shell",
      "command": "cmake --build build -j8",
      "group": { "kind": "build", "isDefault": true },
      "dependsOn": ["cmake configure"]
    },
    {
      "label": "run",
      "type": "shell",
      "command": "${workspaceFolder}/build/myapp",
      "dependsOn": ["cmake build"]
    }
  ]
}

launch.json — 调试配置

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug MyApp",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/build/myapp",
      "args": [],
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "cmake build"
    }
  ]
}

快捷键

操作快捷键
命令面板Ctrl+Shift+P
快速打开文件Ctrl+P
全局搜索Ctrl+Shift+F
跳转到定义F12
查看引用Shift+F12
重命名符号F2
格式化文档Shift+Alt+F
切换终端`Ctrl+“
多光标Alt+Click
列选择Shift+Alt+拖拽
折叠/展开代码块Ctrl+Shift+[ / ]

clangd 替代 C++ 插件(推荐)

# 安装 clangd
sudo apt install clangd-17

# VS Code 设置:
# 1. 禁用 C_Cpp IntelliSense
# 2. 安装 clangd 插件
# 3. 确认 build/ 下有 compile_commands.json
#    (CMake: set(CMAKE_EXPORT_COMPILE_COMMANDS ON))

# clangd 优势:
# - 索引速度比 C++ 插件快 3-5 倍
# - InlayHints(自动显示参数名/类型推导结果)
# - 更准确的补全和诊断

设置同步

# 方式 1: VS Code 内置(需要 GitHub/MS 账号)
# Settings Sync → 登录 → 自动同步

# 方式 2: 手动备份
cp ~/.config/Code/User/settings.json ~/dotfiles/
cp ~/.config/Code/User/keybindings.json ~/dotfiles/

# 方式 3: 用 Git 管理 dotfiles
git init ~/dotfiles
# 把 settings.json 软链到 ~/dotfiles/vscode/

Qt 项目配置

// .vscode/c_cpp_properties.json(用 C++ 插件时)
{
  "configurations": [{
    "name": "Qt",
    "includePath": [
      "${workspaceFolder}/**",
      "/usr/include/x86_64-linux-gnu/qt6",
      "/usr/include/x86_64-linux-gnu/qt6/QtCore",
      "/usr/include/x86_64-linux-gnu/qt6/QtWidgets"
    ],
    "defines": [],
    "compilerPath": "/usr/bin/g++",
    "cStandard": "c17",
    "cppStandard": "c++17",
    "compileCommands": "${workspaceFolder}/build/compile_commands.json"
  }]
}
// ⚠️ 用 clangd 时不需此文件 — clangd 直接读 compile_commands.json