1

I'm linking a static library that has a std::cout wrapper that works if I use it from the application code, but non of the library's internal outputs (used in exactly the same fashion) show any output.

Maybe it's not important, but I'm using Qt Creator and qmake project files to build. I have added console to the application's CONFIG (and even tried that for the static library, but it had no effect).

What could be going wrong and how can I fix this? Thanks!

UPDATE: well, the wrapper is an adapted version of this one:

Community
  • 1
  • 1
rubenvb
  • 74,642
  • 33
  • 187
  • 332

1 Answers1

2

The std::cout wrapper won't be able to 'reach in' to another library implicitly. Have you thought about redirecting cout altogether? Something likesrc:

int main() { 
    std::streambuf* cout_sbuf = std::cout.rdbuf(); // save original sbuf 
    std::ofstream   fout("cout.txt"); 
    std::cout.rdbuf(fout.rdbuf()); // redirect 'cout' to a 'fout' 
    // ... 
    std::cout.rdbuf(cout_sbuf); // restore the original stream buffer 
}

That way you'd have control over data fed to std::cout, regardless of the library doing the output (unless, of course, they redirect std::cout themselves.)

fbrereto
  • 35,429
  • 19
  • 126
  • 178
  • You should use RIAA to make sure that the original buffer is returned. I have seen implementations that dynamically allocate the cout buffer during construction and delete on destruction. If you mess up the pointers it leads to UB so you should restore the original buffer before the cout object is destroyed just in case (like you do but in an exception safe way). – Martin York Feb 17 '11 at 19:14
  • Well, it's my own library, and it doesn't do anything special to the cout stream. – rubenvb Feb 17 '11 at 21:05
  • 1
    I think you mean RAII - Resource Allocation Is Initialization, and not the recording industry ;) – Tim Feb 17 '11 at 21:07
  • I found my problem, it was an evil typo somewhere critical for this output to work... I accepted your answer nonetheless. Thanks though. – rubenvb Feb 20 '11 at 11:06