C++ 调试时无法单步执行功能

C++ 调试时无法单步执行功能,c++,debugging,gdb,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!";

我的简单测试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;
}
然后以调试模式运行:

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.
我可以毫无问题地进入hello函数

我使用以下命令查找分配器.h的位置:

sudo find / | grep allocator.h
我得到的结果如下(仅列出部分结果):

为什么会发生这种情况? 谢谢

为什么会发生这种情况

您想进入
void hello()
,但进入了
std::string
copy构造函数。现在,您可以使用
finish
命令退出
std::string
构造函数,进入
void hello()

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

void hello(const string& str) {
    cout << str << endl;
}
void hello(常量字符串和str){

cout stdio.h是一个C头。请删除该包含并重试again@bejado与当前问题无关。可能与此无关,但肯定是因为您没有在DEBUG_FLAGS=ON的情况下编译libstdc++,或者您无权访问源的文件夹。/build/gcc/src/gcc build/is分支,因为它们是上次编译时的分支,/usr/include/只包含user@bejado“不允许”使用它们按照标准,通常只包含一行包含stdio.hthx的代码。这两种方法都有助于进入函数!但对于第一个选项,它似乎进入汇编代码。这是因为您尚未安装gcc/libstdc++的调试信息。但是如果您的目标是进入
hello()
您可以忽略这一点。谢谢~非常有帮助
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
(gdb) finish
(gdb) step
void hello(const string& str) {
    cout << str << endl;
}