You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio Code中C++断点设置错位问题求助

C++断点错位问题(与Stack Overflow「breakpoint wrong place - vue」本质相同)

在C++调试场景中,点击编辑器 gutter 设置行断点时,断点会被创建到错误的行。例如点击第28行设置断点,断点实际会出现在第34行(编辑器视觉显示和调试器实际停止位置均如此)。仅少数行断点设置正常,大多数都会出现错位情况。

调试引擎日志

1: (41828) <-1052-break-insert -f B_Not_Quite_Latin_Square.cpp:28
1: (41837) ->1052^done,bkpt={number="12",type="breakpoint",disp="keep",enabled="y",addr="0x00007ff6e0de1880",
    func="std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char>>&, char)",
    file="C:\Data\CompetitiveProgramming\B_Not_Quite_Latin_Square.cpp",
    fullname="C:\Data\CompetitiveProgramming\B_Not_Quite_Latin_Square.cpp",
    line="34",thread-groups=["i1"],times="0",original-location="B_Not_Quite_Latin_Square.cpp:28"

日志可见,请求在第28行插入断点,但调试器实际将断点插入到了第34行。

当前配置文件

tasks.json

{
  "tasks": [
    {
      "label": "build",
      "type": "cppbuild",
      "command": "C:/Program Files/mingw64/bin/g++.exe",
      "args": [
        "-x",
        "c++",
        "-g",
        "-O2",
        "-std=gnu++20",
        "-static",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe"
      ],
      "options": {
        "cwd": "C:/Program Files/mingw64/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ],
  "version": "2.0.0"
}

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Debug",
      "preLaunchTask": "build",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",
      "logging": {"engineLogging":true},
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        },
        {
          "description": "Set Disassembly Flavor to Intel",
          "text": "-gdb-set disassembly-flavor intel",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

已尝试的解决方法

曾在launch.json中添加sourceFileMap配置,但问题未得到解决:

"sourceFileMap":{
  "${fileDirname}": {
    "editorPath": "${fileDirname}",
    "useForBreakpoints": true
  }
},

内容的提问来源于stack exchange,提问作者Simelvia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 05:35:29