15

I'm trying to call std::cout within lldb in an Xcode 5 C++ project. My project has an #include <iostream> line (and I verified that compiled std::cout commands work fine), but it does not have a using namespace std; line.

When I stop at a breakpoint in lldb, I can't call std::cout:

(lldb) expr std::cout << "test"
error: no member named 'cout' in namespace 'std'
error: 1 errors parsing expression

(lldb) expr cout << "test"
error: use of undeclared identifier 'cout'
error: 1 errors parsing expression

For those interested, I'm trying to use std::cout to print an OpenCV Mat object. But that detail is probably not important.

My lldb version is lldb-300.2.53.

By request, here's the (trivial) code:

#include <iostream>
int main(int argc, const char * argv[])
{
  std::cout << "Hello World" << std::endl;
  return 0;
}

The breakpoint is at the return 0; line.

Daniel Golden
  • 3,752
  • 2
  • 27
  • 32
  • Can you post the code? – IllusiveBrian Dec 06 '13 at 18:00
  • `expr std::cout << "test"` is not a trivial expression and i think it will not work there since it involves a function call. – Raxvan Dec 06 '13 at 18:06
  • @DieterLücking, if by "actual code", you mean my lldb command, I tried both `std::cout` and `cout`. The code for the project in which I tested the lldb commands is explicitly as I wrote it in the OP, without a `using namespace std;` line. – Daniel Golden Dec 06 '13 at 18:35
  • @DieterLücking, no, you can see that the `#include ` line is explicitly in my code sample. I also confirmed that the `std::cout` command works in the compiled code, just not in the lldb debugger. – Daniel Golden Dec 06 '13 at 22:02
  • How about expr std::cout.operater<<("test") ? – ZijingWu Dec 06 '13 at 23:33
  • @ZijingWu, you meant `std::cout.operator<<("test")` (there was a typo in your original command). But the result is the same: `error: no member named 'cout' in namespace 'std'`. – Daniel Golden Dec 06 '13 at 23:36

3 Answers3

3

maybe you can do it by another way:

1, create a dylib, import all headers needed, write a function like this:

void mylog(const MyObject& obj)
{ 
   //assume MyObject is the type you want to view in Debuger
   std::cout << obj << std::endl;
}

build as libdbghelper.dylib in your desktop(or another path which is short).

2,load it in to your debugging project:

(lldb) target modules add /Users/yourName/Desktop/libdbghelper.dylib

3,then you can log it with command

(lldb)expr mylog((const MyObject&)myobj);

here is the running result in my mac: https://i.stack.imgur.com/LBBLJ.jpg

the code of dylib like that: https://i.stack.imgur.com/H1Q9v.jpg

ccnyou
  • 337
  • 2
  • 11
  • This worked, except I built a separate Xcode project with that `std::cout` wrapper and then I included it in my current Xcode project using the steps from [this other stack overflow post](http://stackoverflow.com/questions/9370518/xcode-4-2-how-include-one-project-into-another-one). I didn't have to include the headers to use the `mylog` function; just the library. Now all I have to do is figure out how to evaluate [overloaded operators in lldb](http://stackoverflow.com/questions/20387942/evaluating-an-expression-with-overloaded-operators-in-c-lldb) so that I can examine vector elements. – Daniel Golden Dec 09 '13 at 19:28
2

you cannot using std::cout in commandline as you cannot WATCH it in ANY Debuger, but you can declare a reference to it like this:

std::ostream& os = std::cout;

so that you can execuate command expr os << "ok" in lldb.

here is the running result in my mac:

https://i.stack.imgur.com/lHvfa.jpg

hope it helpful

ccnyou
  • 337
  • 2
  • 11
  • 1
    That seems to print the structure of `os`, but it doesn't actually print the string ("ok" in your example). Plus, this won't work when debugging a shared library (which I didn't mention in the OP), since it's hard or impossible to modify the code to add the `std::ostream& os = std::cout;` line. I'd prefer a debugger-only solution. – Daniel Golden Dec 09 '13 at 15:36
1

I'm not positive this is a dup but I believe the answer from Jim Ingham over in

Evaluating an expression with overloaded operators in c++ lldb

is likely highly relevant to the problem you're seeing here.

Community
  • 1
  • 1
Jason Molenda
  • 14,835
  • 1
  • 59
  • 61
  • That's right, the root cause of each issue is probably the same (inlined template functions in the standard library). It's no coincidence that the other stack exchange article that you're referencing is also mine! – Daniel Golden Feb 10 '14 at 00:31