Debug build but with STL optimized like release build

Is it possible to compile the application code I write in a "debug" version (i.e. with symbols, ability to use breakpoints and step thru execution, watch variables, and all those goodies which aren't available in a release build) while forcing any underlying STL functionality to be optimized by the compiler (e.g. things like skipping array bounds checking and other performance enhancements) like a "release" build? I don't feel the need to step down into STL code during debugging and would rather keep the code running fast and have smaller binaries.

If so, how to configure? #defines? project settings or compiler flags? And is it "safe"?

I'm using VS2019 but the question applies to g++ and other compilers as well.

I'm hoping this is a softball question for experienced programmers, but I was unable to find an answer specific to the instance when I'm compiling my code and the STL code together in one build. (Related Q&A seems to be primarily about why not to combine executables which are compiled with one configuration with libraries compiled in the opposite configuration.)
https://pornchimp.com https://pornachi.com https://pornbrb.com https://pornbitte.com https://pornacho.com https://pornojefe.com https://pornoelle.com https://pornkoi.com https://pornzillo.com https://pornessa.com https://porndeus.com https://pornabelle.com https://pornadora.com https://pornarte.com https://pornbia.com https://porndrip.com https://pornether.com https://pornhush.com https://pornoio.com https://pornrei.com https://pornwick.com
Last edited on
In VS, you have Step Into (F11) and Step Over (F10) which either steps into a function on debug or steps over a function if you don't want to trace through a particular function (such as a standard library function).
With libstdc++ (GCC's implementation of the standard library) the "debug mode" is turned off by default so as long as you don't pass -D_GLIBCXX_DEBUG you won't get any range checking in operator[] and iterators, and so on.

To use the libstdc++ debug mode, compile your application with the compiler flag -D_GLIBCXX_DEBUG.
https://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode_using.html


GCC has an optimization flag -Og that enables some optimizations while still providing good debugging experience. I haven't actually tested to be honest but it sounds great.

Optimize debugging experience. -Og should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience. It is a better choice than -O0 for producing debuggable code because some compiler passes that collect debug information are disabled at -O0.
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-Og


As for turning on debug symbols with GCC you can use the -g flag (although there are other flags you can use). Just don't combine it with the -s flag or the strip command.

To tell GCC to emit extra information for use by a debugger, in almost all cases you need only to add -g to your other options.
https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html


libstdc++ could be compiled with a different optimization level than what you use to compile your own code files with but that will only affect the parts that gets "linked". It probably won't affect "header-only" stuff because it is compiled wherever it is included. E.g. if libstdc++ is compiled with -O2 and your code that uses std::sort is compiled with -Og then the std::sort code will end up using -Og because it's a template that gets compiled, instantiated and optimized as part of a translation unit that belongs to your code.
Last edited on
@Peter87: _GLIBCXX_DEBUG was a really useful tip. Thanks
Topic archived. No new replies allowed.