【发布时间】:2014-07-29 03:22:16
【问题描述】:
有时,我似乎无法使用 lldb 调用带有字符串参数的函数。在这个问题 Invoking function with string argument with lldb: how? 中,我给出了一个简单的测试文件来说明问题。一位用户回答了这个问题,以说明在这种情况下修改测试文件将如何解决问题。
但对于更一般的情况,我仍然无法从 lldb 调用带有字符串参数的函数。
不过,要设置它,我需要显示两个文件。第一个文件就像在另一个问题中一样,一个简单的类 lldbtest,它定义了一个带有字符串参数的构造函数。然后,另一个文件定义了调试器将在其中中断的函数。
第一个文件(没有一些#includes)如下所示:
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; }
int main(){
string name="fum";
lldbtest x,y(3);
cout<<x.bar<<y.bar<<endl;
int zz=nothing();
return 0;
}
第二个文件如下所示:
using namespace std;
int nothing(){
cout<<"hello there"<<endl;
int n=0,m=1,r=2;
return m+r+n;
}
现在,当我在第二个文件的 return 语句处设置断点并评估时:
p lldbtest(string("bar"))
我收到一条很长的错误消息,很难在此处发布,但看起来像这样:
error: no matching conversion for functional-style cast from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'lldbtest'
note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'const lldbtest' for 1st argument
note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'lldbtest' for 1st argument
note: candidate constructor not viable: no known conversion from 'string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') to 'int' for 1st argument
note: candidate constructor not viable: expects an l-value for 1st argument
note: candidate constructor not viable: requires 0 arguments, but 1 was provided
error: 1 errors parsing expression
哇,错误信息比代码长。
无论如何,我的问题是:如何从 lldb 中的字符串参数调用函数/构造函数,而不是像这里定义/声明构造函数的文件?
使用 g++ -g -std=c++11 编译。我使用这些相同的选项来链接目标文件,在 Mac 上也使用 g++。
【问题讨论】: