内联函数C++调试

问题描述 投票:0回答:1

当我调试并在求值表达式框中键入一些值时。我收到此错误“无法评估函数 - 可能是内联的”。谁能告诉我如何评估评估表达式框中的某些对象属性。 抱抱谢谢!

我搜索了与此问题相关的所有页面,所有页面都告诉我这是不可能的,因为 GBD 编译器优化功能。但我想再问大家一次。

这是我的 C++ 代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    stack<int> the_stack = stack<int>({1, 2, 3});
    return 0;
}

在我的示例中,如何在计算表达式框中键入“the_stack.size()”或“the_stack.top()”,然后获得预期值。

我的构建命令配置:

       {
      "label": "build",
      "type": "shell",
      "command": "g++",
      "args": ["-O0", "${file}", "-g", "-o", "${workspaceFolder}/build/${fileBasenameNoExtension}"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
}
visual-studio-code debugging gdb
1个回答
0
投票

您没有在代码中使用

the_stack.size()
并且
the_stack
是基于模板的实例,因此您不使用的成员函数很可能不存在于二进制文件中。

只需尝试

return the_stack.size();
而不是
return 0;
(只是为了使用成员函数),然后运行调试器并在单步执行初始化后执行
p the_stack.size();

可能的输出:

Reading symbols from stack...
(gdb) start
Temporary breakpoint 1, main () at stack.cpp:5
5           stack<int> the_stack = stack<int>({1, 2, 3});
(gdb) p the_stack.size()
$1 = 18446744073643061723
(gdb) n
6           return the_stack.size();
(gdb) p the_stack.size()
$2 = 3
(gdb)

您可以强制尽早实例化以生成成员函数。对于生产代码来说不是很好,但也许您会发现它很有用:

#include <stack>

template class std::stack<int>;

int main()
{
    std::stack<int> the_stack = std::stack<int>({1, 2, 3});
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.