Use C++ std::cout in lldb - c++

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.

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:
http://i.stack.imgur.com/LBBLJ.jpg
the code of dylib like that:
http://i.stack.imgur.com/H1Q9v.jpg

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:
http://i.stack.imgur.com/lHvfa.jpg
hope it helpful

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.

Related

Why does codeblocks ide raise: fatal error: iostream: no such file or directory

when I run this test code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << end1;
return 0;
}
with a more than less than around iostream just can't use it on stackover flow.
I got this code from an old book (2014) it might be outdated. I've never used C++ before how do I fix this.

CLion finds a wrong function signature

I constantly have CLion editor showing me parameter type mismatch errors while during build everything is fine. For example, consider the following MWE:
#include <iostream>
#include <boost/container/flat_set.hpp>
using namespace std;
namespace bc = boost::container;
int main() {
bc::flat_set<bc::flat_set<int>> manySets;
bc::flat_set<int> oneSet({1, 2, 3});
manySets.insert(oneSet);
cout << "Hello, World!" << endl;
return 0;
}
Here flat_set is a template from boost library (description could be seen here). Editor shows me an error:
But when I build it (even from CLion), everything is compiled fine.
My system is:
Ubuntu 15.10 64bit
CLion 1.2.4
This looks like a known problem - https://youtrack.jetbrains.com/issue/CPP-6027. We hope to fix it soon.

C++ and Visual Studio error - no suitable conversion function from "std::basic_ostream<char, std::char_traits<char>>" to "int" exists

Visual Studio is going crazy on me recently, and gives me the error in the subject when all I did was a simple cout...
CODE:
// Lang.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main{
cout << "hi";
}
int main{
cout << "hi";
}
Due to the possibility of initialising objects in C++ with the {} syntax, your compiler probably interprets this code as an attempt to create a global int variable called main, initialised with the result of std::ostream::operator<<, a member function which returns a reference to the std::ostream itself.
It's as if you had written:
double some_variable { cout << "hi" }
Or:
double some_variable { cout }
std::ostream is actually std::basic_ostream<char, std::char_traits<char>>. And that type is not compatible with int.
The only thing which is strange is why the ; after "hi" does not immediately cause the compiler to stop trying; but you don't say which compiler version and which options you are using.
In any case, all of those facts finally result in the error message:
no suitable conversion function from “std::basic_ostream<char,
std::char_traits<char>>” to “int” exists
and in:
Also, the semicolon after "hi" is highlighted, and says "expected a }"
Solution: make main a function:
int main() {
cout << "hi";
}

boost iostreams assertion failure

I need to be able to use a single fstream to have platform-independent way of using files. In particular, I need to be able to support file paths with unicode characters on Windows with as minimal intrusion into code to support it as possible. As such, it seemed like boost iostreams could provide the answer. However, upon trying to use it, I get two failures which cause me concern. See the following sample code:
// MinGW (MSYS)
// GCC 4.7.2
// Boost 1.50.0
// g++ -g ifstreamtest.cpp -o test.exe -I /t/tools/boost/boost_1_50_0 -L /t/tools/boost/boost_1_50_0/stage/lib -lboost_system-mgw47-mt-d-1_50 -lboost_filesystem-mgw47-mt-d-1_50 -lboost_locale-mgw47-mt-d-1_50 -lboost_iostreams-mgw47-mt-d-1_50
#include <boost/locale.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/filesystem/path.hpp>
namespace MyNamespace
{
typedef ::boost::iostreams::file_descriptor fd;
typedef ::boost::iostreams::stream< ::boost::iostreams::file_descriptor> fstream;
typedef ::boost::iostreams::stream< ::boost::iostreams::file_descriptor_sink> ofstream;
typedef ::boost::iostreams::stream< ::boost::iostreams::file_descriptor_source> ifstream;
} // namespace MyNamespace
int main(int argc, char **argv)
{
// Imbue boost filesystem codepoint conversions with local system
// Do this to ensure proper UTF conversion.
boost::filesystem::path::imbue(boost::locale::generator().generate(""));
// Test file path.
boost::filesystem::path file_path("test.txt");
// Anonymous scope for temporary object.
{
// Open file in ctor, write to output, neglect to clean up until dtor.
MyNamespace::ofstream output(file_path, std::ios_base::out | std::ios_base::app);
if ( output.is_open() == false ) std::cout << "Unable to open #" << __LINE__ << std::endl;
output << "test line 1" << std::endl;
std::cout << "done #" << __LINE__ << std::endl;
}
// Temporary object destroyed while still open.
// Anonymous scope for temporary object.
{
// Open file in ctor, write to output, specifically close file.
MyNamespace::ofstream output1(file_path, std::ios_base::out | std::ios_base::app);
if ( output1.is_open() == false ) std::cout << "Unable to open #" << __LINE__ << std::endl;
output1 << "test line 2" << std::endl;
output1.close();
std::cout << "done #" << __LINE__ << std::endl;
}
// Temporary object destroyed.
// Anonymous scope for temporary object.
{
// Default-ctor; open later. Write to file, neglect to clean up until dtor.
MyNamespace::ofstream output2;
// Next line causes "Assertion failed: initialized_, file t:/tools/boost/boost_1_50_0/boost/iostreams/detail/optional.hpp, line 55"
output2->open(file_path, std::ios_base::out | std::ios_base::app);
if ( output2.is_open() == false ) std::cout << "Unable to open #" << __LINE__ << std::endl;
output2 << "blah test test blah" << std::endl;
}
// Temporary object destroyed.
// MyNamespace::ifstream input;
// Compile success, but linker failure:
// s:\reactor\utf8stream/ifstreamtest.cpp:42: undefined reference to `void boost::iostreams::file_descriptor_source::open<boost::filesystem::path>(boost::filesystem::path const&, std::_Ios_Openmode)'
// input->open(file_path, std::ios_base::in);
std::cout << "done." << std::endl;
return 0;
}
On Windows, I am limited to GCC 4.7.2 and Boost 1.50.
The comments explain the two failures, but I will expand on them here. The first and most problematic to me is when I try to use the stream object as a "normal" fstream object. If I open the fstream in its constructor, then all is fine and dandy (as can be seen in the first two anonymous scopes). But if I allocate the fstream object and then attempt to open it later, "Bad Things" happen.
If I try to invoke boost::iostreams::stream::open(), I get compiler errors saying that it can't convert parameter 1 (boost::filesystem::path) to a boost::iostreams::file_descriptor_sink. Why should that not work when it can be constructed with a boost::filesystem::path? In either case, attempting to call boost::iostreams::file_descriptor_sink::open() by using the stream's operator->() fails an assertion (as shown in the third anonymous scope). This is quite evil as it would seem to me that it should throw an exception instead of failing an assertion. Failing an assertion would indicate to me that there's a bug in the boost code.
The second failure I have is that the typedefed fstream and ofstream seem to work (well, compile and link) just fine. But when I try to use the ifstream, I get a linker failure when trying to call ifstream->open(); I get this on Windows (MinGW configuration as stated before) as well as on OS X 10.8.5 with Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn). Since it compiles fine and the only difference between the two there is whether it's a source or a sink... and both should be able to open a file... it makes me think that this is also a bug in boost.
Any thoughts or suggestions?
Regarding the compiler error:
I can reproduce the linking error on Linux, with g++ and Clang++, so it's not a specific Windows problem (I'm also using Boost 1.55.0, so it's not specific to 1.50).
I suppose it is caused by a template definition that is allowed in the header file, but not implemented in the source/library.
Solution (for the linking problem only): Instead of
input->open(file_path, std::ios_base::in);
use
input->open(file_path.string(), std::ios_base::in);
This circumvents the potentially misdefined template by using a string-based constructor.
Regarding the assertion error:
The issue is you need to initialize the file_descriptor_sink separately, else the iostream initialization won't be handled properly. Use this code:
//We need to initialize the sink separately
boost::iostreams::file_descriptor_sink output2Sink(file_path, std::ios_base::out | std::ios_base::app);
MyNamespace::ofstream output2(output2Sink);
if ( output2.is_open() == false ) std::cout << "Unable to open #" << __LINE__ << std::endl;
output2 << "blah test test blah" << std::endl;
open() doesn't seem to reset the optional that causes the assertion.
The same method needs to be applied to the the MyNamespace::ifstream:
boost::iostreams::file_descriptor_source inputSource(file_path, std::ios_base::in);
MyNamespace::ifstream input(inputSource);
//Test reading back what we wrote earlier
std::string inputContent;
input >> inputContent;
//Prints: blah (only the first word is read!)
std::cout << "Read from test.txt: " << inputContent << std::endl;
Also note that it is not neccessary to apply the solution to avoid the compiler error from above.
With these modifications your program appears to be working on my system.

linking <iostream.h> in linux using gcc

I'm trying to run my very first c++ program in linux (linux mint 8). I use either gcc or g++, both with the same problem: the compiler does not find the library I am trying to import.
I suspect something like I should either copy the iostream.h file (which I don't know where to look for) in the working folder, move my file to compile somewhere else or use an option of some sort.
Thanks for your suggestions.
Here's the gcc command, the c++ code, and the error message:
gcc -o addition listing2.5.c
.
#include <iostream.h>
int Addition(int a, int b)
{
return (a + b);
}
int main()
{
cout << "Resultat : " << Addition(2, 4) << "\n";
return 0;
}
.
listing2.5.c:1:22: error: iostream.h: No such file or directory
listing2.5.c: In function ‘main’:
listing2.5.c:10: error: ‘cout’ undeclared (first use in this function)
listing2.5.c:10: error: (Each undeclared identifier is reported only once
listing2.5.c:10: error: for each function it appears in.)
Now the code compiles, but I cannot run it from the command line using the file name. addition: command not found Any suggestion?
cout is defined in the std:: namespace, you need to use std::cout instead of just cout.
You should also use #include <iostream> not the old iostream.h
use g++ to compile C++ programs, it'll link in the standard c++ library. gcc will not. gcc will also compile your code as C code if you give it a .c suffix. Give your files a .cpp suffix.
please use g++ not gcc to compile it
You need <iostream> not <iostream.h>.
They are also header files not libraries.
Other things to fix, cout should be std::cout and you should use std::endl instead of "\n".
You need <iostream>, <iostream.h> is non-standard too-old header. Try this:
#include <iostream>
int Addition(int a, int b)
{
return (a + b);
}
int main()
{
using namespace std;
cout << "Resultat : " << Addition(2, 4) << "\n";
return 0;
}
If you don't want to use std alongside cout as below-
std::cout << "Hello World";
You can also define std at beginning of program by 'using namespace' keywords as-
#include <iostream >
using namespace std;
int Addition(int a, int b)
{
return (a + b);
}
int main()
{
cout << "Result : " << Addition(2, 4) << "\n";
return 0;
}
Now you need not to write std,everytime you use I/O operations.