Are asserts disabled in release build? [duplicate] - c++

This question already has answers here:
How can I completely disable calls to assert()?
(5 answers)
Closed 6 years ago.
Are asserts disabled in 'release' build?
How optional flags like -O0,-O3,-g of g++ affects it's behaviour?

assert() in the <cassert> header is only disabled if you define the macro NDEBUG prior to including the <cassert> header file. See also these docs
With gcc/g++, the easiest way to do so is to define the NDEBUG macro on the command line when invoking the compiler like so:
g++ -DNDEBUG ... other args...
Arguments such as optimization flags and similar flags does not disable the assert.

From the man page:
If the macro NDEBUG was defined at the moment assert.h was last
included, the macro assert() generates no code

Related

Is there a preprocessor Macro i can check to see if arc4random is available? [duplicate]

This question already has an answer here:
checking whether library exist via preprocessor
(1 answer)
Closed 12 months ago.
Windows doesn't seem to support arc4random. Is there a preprocessor macro I can use to identify if arc4random is supported by the platform I'm compiling for ?
https://man.openbsd.org/arc4random.3
The manual says it's from <bsd/stdlib.h>, so...
#if __has_include(<bsd/stdlib.h>)

Will -std=c++11 compiler flag be default at some point? [duplicate]

This question already has answers here:
Why C++11 compiler support still requires a flag?
(2 answers)
Closed 8 years ago.
As in title. Will it be default or to use C++11 features we will always have to add -std=c++11?
Right now, because of this option, C++11 still feels like some extra, non-standard thing.
To specify compiler, -std=c++11 flag is used by clang, g++ and even ICC.
With g++, use -dumpspecs to generate a specs file.
Use -print-search-dirs and first line is where to place specs file.
Add the -std=c++11 option appropriate place in specs file, on the line following *cc1plus:.

Eclipse: function 'to_string' could not be resolved [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Turn off eclipse errors (that arent really errors)
I'm facing this annoying issue: Eclipse refuses to recognize the std::to_string function, but my program compiles without errors. What am I missing?
According to cppreference the std::to_string function is defined in <string>, so I included it explicitly in the incriminated .cpp file. I also tried this, this and this solutions, with no luck.
Any other suggestions?
EDIT:
I'm using g++ 4.7.2 under Linux.
UPDATE: It's been a long time since I posted the original answer and it has become outdated. I double-checked today (Mar 15, 2014): in Eclipse Kepler (Build id 20130614-0229) it is sufficient to
add under Project > Properties > C/C++ Build > Settings then on the Tool Settings tab GCC C++ Compiler > Miscellaneous the -std=c++11 flag,
then under Window > Preferences > C/C++ > Build > Settings on the Discovery tab chose CDT GCC Built-in Compiler Settings and add the -std=c++11 flag to Command to get compiler specs. On my machine it looks like this after the change:
${COMMAND} -E -P -v -dD -std=c++11 "${INPUTS}"
clean and rebuild both your project and your index (Project > C/C++ Index > Rebuild) as Eclipse tends to cache error messages and show them even though they are gone after changing the settings.
This works on my machine for sure. If it doesn't on yours, then you might want to give a shot to this: C++11 full support on Eclipse although I am neither sure about the correctness of this approach nor was it necessary to do it on my machine. As of March 7, 2014 users claim that it helped them whereas the above approach didn't.
The original post, now outdated:
It seems like you have run into the common problem with Codan, see my answer here.
It isn't 100% clear how the code compiles. Within Eclipse? Or from command line, properly setting the flags? So just in case:
You are using a C++11 function. Do you pass the -std=c++0x or the -std=c++11 flags to the compiler (assuming gcc)?
You might have to also add __GXX_EXPERIMENTAL_CXX0X__ to your defines (again, assuming gcc) and restart Eclipse.
In my case eclipse believes __cplusplus is defined to 199711L but I'm quite certain that this should be defined to something along the lines of 201103L because the libstdc++ v3 uses
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else
in most of the new C++11 headers such as <future> and basic_string.h (where the definition of std::to_string is) which is included by <string>. Although when compiling with g++ (Built by MinGW-builds project) 4.8.0 20121225 (experimental) I get absolutely no error. This strange behaviour apparently confuses eclipse and makes it fail to properly phrase the included files.
Defining __cplusplus to something over 201103L before including the C++11 files should fix the bogus eclipse syntax errors such as Symbol 'shared_ptr' could not be resolved.
#undef __cplusplus
#define __cplusplus 201900L
After making the redefinition you'll want to right click on the project Index -> Rebuild & Freshen all files or even better restart eclipse all together.

capture C++0x flag inside the program [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
GNU C++ how to check when -std=c++0x is in effect?
What I want to do is:
#if defined(CPLUSPLUS_OXFLAG)
//do something
#else
//do something else
#endif
Now how can I capture -std=c++0x argument passed to the compiler(g++) to define my CPLUSPLUS_OXFLAG flag?
The GCC documentation states that the preprocessor symbol __GXX_EXPERIMENTAL_CXX0X__ is defined when compiling with -std=c++0x.
For GCC have a look at this:
__GXX_EXPERIMENTAL_CXX0X__
This macro is defined when compiling a C++ source file with the option -std=c++0x or -std=gnu++0x. It indicates that some features likely to be included in C++0x are available. Note that these features are experimental, and may change or be removed in future versions of GCC.
Find the reference here.
GCC defines __GXX_EXPERIMENTAL_CXX0X__ when std=c++0x is enabled.

C++ determine if compiling with debug symbols without defining a preprocessor symbol

I have been using something like this:
int main(int argc, char *argv[])
{
#ifdef DEBUG
printf("RUNNING DEBUG BUILD");
#else
printf("Running... this is a release build.");
#endif
...
However this requires me to compile with -DDEBUG for the debug build. Does GCC give me some way for me to determine when I am compiling with debug symbols (-g flag) such as defining its own preprocessor macro that I can check for?
Answer is no. Usually these macros (DEBUG, NDEBUG, _DEBUG) are set by the IDE/make system depending on which configuration (debug/release) you have active. I think these answers can be of help:
C #define macro for debug printing
Where does the -DNDEBUG normally come from?
_DEBUG vs NDEBUG
I think the answer that I was looking for was essentially what Adam posted as a comment, which is:
The compiler's job does not include preprocessing, and in fact the compiler will choke on any preprocessor switches not handled by the preprocessor that make their way into code.
So, because the way to branch code has to leverage the preprocessor, it means by the time the compiler gets any code it's already one or the other (debug code or release code), so it's impossible for me to do what my question asks at this stage (after preprocessor).
So it is a direct consequence of the preprocessor being designed as a separate process for feeding the code through.