我是lldb的初学者,我想知道lldb是否可以调用需要
std::string
参数的成员函数,所以我编写了以下演示。
#include <string>
#include <unordered_map>
struct A {
std::unordered_map<std::string, std::string> m;
std::string at(const std::string &s) { return m.at(s); }
};
// help function to generate a std::string in lldb
std::string SSS(const char *s) { return std::string(s); }
int main() {
A a = {{}};
a.m.insert({"good", "bad"});
a.m.insert({"foo", "bar"});
a.m.insert({"foofoo", "barbar"}); // <- set a breakpoint here
return 0;
}
我通过clang++编译这段代码,编译命令是
clang++ foofoobar.cpp -std=c++2b -g
。
在lldb中,我在
a.m.insert({"foofoo", "barbar"});
处设置了一个断点并启动二进制文件,程序在断点处停止,我尝试调用at
中定义的struct A
函数,我使用(lldb) p a.at(SSS("foo"))
来调用该函数,但lldb抛出错误error: expression failed to parse: error: Couldn't lookup symbols: __ZN1A2atERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE
。
这个错误是什么意思?我是否使用了错误的方式来调用成员函数?我该如何解决这个问题。我需要帮助。
由于
at
未在您的程序中使用,链接器不会将其添加到您的可执行文件中。因此调试器无法找到它来调用。您可以尝试直接使用该函数(尽管如果使用优化的代码,它仍然可以被内联,因此不会包含在可执行文件中),或者类似 &A::at
的内容可能会强制链接器不丢弃该方法。