【发布时间】:2013-12-21 16:21:52
【问题描述】:
我正在使用 lldb 在 Xcode 5 中调试 C++ 程序,我想在调试器中评估任意表达式,尤其是那些使用重载运算符的表达式。
例如,我创建了一个非常简单的 Xcode 5 C++ 项目,其中包含以下 main.cpp 和所有编译器/链接器/等选项设置为默认值:
#include <iostream>
#include <vector>
int main(int argc, const char * argv[])
{
std::vector<int> vec;
vec.push_back(42);
std::cout << "vec[0] = " << vec[0] << std::endl;
return 0;
}
我在return 0; 行设置断点并运行程序。
然后,在 lldb 提示符下,将向量作为一个整体打印就可以了:
(lldb) expr vec
(std::__1::vector<int, std::__1::allocator<int> >) $0 = size=1 {
[0] = 42
}
但是,我无法使用重载的operator[] 访问其成员:
(lldb) expr vec[0]
error: call to a function 'std::__1::vector<int, std::__1::allocator<int> >::operator[](unsigned long)' ('_ZNSt3__16vectorIiNS_9allocatorIiEEEixEm') that is not present in the target
error: The expression could not be prepared to run in the target
同样,我无法获取迭代器(虽然我在这里经验较少,所以我的语法可能是错误的):
(lldb) expr vector<int>::iterator it = vec.begin()
error: use of undeclared identifier 'vector'
error: expected '(' for function-style cast or type construction
error: expected '(' for function-style cast or type construction
error: 3 errors parsing expression
和
(lldb) expr (vector<int>::iterator) vec.begin()
error: use of undeclared identifier 'vector'
error: expected '(' for function-style cast or type construction
error: expected '(' for function-style cast or type construction
error: 3 errors parsing expression
类似地,打印一个简单的字符串也可以正常工作:
(lldb) expr string("a")
(std::__1::string) $0 = "a"
但是,简单的字符串连接失败:
(lldb) expr string("a") + string("b")
error: invalid operands to binary expression ('string' (aka 'std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >') and 'string')
error: 1 errors parsing expression
我做错了什么? lldb 是否支持重载运算符的求值?
提前谢谢你!
【问题讨论】:
-
帧变量 vec[0] 应该可以打印向量中的元素
-
啊,确实有效,谢谢@EnricoGranata。但是,它不适用于获取向量迭代器或连接字符串。所以我是那里的 1/3。
-
我不确定为什么 vec[0] 在表达式中不起作用,但是如果您在源代码中使用 vec.begin(),那么您应该可以说 expr vec.begin( );试着在你的 main() 中调用 begin()