类似的问题也发生在我身上:error: Couldn't lookup symbols:
我的解决方案是在源代码的某处明确使用有问题的函数。
#include <vector>
template<typename T>
struct Vector : std::vector<T>
{
Vector(size_t n)
: std::vector<T>{n}
{}
T& operator[](size_t n)
{ return std::vector<T>::operator[](n); }
};
struct XXX
{
int x;
};
void func()
{
std::vector<XXX> a{10};
Vector<XXX> b{10};
auto x = b[0]; // gcc will produce an assembler code of operator[] for debug purpose
1; // as a break point
}
在第 1 行设置断点;并运行它。
(lldb) p a[0]
error: Couldn't lookup symbols:
__ZNSt3__16vectorI3XXXNS_9allocatorIS1_EEEixEm
(lldb) p b[0]
(XXX) $0 = (x = 0)
宾果!!该函数是否存在于 TEXT 块中?
(lldb) image lookup -r -n 'XXX.*operator'
1 match found in /Users/xxx/Library/Developer/Xcode/DerivedData/xxx:
Address: sandbox[0x00000001000011f0] (sandbox.__TEXT.__text + 256)
Summary: sandbox`Vector<XXX>::operator[](unsigned long) at main.cpp:19
我不确定,但我以前学过这个。在调试阶段,而不是生产阶段。如果我们在模板函数的行上设置断点,调试器会做什么?设置断点,实际上用陷阱或跳转替换一些现有的汇编代码,到处都是应用模板?或者只是在函数中设置一个断点?它是作为模板编写的。所以它应该在生产阶段内联。然而,在调试阶段,该函数不是内联的,而是作为普通函数编写的。请不要简单地相信我在这里所说的话。请自行确认。查阅gcc,clang,和lldb.的文档
#include <vector> 的 MacOS 10.13.6,Xcode 版本 9.4.1 有一个宏 _LIBCPP_INLINE_VISIBILITY:
template <class _Tp, class _Allocator>
inline _LIBCPP_INLINE_VISIBILITY
typename vector<_Tp, _Allocator>::reference
vector<_Tp, _Allocator>::operator[](size_type __n)
{
_LIBCPP_ASSERT(__n < size(), "vector[] index out of bounds");
return this->__begin_[__n];
}
_LIBCPP_INLINE_VISIBILITY 在#include <__config> 中定义为:
#define _LIBCPP_INLINE_VISIBILITY __attribute__ ((__visibility__("hidden"), __always_inline__))
hidden 和 __always_inline__ 这样的关键字似乎可以控制行为。
当我在上面的示例解决方案代码中添加inline _LIBCPP_INLINE_VISIBILITY 时:
inline _LIBCPP_INLINE_VISIBILITY
T& operator[](size_t n)
{ return std::vector<T>::operator[](n); }
导致:
(lldb) p b[0]
error: Couldn't lookup symbols:
__ZN6VectorI3XXXEixEm
我希望帮助和有人更深入地研究。