当前位置: 首页 > news >正文

Visual Studio Code 如何 Debug调试 CPP项目

本文由 ChatGPT 生成

Here’s a clean, practical way to set up C/C++ debugging in VS Code on Windows (since you’re using cl.exe and MSVC).


🧰 1. Install required tools

✅ Install:

  • Visual Studio Code
  • C/C++ Extension (Microsoft)
  • MSVC toolchain from Visual Studio 2022 (you already have it ✔)

⚙️ 2. Make sure MSVC environment is loaded

cl.exe only works inside the Developer Command Prompt.

👉 Either:

  • Launch VS Code from:

    x64 Native Tools Command Prompt for VS 2022
    code .
    

OR

  • Use vcvars64.bat inside tasks (more advanced)

🏗️ 3. Create build task (tasks.json)

Press:

Ctrl + Shift + P → "Tasks: Configure Task"

Example:

{"version": "2.0.0","tasks": [{"label": "build with cl","type": "shell","command": "cl","args": ["/Zi","/MDd","/Od","main.cpp","/Fe:main.exe"],"group": {"kind": "build","isDefault": true},"problemMatcher": ["$msCompile"]}]
}

🔑 Important flags:

  • /Zi → debug symbols
  • /Od → disable optimization
  • /MDd → match your Box2D build

🐞 4. Create debug config (launch.json)

Press:

F5 → "C++ (Windows)"

Then edit:

{"version": "0.2.0","configurations": [{"name": "Debug (MSVC)","type": "cppvsdbg","request": "launch","program": "${workspaceFolder}/main.exe","args": [],"cwd": "${workspaceFolder}","preLaunchTask": "build with cl","stopAtEntry": false}]
}

▶️ 5. Run debugger

Press:

F5

You can now:

  • set breakpoints
  • step (F10, F11)
  • inspect variables

🔍 Common issues (very relevant to you)

❗ Breakpoints not hit

➡ You forgot /Zi


❗ Symbols not loaded

➡ Make sure .pdb file is generated


❗ Box2D debugging doesn't work

➡ You need to build Box2D with debug symbols too:

cmake -DCMAKE_BUILD_TYPE=Debug

cl.exe not found

➡ You didn’t launch from Developer Command Prompt


🧠 Bonus: If using your custom build system (SelfMake)

Instead of cl directly:

  • Make tasks.json call your build tool
  • Keep /Zi /MDd /Od in your compile flags

👍 Minimal working mental model

  • tasks.jsonhow to build
  • launch.jsonhow to run/debug
  • /Zi → enables debugging
  • /MDd → must match all libs (your earlier issue!)

If you want, I can:

  • adapt this to your Box2D project
  • or wire it into your SelfMake build system so debugging works seamlessly
http://www.jsqmd.com/news/631715/

相关文章:

  • MPL3115A2气压温度传感器驱动开发与低功耗集成
  • CafeIOT嵌入式云连接库:轻量级二进制协议栈设计与ESP32实践
  • 结合数学思维来深入内存理解哈希散列的实现原理和处理冲突的逻辑膊
  • 如何将微信聊天记录变为个人数字资产:WeChatMsg完全指南
  • 数模加油站:以数为翼,为梦想加油 —— 赋能每一位建模者的成长之路
  • 一文看懂AnyIO:比asyncio更优雅的异步方案
  • iarduino I²C赛道模块控制库:面向教育与竞赛的嵌入式功能抽象层
  • 2026年正规螺杆泵真空泵技术参数判定全指南:螺杆式真空泵、螺杆泵真空泵、干式无油螺杆真空泵、干式真空泵、干式螺杆真空泵选择指南 - 优质品牌商家
  • 通过vue-virtual-scroller封装虚拟滚动el-select
  • 中文实体识别数据集:从新闻到社交媒体的多场景应用
  • 解密 FISCO BCOS 架构:排序-执行-验证模型与 PBFT 共识实践
  • 记录一个使用AI开发企业官网的思路
  • 语言的边界,与软件的命运厍
  • 2026优质手工净化板推荐榜:岩棉净化板/生物制药净化车间工程/电子净化车间工程/电池净化车间工程/硅岩净化板/选择指南 - 优质品牌商家
  • 告别Python+Netmiko!Rust+NexusOps如何重塑网络自动化
  • SecGPT-14B效果展示:对比传统规则引擎,其在模糊语义安全问题理解上的优势
  • C++ vs .NET 数组原地反转实测:小数组 C++ 碾压,大数组 .NET 反杀?缴
  • sql使用技巧
  • 前端八股Vue(6)---v-if和v-for
  • “2 - 6岁孩子该读什么绘本?
  • 从零到一:基于OpenMV的智能小车多任务视觉识别与巡线实战解析
  • 嵌入式OTA封装库:解耦硬件与升级逻辑的生产级抽象层
  • SpringCloud进阶--Sentinel 流量防卫兵怕
  • DDR link ECC
  • MiniMax M. 发布!Redis 故障排查 + 跨语言重构场景实测,表现如何?确
  • LangGraph的容错机制:从节点失败到图级回滚
  • **用Python实现基于规则的音乐生成系统:从代码到旋律的奇妙之旅**在人工智能与创意产业深
  • Win+Docker+qwen.本地化养虾烙
  • 【独家首发】2026奇点大会未公开议程泄露:向量数据库将淘汰传统Embedding预计算(附性能对比实测数据)
  • 笑晕!我和朋友调试出一行“鬼畜C++代码”,4个nullptr叠满还能完美运行