zur*_*uby 7 c++ visual-studio-code vscode-debugger
我在 Mac 上使用 Visual Studio Code 调试 c++11 代码时遇到问题。
具体来说,我的代码可以编译(使用 g++/clang++)并毫无问题地运行。但是当我在 VS code 中使用调试模式,一步步运行我的代码时,它会卡在某个随机点。然后点击步入/步过就没有反应了。左侧的变量窗口将永远显示一个加载圆圈,如下所示。我的笔记本电脑也会变热,此时风扇也会运转。
我已经尝试过下面的拓扑排序代码。有时它卡在主函数之后的第一行,有时它卡在其他地方。无需一步步运行,代码就可以执行到底并得到正确的结果。
我不知道发生了什么,所以我重新安装它,但问题仍然存在。我附加了 task.json 配置文件以及用于测试的代码。请帮助我并提出建议。提前致谢!
下面是我用来设置调试配置的tasks.json:
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++11",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
Run Code Online (Sandbox Code Playgroud)
下面是我在 c++11 中进行拓扑排序的代码:
#include <queue>
#include <unordered_map>
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
vector<int> topoSort(vector<vector<int>>& edgeInfo) {
queue<int> zeroDegree;
// ->(stuck at next line, without reaction when click on step into/over)
unordered_map<int, int> inDegree;
vector<int> answer;
vector<vector<int>> graph(4);
for (auto edge: edgeInfo) {
graph[edge[0]].push_back(edge[1]);
inDegree[edge[1]]++;
}
for (int i=0; i<graph.size(); ++i) {
if (inDegree[i] == 0) {
zeroDegree.push(i);
answer.push_back(i);
}
}
while (!zeroDegree.empty()) {
int curr_vertex = zeroDegree.front();
zeroDegree.pop();
for (auto neighbor: graph[curr_vertex]) {
inDegree[neighbor]--;
if (inDegree[neighbor] == 0) {
zeroDegree.push(neighbor);
answer.push_back(neighbor);
}
}
}
return answer;
}
};
int main() {
// ->(or stuck at next line)
vector<vector<int>> edgeInfo = {{0,1}, {1,2}, {1,3}};
Solution topologicalSort;
vector<int> answer = topologicalSort.topoSort(edgeInfo);
for (auto node: answer) {
cout << node << "-> ";
}
cout << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
小智 1
我不知道是什么原因导致此问题,也不知道如何解决真正的问题,但我已经找到了解决此问题的方法。
我在 2013 MacPro 上运行 macOS Monterey,在用于 C++ 开发的 VScode 中也遇到了这个问题。
根据朋友的提示,我安装了CodeLLDB扩展。不要忘记将该"type": "lldb"属性添加到 launch.json 以集成 CodeLLDB。
这为我解决了这个问题,除了启动一个稍微凉爽的调试器之外。