I was looking at the source of SystemC and saw that there are things like:
#define DEBUGF \
if (0) std::cout << "sc_cor_pthread.cpp(" << __LINE__ << ") "
and later on there are lines such as:
DEBUGF << this << ": sc_cor_pthread::sc_cor_pthread()" << std::endl;
(these are from sc_cor_pthread.cpp)
I have already enabled debug option when configuring using ../configure --enable-debug but it doesn't seem to activate these kinds of stuff. How am I supposed to turn these on instead of manually modifying source?
Add this to your compile line:
-DDEBUGF
Related
Using C++ with the CMake Tools extensions for VSCode... when I debug my program it writes to VSCode's Debug Console but every line starts with "#" and shows the newlines at the end:
std::cout << "Hello, World!" << std::endl;
std::cout << "Another line..." << std::endl;
#"Hello, World!\r\n"
#"Another line...\r\n"
How do I configure VSCode and/or the cmake extension to print 'normally'?
I used a code from here (Color folder) in Visual Studio 2013 to capture color data from Kinect v2. But when I want to run the code, these lines of code in util.h file causes a compile error:
#define ERROR_CHECK( ret ) \
if (FAILED(ret)){
\
std::stringstream ss; \
std::ss << "failed " #ret " " << std::hex << ret << std::endl; \
throw std::runtime_error(ss.str().c_str()); \
}
And my Visual Studio below draws ss a red line and is written: namespace "std" has no member "ss" below <<: expected a ";" and below throw: expected a declaration.
What is this part of code and how can I fix it?
This code is wrong, changing std::ss to ss should help:
#define ERROR_CHECK( ret ) \
if (FAILED(ret)) { \
\
std::stringstream ss; \
ss << "failed " #ret " " << std::hex << ret << std::endl; \
throw std::runtime_error(ss.str().c_str()); \
}
Another error in the code snippet you show that there was a line continuation missing after the if(statement).
Also ensure that <sstream> is included when using this.
The macro seems to be already fixed in the original code, but can be that other buggy versions of util.h are existing in the other sample folders.
It may not be what you wanted, but does your program work if you just comment every line? It is "just" a stringstream that only purpose (as far as I can see) is to print out when an error occurs. Replace it with some kind of printf() if you really want to see the output.
#define ERROR_CHECK( ret )
if (FAILED(ret)){
// std::stringstream ss;
// std::ss << "failed " #ret " " << std::hex << ret << std::endl;
// throw std::runtime_error(ss.str().c_str());
printf("Error : FAILED(ret)");
}
This way your code compiles but you do not have the original throw of an error IFF an error occurs
I have inherited a piece of C++ code which has many #ifdef branches to adjust the behaviour depending on the platform (#ifdef __WIN32, #ifdef __APPLE__, etc.). The code is unreadable in its current form because these preprocessor directives are nested, occur in the middle of functions and even in the middle of multi-line statements.
I'm looking for a way of somehow specifying some preprocessor tags and getting out a copy of the code as if the code had been pre-processed with those flags. I'd like the #include directives to be left untouched, though.
Example:
#include <iostream>
#ifdef __APPLE__
std::cout << "This is Apple!" << std::endl;
#elif __WIN32
std::cout << "This is Windows" << std::endl;
#endif
would turn into:
#include <iostream>
std::cout << "This is Apple!" << std::endl;
after being processed by: tool_i_want example.cpp __APPLE__.
I've hacked a quick script that does something similar, but I'd like to know of better tested and more thorough tools. I am running a Linux distribution.
I have decided against just running the C-preprocessor because if I'm not mistaken it will expand the header files, which would make everything more unreadable.
Use unifdef. It is designed for that purpose.
Complementing Basile Starynkevitch's answer, I want to mention coan. The major advantage is that, when used with -m it does not require the user to unset all symbols they want undefined.
This code:
#include <iostream>
#ifdef __ANDROID__
std::cout << "In Android" << std::endl;
#endif
#ifndef __WIN32
std::cout << "Not a Windows platform" << std::endl;
#endif
#ifdef __APPLE__
std::cout << "In an Apple platform" << std::endl;
#elif __linux__
std::cout << "In a Linux platform" << std::endl;
#endif
would result in this code if simply run as: unifdef -D__APPLE__ example.cpp:
#include <iostream>
#ifdef __ANDROID__
std::cout << "In Android" << std::endl;
#endif
#ifndef __WIN32
std::cout << "Not a Windows platform" << std::endl;
#endif
std::cout << "In an Apple platform" << std::endl;
Using unifdef one would need to use
unifdef -D__APPLE__ -U__ANDROID__ -U__WIN32 -U__linux__ example.cpp:
#include <iostream>
std::cout << "Not a Windows platform" << std::endl;
std::cout << "In an Apple platform" << std::endl;
This can get exhausting quickly when dealing with code considering several different platforms. With coan it's a matter of:
coan source -D__APPLE__ -m example.cpp.
I know there many subjects about this but none of them helps me.
I use in my C/C++ project std::cout and std::cerr to print info (cout) or error (cerr).
But when executing it they don't print in the right order, they seems to "group print". Sometime all cerr then all cout and sometime all cout first then all cerr.
I tried to flush() after every line, don't work. (luckily, it would be awful having to use it every time ...).
Also tried setvbuf(stdout, NULL, _IONBF, 0); same issue...
If run program directly in linux's console, order is good but eclipse console more useful due to colors.
Here code sample
#include <iostream>
int main(int argc, char** argv)
{
std::cerr << __LINE__ << std::endl;
std::cerr << __LINE__ << std::endl;
std::cout << __LINE__ << std::endl;
std::cerr << __LINE__ << std::endl;
std::cerr << __LINE__ << std::endl;
std::cout << __LINE__ << std::endl;
}
And console print
11
12
14
15
13
16
==> Wrong order ...
In this example cerr comes out before cout
Ok the situation is as follows, std::cout is added into a buffor, and std::cerr does not. std::cerr is faster and it does not need to be flushed. Also std::cerr and std::cout uses different streams.
The problem here is that std::cerr shows right away and std:cout needs to be flushed before it's showed.
Is there easily embeddable C++ test lib with a friendly license? I would like a single header file. No .cpp files, no five petabytes of includes. So CppUnit and Boost.Test are out.
Basically all I want is to drop single file to project tree, include it and be able to write
testEqual(a,b)
and see if it fail. I'd use assert, but it doesn't work in non-debug mode and can't print values of a and b, and before rewriting assert I'd rather search existing library.
I'm tempted to say "write your own", which is what I have done. On the other hand, you might want to reuse what I wrote: test_util.hpp and test_util.cpp. It is straightforward to inline the one definition from the cpp file into the hpp file. MIT lisence. I have also pasted it into this answer, below.
This lets you write a test file like this:
#include "test_util.hpp"
bool test_one() {
bool ok = true;
CHECK_EQUAL(1, 1);
return ok;
}
int main() {
bool ok = true;
ok &= test_one();
// Alternatively, if you want better error reporting:
ok &= EXEC(test_one);
// ...
return ok ? 0 : 1;
}
Browse around in the tests directory for more inspiration.
// By Magnus Hoff, from http://stackoverflow.com/a/9964394
#ifndef TEST_UTIL_HPP
#define TEST_UTIL_HPP
#include <iostream>
// The error messages are formatted like GCC's error messages, to allow an IDE
// to pick them up as error messages.
#define REPORT(msg) \
std::cerr << __FILE__ << ':' << __LINE__ << ": error: " msg << std::endl;
#define CHECK_EQUAL(a, b) \
if ((a) != (b)) { \
REPORT( \
"Failed test: " #a " == " #b " " \
"(" << (a) << " != " << (b) << ')' \
) \
ok = false; \
}
static bool execute(bool(*f)(), const char* f_name) {
bool result = f();
if (!result) {
std::cerr << "Test failed: " << f_name << std::endl;
}
return result;
}
#define EXEC(f) execute(f, #f)
#endif // TEST_UTIL_HPP
Try google-test https://github.com/google/googletest/
it's really light weight, cross platform and simple.