调试时不能不进入功能

时间:2017-04-02 09:21:45

标签: c++ debugging gdb

我遵循了简单的测试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.

我发现只有当函数的参数类型为string时才会出现此错误。例如:

void hello(int i);

我可以毫无问题地进入函数hello。

我使用以下命令查找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

为什么会这样? THX !!!

1 个答案:

答案 0 :(得分:1)

  

为什么会发生这种情况?

您想进入void hello(),但步入std::string复制构造函数。现在,您可以使用std::string命令离开finish构造函数并进入void hello()

(gdb) finish
(gdb) step

另一种选择是通过引用将字符串参数传递给void hello()以避免不必要的复制。这样,您只需一步即可进入所需的功能:

void hello(const string& str) {
    cout << str << endl;
}
相关问题