[zeromq-dev] Compilation fails when NDEBUG is enabled.

Arthur O'Dwyer arthur at push.am
Tue Aug 14 23:37:08 CEST 2012


On Tue, Aug 14, 2012 at 1:39 PM, Chernyshev Vyacheslav <astellar at ro.ru> wrote:
>
> In recent git master tree there are 2 tests with issues that cause
> compilation errors if warnings are treated as errors and NDEBUG is
> defined during compilation.
[...]
> As a side note, I really think that NDEBUG _must_ be ignored for all
> test cases, so probably there should be some file that is included on
> top of all tests and contains something like:
>      #if defined(NDEBUG)
>      #  undef NDEBUG
>      #endif

Actually, this is the *main* note. :)  All of the tests use assert()
to check for failure, so if you compile the tests with NDEBUG turned
on, the tests will always pass, whether there are bugs or not!

You could just declare that nobody should ever try to compile the
tests with NDEBUG turned on --- just compile the non-NDEBUG tests
against the NDEBUG-compiled library --- but it's just barely plausible
that there could exist a bug in the header files themselves that
manifests only with NDEBUG. (For example, an inline function whose
behavior depends on side effects nested inside an assert().)

Luckily there is a fix even easier and cleaner than what you
suggested: Just make sure that each test_*.cpp file contains these two
lines after it #includes all the other headers.

    #undef NDEBUG
    #include <assert.h>

The Standard guarantees that you can #include <assert.h> multiple
times, and that it's only the most recent inclusion (and setting of
NDEBUG) which takes effect. So after the two lines above, you're
guaranteed that assert(x) will actually be enabled.

Hope this helps,
-Arthur



More information about the zeromq-dev mailing list