29

Our build system has somehow changed such that optimized builds are no longer getting the -DNDEBUG added to the compile line. I searched our makefiles and didn't find this.

Where does -DNDEBUG originate for most people and how might that have changed? Before we did have -DNDEBUG, and I don't think this was removed from any of our makefiles.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
WilliamKF
  • 41,123
  • 68
  • 193
  • 295

3 Answers3

20

Since the compiler can't decide on its own when to add the NDEBUG define and when not to, the flag is always set by either the makefile or project file (depending on your build system).

Tal Pressman
  • 7,199
  • 2
  • 30
  • 33
10

It is really difficult to answer this since you didn't specify the build system you are using. If you're using Autoconf, then either the user is expected to put -DNDEBUG in CPPFLAGS manually (or perhaps you were setting it from a CONFIG_SITE file that has changed), or the configure.ac file might set up a custom variable for setting NDEBUG, or configure.ac may invoke AC_HEADER_ASSERT, in which case adding --disable-assert to the invocation of configure will define NDEBUG in config.h. There are lots of ways it can happen.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • If I need to choose between doing `AC_DEFINE(NDEBUG,[],[])` and `CPPFLAGS="$CPPFLAGS -DNDEBUG"` in `configure.ac`, what should I do? Is it a disadvantage to have #define NDEBUG in config.h? If I do both I get lots of "NDEBUG duplicate definition" warnings that I would like to avoid. – peschü Apr 16 '17 at 19:49
  • 1
    If you are maintaining the autoconf metadata files, you should use AC_HEADER_ASSERT and let the user specify --disable-assert at configure time. The user is responsible for ensuring that -DNDEBUG is passed to the compiler if they so desire. It is not appropriate to define NDEBUG by default; that's not the package maintainer's prerogative. – William Pursell Apr 16 '17 at 22:07
  • What if -DNDEBUG is not enabled by default but by an option --enable-release? Would this be appropriate? And how would I activate -DNDEBUG in that case? – peschü Apr 17 '17 at 07:16
  • Regarding `--enable-release`, please read http://stackoverflow.com/questions/4553735/gnu-autotools-debug-release-targets/4680578#4680578 – William Pursell Apr 17 '17 at 14:46
5

In my experience, -DNDEBUG has been passed manually, i.e. the makefiles have been written in such a way that the flag is passed when building in release mode.

Zach Hirsch
  • 24,631
  • 8
  • 32
  • 29