创建 C++ GDB 中的字符串
我在 GDB 中创建 std::string
(或任何 C++ 对象,我猜)时遇到问题。我尝试了以下多种变体,但似乎都不起作用:
(gdb) p std::string("hello")
A syntax error in expression, near `"hello")'.
有办法做到这一点吗?
(我很惊讶我在网上找不到任何关于此的信息。我开始思考我的 GDB 是否有问题或者我做错了什么。)
I'm having trouble creating an std::string
(or any C++ object, I guess) in GDB. I tried lots of variations to the following and none of them seem to work:
(gdb) p std::string("hello")
A syntax error in expression, near `"hello")'.
Is there a way to do it?
(I'm surprised I couldn't find anything about this on the Web. I'm starting to think if my GDB is buggy or I'm doing something very wrong.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够在 GDB 中构造一个新的 std::string 。您想要在堆上分配空间来保存 std::string 对象、调用默认构造函数并分配字符串值。这是一个例子:
You should be able to construct a new std::string within the GDB. You want to allocate space on the heap to hold the std::string object, invoke the default constructor, and assign your string value. Here is an example:
GDB 不能真正做到你所描述的。您的案例涉及:
这意味着它必须完成与编译器相同复杂性的工作。这不是调试器的工作。
话虽如此,GDB 能够评估语句的有限子集,例如使用现有数据调用现有函数并检索其结果,因为这不会涉及生成很多代码。
GDB cannot really do what you describe. Your case involves:
basic_string
template and generating code for the classThis means it must do the work of the same complexity as a compiler. This is not the job of the debugger.
With that said, GDB is capable of evaluating a limited subset of statements, like calling an existing function with existing data and retrieving its result, since this won't involve generating a lot of code.
你所说的“创造”是什么意思? GDB 不会持久保存 C++ 对象(您的应用程序会这样做),因此您无法在 GDB 本身中创建 C++ 对象。
但是,您应该能够从 GDB 调用应用程序的特定函数。
What do you mean by "creating"? GDB doesn't persist C++ objects (your application does that), so you can't create a C++ object in GDB itself.
However, you should be able to call specific function of your application from GDB.