惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
GbyAI
GbyAI
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
P
Proofpoint News Feed
The Cloudflare Blog
H
Help Net Security
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
量子位
博客园 - 聂微东
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
月光博客
月光博客

博客园 - zdleek

Vibe Coding(氛围编码/状态编程) 常用符号名称与读法 小记VS code调用VC++编译器的环境配置 小记:DS给的一个lamda表达式例子 GPG4win 加解密使用笔记 Windows 11 Version 22H2 (Build 22621) 开发者资源下载 U盘启动多系统工具Ventoy 15种经典机器学习算法[转] 微软原版WIN10PE制作流程 [转]如何彻底关闭win10自动更新 C++模板的哲学 正则表达式简介 C++ 正则表达式使用例子 Lambda 表达式简介 一段bitset的简单例子 在VC++中的使用gRPC编程 Win10下编译gRPC主要步骤 在VC++中使用ProtoBuf进行数据序列化的编程 Win10下编译ProtoBuf主要步骤
VSCode的【Microsoft C/C++ 扩展 】的使用简介[由DS给出]
zdleek · 2025-03-10 · via 博客园 - zdleek

Visual Studio Code 的 **Microsoft C/C++ 扩展** 是一个功能强大的工具,用于在 VS Code 中开发、调试和编译 C/C++ 程序。

以下是关于如何使用该扩展的详细指南。

---

### 1. 安装 Microsoft C/C++ 扩展
1. 打开 VS Code。
2. 点击左侧的扩展图标(或按 `Ctrl+Shift+X`)。
3. 在搜索框中输入 `C/C++`。
4. 找到 **C/C++** 扩展(由 Microsoft 发布),点击安装。

---

### 2. 配置 C/C++ 扩展
安装完成后,C/C++ 扩展会自动检测你的开发环境(如编译器、调试器等),但你可能需要手动配置一些设置。

#### 2.1 配置编译器路径
1. 打开命令面板(`Ctrl+Shift+P`)。
2. 输入 `C/C++: Edit Configurations (UI)`,然后选择。
3. 在打开的界面中,找到 **Compiler path**,输入你的编译器路径。例如:
   - Windows (MSVC): `C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe`
   - Linux (GCC): `/usr/bin/g++`
   - macOS (Clang): `/usr/bin/clang++`

#### 2.2 配置 IntelliSense 模式
1. 在 `C/C++: Edit Configurations (UI)` 界面中,找到 **IntelliSense mode**。
2. 根据你的编译器选择模式:
   - Windows (MSVC): `msvc-x64`
   - Linux (GCC): `gcc-x64`
   - macOS (Clang): `clang-x64`

#### 2.3 配置包含路径
1. 在 `C/C++: Edit Configurations (UI)` 界面中,找到 **Include path**。
2. 添加你的头文件路径。例如:
   - Windows (MSVC): `C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/include`
   - Linux (GCC): `/usr/include`
   - macOS (Clang): `/usr/local/include`

---

### 3. 调试 C/C++ 程序
C/C++ 扩展支持使用 GDB、LLDB 或 MSVC 调试器进行调试。

#### 3.1 配置 `launch.json`
1. 打开你的项目文件夹。
2. 点击左侧的调试图标(或按 `Ctrl+Shift+D`)。
3. 点击“创建一个 launch.json 文件”。
4. 选择 `C++ (GDB/LLDB)` 或 `C++ (Windows)`,具体取决于你的平台。
5. 根据你的需求修改 `launch.json` 文件。例如:

   - **GDB/LLDB 配置**:
     ```json
     {
         "version": "0.2.0",
         "configurations": [
             {
                 "name": "Debug with GDB",
                 "type": "cppdbg",
                 "request": "launch",
                 "program": "${workspaceFolder}/build/${fileBasenameNoExtension}",
                 "args": [],
                 "stopAtEntry": false,
                 "cwd": "${workspaceFolder}",
                 "environment": [],
                 "externalConsole": false,
                 "MIMode": "gdb",
                 "miDebuggerPath": "/usr/bin/gdb",
                 "setupCommands": [
                     {
                         "description": "Enable pretty-printing for gdb",
                         "text": "-enable-pretty-printing",
                         "ignoreFailures": true
                     }
                 ],
                 "preLaunchTask": "build"
             }
         ]
     }
     ```

   - **MSVC 配置**:
     ```json
     {
         "version": "0.2.0",
         "configurations": [
             {
                 "name": "Debug with MSVC",
                 "type": "cppvsdbg",
                 "request": "launch",
                 "program": "${workspaceFolder}/build/${fileBasenameNoExtension}.exe",
                 "args": [],
                 "stopAtEntry": false,
                 "cwd": "${workspaceFolder}",
                 "environment": [],
                 "externalConsole": true,
                 "preLaunchTask": "build"
             }
         ]
     }
     ```

#### 3.2 配置 `tasks.json`(用于编译)
1. 打开命令面板(`Ctrl+Shift+P`)。
2. 输入 `Tasks: Configure Task`,然后选择 `Create tasks.json file from template`。
3. 选择 `Others` 模板。
4. 根据你的编译器修改 `tasks.json` 文件。例如:

   - **GCC 配置**:
     ```json
     {
         "version": "2.0.0",
         "tasks": [
             {
                 "label": "build",
                 "type": "shell",
                 "command": "g++",
                 "args": [
                     "-g",
                     "${file}",
                     "-o",
                     "${workspaceFolder}/build/${fileBasenameNoExtension}"
                 ],
                 "group": {
                     "kind": "build",
                     "isDefault": true
                 },
                 "problemMatcher": [
                     "$gcc"
                 ],
                 "detail": "Generated task by VS Code"
             }
         ]
     }
     ```

   - **MSVC 配置**:
     ```json
     {
         "version": "2.0.0",
         "tasks": [
             {
                 "label": "build",
                 "type": "shell",
                 "command": "cl",
                 "args": [
                     "/EHsc",
                     "/Fe:build/${fileBasenameNoExtension}.exe",
                     "${file}"
                 ],
                 "group": {
                     "kind": "build",
                     "isDefault": true
                 },
                 "problemMatcher": [
                     "$msCompile"
                 ],
                 "detail": "Generated task by VS Code"
             }
         ]
     }
     ```

---

### 4. 使用 IntelliSense
- C/C++ 扩展提供了强大的 IntelliSense 功能,包括代码补全、参数提示、错误检查等。
- 如果 IntelliSense 无法正常工作,检查以下设置:
  1. 确保 `c_cpp_properties.json` 中的 `compilerPath` 和 `includePath` 正确。
  2. 确保你的代码文件已保存(未保存的文件可能会导致 IntelliSense 失效)。

---

### 5. 常见问题

#### 问题 1:IntelliSense 无法正常工作
- 确保 `c_cpp_properties.json` 配置正确。
- 尝试重新加载 VS Code(`Ctrl+Shift+P`,输入 `Reload Window`)。

#### 问题 2:调试器无法启动
- 确保 `launch.json` 中的 `program` 路径正确。
- 确保 `preLaunchTask` 中的编译任务成功完成。

#### 问题 3:编译失败
- 检查 `tasks.json` 中的编译命令是否正确。
- 确保编译器已正确安装并配置到系统环境变量中。

---

### 6. 参考文档
- [C/C++ 扩展官方文档](https://code.visualstudio.com/docs/cpp)
- [VS Code 调试指南](https://code.visualstudio.com/docs/editor/debugging)
- [MSVC 工具链文档](https://docs.microsoft.com/en-us/cpp/build/reference/compiler-options)

通过以上步骤,你应该能够顺利使用 Microsoft C/C++ 扩展开发、调试和编译 C/C++ 程序。如果有其他问题,请随时提问!