[英]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.