[英]cannot not step into function while debugging
我的简单测试cpp遵循:
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
void hello(string str) {
cout << str << endl;
}
int main(int argc, const char **argv) {
string str = "hello world!";
hello(str);
return 0;
}
我用命令编译cpp:
g++ hello.cpp -o hello -g
然后以调试模式运行:
cgdb hello
(gdb) b main
(gdb) r
(gdb) n
(gdb) s
在gdb中使用step命令后,出现以下错误:
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string (this=0x7fffffffe5c0, __str="hello world!") at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:399
399 /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h: No such file or directory.
我发现仅当函数具有字符串类型的参数时才会发生此错误。 例如:
void hello(int i);
我可以顺利进入功能问候。
我使用以下命令查找allocator.h在哪里:
sudo find / | grep allocator.h
我得到的结果如下(仅列出部分结果):
/usr/include/c++/6.3.1/ext/bitmap_allocator.h
/usr/include/c++/6.3.1/ext/debug_allocator.h
/usr/include/c++/6.3.1/ext/new_allocator.h
/usr/include/c++/6.3.1/ext/extptr_allocator.h
/usr/include/c++/6.3.1/ext/throw_allocator.h
/usr/include/c++/6.3.1/ext/pool_allocator.h
/usr/include/c++/6.3.1/ext/array_allocator.h
/usr/include/c++/6.3.1/ext/malloc_allocator.h
/usr/include/c++/6.3.1/x86_64-pc-linux-gnu/bits/c++allocator.h
/usr/include/c++/6.3.1/bits/allocator.h
/usr/include/c++/6.3.1/bits/uses_allocator.h
/usr/include/gc/gc_allocator.h
为什么会这样? 谢谢!!!
为什么会这样?
您想进入void hello()
但进入std::string
复制构造函数。 现在,您可以使用finish
命令退出std::string
构造函数,并进入void hello()
:
(gdb) finish
(gdb) step
另一种选择是通过引用将字符串参数传递给void hello()
以避免不必要的复制。 这样,您只需一步即可进入所需的功能:
void hello(const string& str) {
cout << str << endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.