c - Compile-time assertion fails without GCC optimization -


i have following compile-time assertion fails if compile without -o[1-3] flags.

#ifndef __compiletime_error #define __compiletime_error(message) #endif #ifndef __compiletime_error_fallback #define __compiletime_error_fallback(condition) { } while (0) #endif  #define __compiletime_assert(condition, msg, prefix, suffix)        \     {                                \         int __cond = !(condition);              \         extern void prefix ## suffix(void) __compiletime_error(msg); \         if (__cond)                     \             prefix ## suffix();             \         __compiletime_error_fallback(__cond);           \     } while (0)  #define _compiletime_assert(condition, msg, prefix, suffix) \     __compiletime_assert(condition, msg, prefix, suffix)  #define compiletime_assert(condition, msg) \     _compiletime_assert(condition, msg, __compiletime_assert_, __line__)  #endif 

this combine following macro located in (gcc-4 specific) file:

#define __compiletime_error(message)    __attribute__((error(message))) 

the issue comes line in code:

        extern void prefix ## suffix(void) __compiletime_error(msg); \ 

it seems gcc not understand extern in macro without -o[1-3] flag. not sure how should declare __compiletime_error before gets called in macro. if remove line, famous warning of implicit declaration of function

your compiletime_assert framework relying on optimizer performing dead code elimination remove call prefix ## suffix. highly fragile , in no way guaranteed work.

instead, try using 1 of solutions ways assert expressions @ build time in c - or, since you're using modern compiler, use c11 _static_assert.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -