我无法使用 lldb 调用采用字符串参数的简单、非模板函数。有没有办法让 lldb 理解 C++ 数据类型“string”,这是 C++ 程序中常用的数据类型?
这里的示例源代码只是创建了一个带有几个构造函数的简单类,然后调用它们(省略了“iostream”和“string”的包含):
using namespace std;
struct lldbtest{
int bar=5;
lldbtest(){bar=6;}
lldbtest(int foo){bar=foo;}
lldbtest(string fum){bar=7;}
};
int main(){
string name="fum";
lldbtest x,y(3);
cout<<x.bar<<y.bar<<endl;
return 0;
}
在 Mac Maverick 上用
编译时g++ -g -std=c++11 -o testconstructor testconstructor.cpp
程序运行并打印预期输出“63”。
但是,当在 main 中的 return 语句之前设置断点时,尝试调用构造函数会失败并显示一条神秘的错误消息:
p lldbtest(string("hey there"))
error: call to a function 'lldbtest::lldbtest(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)' ('_ZN8lldbtestC1ENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE') that is not present in the target
error: The expression could not be prepared to run in the target
可能也相关,命令:
p lldbtest(name)
什么都不打印。
此外,使用字符串文字调用构造函数也失败,标准方式:
p lldbtest("foo")
给出类似的长错误:
error: call to a function
'lldbtest::lldbtest(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)' ('_ZN8lldbtestC1ENSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE') that is not present in the targeterror: The expression could not be prepared to run in the target
有没有办法让 lldb 理解和使用 C++“字符串”数据类型?我有许多采用字符串参数的函数,需要一种从调试器调用这些函数的方法。在 Mac 上。
最佳答案
问题
这是由于您的代码存在一个微妙的问题,归结为 C++ 标准中的以下措辞:
7.1.2p3-4
Function specifiers[dcl.fct.spec]
A function defined within a class definition is an inline function.
...
An inline function shall be defined in every translation unit in which it is odr-used, and shall have exactly the same definition in every case (3.2).
你的构造函数 lldbtest(std::string)
是在 lldbtest
的主体中定义的,这意味着它将隐式是内联的,这进一步意味着编译器不会为它生成任何代码,除非它用于翻译单元。
由于定义必须出现在可能调用它的每个翻译单元中,我们可以想象编译器会说; “哎呀,我不需要这样做......如果其他人使用它,他们会生成代码”。
lldb 将寻找一个不存在的函数定义,因为 gcc
没有生成;因为你没有使用它。
解决方案
如果我们将 lldbtest
的定义更改为以下内容,我敢打赌它会按您的预期工作:
struct lldbtest{
int bar=5;
lldbtest();
lldbtest(int foo);
lldbtest(string fum);
};
lldbtest::lldbtest() { bar=6; }
lldbtest::lldbtest(int) { bar=7; }
lldbtest::lldbtest(string) { bar=8; }
但是.. p lldbtest(name)
?
lldb
中的命令p
用于打印*
信息,但也可用于计算表达式.
lldbtest(name)
不会调用 lldbtest
的构造函数,其中包含一个名为 name 的变量,它等同于声明 一个名为 name 的变量,类型为 lldbtest
; IE。 lldbtest name
在语义上是等价的。
关于c++ - 使用 lldb : how? 调用带有字符串参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24104814/